当我单击编辑时,我有一个10列的网格视图,需要在另一个页面中显示数据,行数据处于可编辑模式,还有其他一些表格列.请帮帮我...请......,
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class displaypage : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(@"Data Source=SRAVI-PC\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
getdata();
}
public void getdata()
{
string cmd = "select * from namestb";
SqlDataAdapter da = new SqlDataAdapter(cmd, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("editpage.aspx");
}
}
} <code>
Run Code Online (Sandbox Code Playgroud)
的.aspx
<pre>
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="displaypage.aspx.cs" Inherits="WebApplication1.displaypage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" Height="30px"
ImageUrl="~/images/edit image.jpg" Width="38px"
onclick="ImageButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
</div>
</form>
</body>
</html>
<code>
Run Code Online (Sandbox Code Playgroud)
您需要在GridView中添加RowCommand事件.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
<asp:LinkButton ID ="lnkEdit" runat ="server" CommandArgument='<%#Eval("Recordid")%>' CommandName ="cmdEdit" Text ='Edit'></asp:LinkButton>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
这里rocordid是你的记录的id.
在Page behind中你需要编写代码.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdEdit")
{
string Recordid = Convert.ToString(e.CommandArgument.ToString());
Response.Redirect("EditPage.aspx?recordid="+Recordid );
}
}
Run Code Online (Sandbox Code Playgroud)
在EditPage上,您可以从查询字符串中获取recordid,并可以从数据库中获取记录以供进一步处理.
我希望它会对你有所帮助
| 归档时间: |
|
| 查看次数: |
3540 次 |
| 最近记录: |