我正在Asp.net工作并陷入困境。 我有一个带有模板字段的gridview.Gridview有3列,我想将两列的值传递给服务器端。
场景 - 单击lnkRemove(LinkButton),应传递GUID(在命令争论中传递)以及 SEQ_NBR 列的值(传递此值)价值对我来说是个问题)
我正在尝试隐藏字段,但如何在服务器端代码中获取hiiden字段值(即Deletedata
)。
代码
<asp:GridView>
<Columns>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "SEQ_NBR">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("SEQ_NBR") %>' />
<asp:Label ID="SEQ_NBR" runat="server"
Text='<%# Eval("SEQ_NBR")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="SEQ_NBR" runat="server" ReadOnly="true" Text="Auto generated"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server"
CommandArgument = '<%# Eval("GUID")%>'
OnClientClick = "return confirm('Do you want to delete?')"
Text = "Delete" OnClick = "Deletedata"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我的服务器端代码是:
protected void Deletedata(object sender, EventArgs e)
{
//I want Hiddden field value here;I tried below code but not working.Any suggestion .
chkSelect = GridView.Controls[0].Controls[0].FindControl("HiddenField1");
}
答案 0 :(得分:0)
不要使用
GridView.Controls[0].Controls[0].FindControl("HiddenField1");
但在GridViewRow
上,因为那是NamingContainer
:
GridView.Rows[0].FindControl("HiddenField1");
但在这种情况下,您希望从LinkButton的点击事件中找到隐藏字段。因此使用以下方法。 LinkButton&#39; NamingContainer
是GridViewRow
:
protected void Deletedata(object sender, EventArgs e)
{
LinkButton lnkRemove = (LinkButton) sender;
GridViewRow row = (GridViewRow) lnkRemove.NamingContainer;
HiddenField hf = (HiddenField) row.FindControl("HiddenField1");
string seqNbr = hf.Value; // voilà
}
答案 1 :(得分:0)
试试这个
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
GridView.SelectedIndex = row.RowIndex;
var HiddenField= GridView.Rows[gridMain.SelectedIndex].FindControl("HiddenField1") as HtmlInputHidden;
if (HiddenField!= null)
{
sting strValue = HiddenField.Value;
}