almacenar y recuperar datos del campo oculto del control gridview

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

preguntado el 27 de noviembre de 13 a las 05:11

The answer will certainly need to live on the JS side of the fence, but why store the values in a hidden field rather than an appropriately scoped JS variable? -

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., and what if more than one row's check box is checked, which values get saved and displayed on the redirect page? -

@KarlAnderson If multiple check boxes will be selected, then the corresponding row values will be stored into database and it'll redirect to the same page. Because, I am redirecting to only one page on the click event of "Assign" button. -

4 Respuestas

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

respondido 27 nov., 13:06

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.

respondido 27 nov., 13:06

Puedes probar este...

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

respondido 27 nov., 13:06

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.

Saludos!

respondido 27 nov., 13:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.