Question

I have a Gridview control with multiple checkboxes in it.

Gridview with checkboxes as my design page

my requirements are: if I select one checkbox, then the values with respect to that column will be store in hidden field and once I click on assign button, the values will be stored into database and also redirect to another page with the hidden field values.

Is it possible to solve this using c# not in Javascript? Because, I am unable to store values into hiddenfield using c#.

Was it helpful?

Solution

I think you should check below URL :

Delete data on gridview checkbox selection

In above link the same example has been implemented to delete records using multiple checkboxes. You can get an idea from that. it will help you a lot.

enter image description here

OTHER TIPS

One way of achieving this is by looping through all the rows on click of assign button to find out which all rows have check boxes checked. And then you can proceed with your logic.

You can try this one...

<asp:gridview runat="server">
    <asp:TemplateField>     
        <ItemTemplate>       
            <asp:CheckBox ID="chkSelItem" runat="server" />     
            <asp:Label ID="lblSelectedItem" value=<%# Eval("Key.Id")) %> 
                       visible="False"/>
        </ItemTemplate> 
    </asp:TemplateField>
</asp:gridview>

In codebehind try this

protected void btnClick_Click(object sender, EventArgs e)
{ 
    foreach (GridViewRow row in gridDepartments.Rows)         
    {             
        CheckBox chkSelItem = (CheckBox)row.FindControl("chkSelItem");
        Label lblSelectedItem= (Label)row.FindControl("lblSelectedItem");

        if (chkSelItem.Checked) 
        {
            int departmentId = int.Parse(lblSelectedItem.Text); 
        }
    }
}

I think there is no use of Hidden Field in this scenario.

You can iterate through the grid, typecast the checkbox, check whether the checkbox is checked or not and then get the value of that row. If there are multiple rows checked then you can add all the values in a list and perform action on click of Assign button.

For taking values to another screen, put the required information in a Session variable.

Cheers!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top