简体   繁体   中英

store and retrieve data from hidden field from gridview control

I have a Gridview control with multiple checkboxes in it.

Gridview与复选框作为我的设计页面

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#.

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.

在此输入图像描述

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!!

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