【发布时间】:2018-02-16 12:17:35
【问题描述】:
我正在尝试使用 GridView1_RowEditing1 事件从 gridview 选择行传递会话值。我能够获取主键(p.key)的会话值,但 Associate_ID 返回空。
背后的C#代码
protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
{
int primarykey = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value);
string name = Convert.ToString(GridView1.Rows[e.NewEditIndex].FindControl("Associate_ID"));
Session["Number"] = primarykey;
Session["Name"] = name;
//redirect
Response.Redirect("updatee.aspx");
}
.ASPX
<asp:TemplateField HeaderText="Associate_ID" SortExpression="Associate_ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Associate_ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Associate_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
输出:
键:5
姓名:
如您所见,我没有获得所选字段的 Associate_ID。 也尝试使用
string name = Convert.ToString(GridView1.Rows[3].Cells[3].FindControl("Associate_ID"));
但是没有结果,网格有多列多行(8*15)
在gridview findcontrol returning empty "" 上尝试了建议,并尝试停止在 PostBack 上重新绑定 GridView。 从 gridview 中删除 DataSourceID
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowEditing="GridView1_RowEditing1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
后面的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//DataSourceID = "SqlDataSource1"
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
这些都没有帮助。我仍然得到 Associate_ID 的空白值。 我看不出问题出在哪里。请帮忙!
【问题讨论】:
标签: c# asp.net gridview postback findcontrol