简体   繁体   中英

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. 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. Each HiddenField on each row has a different value. User could check multiple CheckBoxes so I need to be able to pull the value of each 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.

Possible duplicates.

How to get values of CheckBoxes inside a gridview that are checked using asp .net

Get the id of selected checkboxes in gridview (Asp.net) c#

How to get the value in the gridview which the checkbox is checked?

One of the answer in above links :

foreach(Gridviewrow gvr in Gridview1.Rows)
{
 if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
 {

   //Get hidden field value here.
 }
}

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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