简体   繁体   English

选中CheckBox时如何获取GridView中HiddenField的值?

[英]How to obtain the value of HiddenField in a GridView when a CheckBox is checked?

I have a GridView which has multiple rows, on each row I have a CheckBox and a HiddenField. 我有一个具有多行的GridView,每行上都有一个CheckBox和一个HiddenField。 On button click I want to check if the CheckBox is checked and if it is I want to take the value of the HiddenField for that row. 在按钮上单击,我想检查CheckBox是否被选中,是否要获取该行的HiddenField的值。 Each HiddenField on each row has a different value. 每行上的每个HiddenField都有一个不同的值。 User could check multiple CheckBoxes so I need to be able to pull the value of each HiddenField. 用户可以检查多个CheckBoxes,所以我需要能够提取每个HiddenField的值。

Any help will be really appreciate it. 任何帮助将不胜感激。

Thank you 谢谢

Loop through each row in the grid, check if the checkbox is checked and if it is, grab the value of the hidden field. 循环浏览网格中的每一行,检查是否已选中该复选框,如果已选中,则获取隐藏字段的值。

foreach (GridViewRow row in grdView.Rows)
{
    if((row.FindControl("chkBoxId") as CheckBox).Checked)
    {
        string hiddenFieldValue = (row.FindControl("hiddenFieldId") as HiddenField).Value;
    }
}

Where chkBoxId is the ID property of your checkbox on the page and hiddenFieldId is the ID of the hiddenfield control on your page. 其中chkBoxId是页面上复选框的ID属性,hiddenFieldId是页面上hiddenfield控件的ID。

You can use a code like this: 您可以使用如下代码:

protected void BtnMybutton_click( Object sender, EventArgs e)
{
    Button Mybutton = (Button) sender;
    GridViewRow row = (GridViewRow) MyButton.NamingContainer;
    CheckBox ChkTest = (CheckBox) row.FindControl("ChkTest");
    HidenFiekd HdfValue = (HidenField) row.FindControl("HdfValue");
    if(ChkTest.Checked)
    {
        Console.WriteLine(HdfValues.Value);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM