在 RowDataBound-Event returns 上找到控件作为 NULL/Empty
Find Control on RowDataBound-Event returns as NULL/Empty
我正在尝试使用 FindControl
从我的 GridView 中获取 selected 行的文本值,但 FindControl
始终 returns 为 NULL。
.ASPX代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CID" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CID" HeaderText="CID" InsertVisible="False" ReadOnly="True" SortExpression="CID" />
<asp:BoundField DataField="CountryID" HeaderText="CountryID" SortExpression="CountryID" />
<asp:TemplateField HeaderText="CountryName" SortExpression="CountryName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CountryName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = e.Row.FindControl("TextBox1") as TextBox;
string name = txt.Text; // returns as NULL
}
}
任何人都可以指出我在这里做错了什么或者有任何其他方法可以做到这一点吗?当单击 select 按钮时,我想从上面的 GridView 中获取 CountryName
的值。
正如@AlexKurryashev 上面的评论,您必须 check/find 从 GridView 的 EditTemplate
模式进行控制:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if its not a header or footer row
if (e.Row.RowType == DataControlRowType.DataRow)
{
// check if its in a EditTemplate
if (e.Row.RowState == DataControlRowState.Edit)
{
TextBox txt = e.Row.FindControl("TextBox1") as TextBox;
string name = txt.Text;
}
}
}
或
您可以使用 OnRowCommand
事件从 select 按钮点击中获取值,如下所示:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
// get row where clicked
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Label txt = row.FindControl("Label1") as Label;
string name = txt.Text;
}
}
谢谢两位!我能够从 "ItemTemplate" 获取数据。但这次我使用了不同的事件。
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
{
Label txt = GridView1.SelectedRow.FindControl("Label1") as Label;
string name = txt.Text;
Label2.Text = name;
Session["Name"] = name;
Response.Redirect("check.aspx");
}
}
我能够使用此代码从 Item HeaderTemplate 获取数据。
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
{
GridViewRow headerrow = GridView1.HeaderRow;
DropDownList ddlId = (DropDownList)headerrow.Cells[0].Controls[1].FindControl("ddlId ");
string headerid = ddlId.SelectedValue;
}
}
我正在尝试使用 FindControl
从我的 GridView 中获取 selected 行的文本值,但 FindControl
始终 returns 为 NULL。
.ASPX代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CID" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CID" HeaderText="CID" InsertVisible="False" ReadOnly="True" SortExpression="CID" />
<asp:BoundField DataField="CountryID" HeaderText="CountryID" SortExpression="CountryID" />
<asp:TemplateField HeaderText="CountryName" SortExpression="CountryName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CountryName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = e.Row.FindControl("TextBox1") as TextBox;
string name = txt.Text; // returns as NULL
}
}
任何人都可以指出我在这里做错了什么或者有任何其他方法可以做到这一点吗?当单击 select 按钮时,我想从上面的 GridView 中获取 CountryName
的值。
正如@AlexKurryashev 上面的评论,您必须 check/find 从 GridView 的 EditTemplate
模式进行控制:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if its not a header or footer row
if (e.Row.RowType == DataControlRowType.DataRow)
{
// check if its in a EditTemplate
if (e.Row.RowState == DataControlRowState.Edit)
{
TextBox txt = e.Row.FindControl("TextBox1") as TextBox;
string name = txt.Text;
}
}
}
或
您可以使用 OnRowCommand
事件从 select 按钮点击中获取值,如下所示:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
// get row where clicked
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Label txt = row.FindControl("Label1") as Label;
string name = txt.Text;
}
}
谢谢两位!我能够从 "ItemTemplate" 获取数据。但这次我使用了不同的事件。
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
{
Label txt = GridView1.SelectedRow.FindControl("Label1") as Label;
string name = txt.Text;
Label2.Text = name;
Session["Name"] = name;
Response.Redirect("check.aspx");
}
}
我能够使用此代码从 Item HeaderTemplate 获取数据。
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
{
GridViewRow headerrow = GridView1.HeaderRow;
DropDownList ddlId = (DropDownList)headerrow.Cells[0].Controls[1].FindControl("ddlId ");
string headerid = ddlId.SelectedValue;
}
}