Recorrer las casillas de verificación de GridView

I am trying to loop through all the checkboxes and get their Value and if they are checked or not and save the checked/unchecked into database based on the Value which is going to contains the ID of the record. I am trying to do this through asp.net any suggestions?

Aquí está el código:

            <Columns>
                <asp:TemplateField HeaderText="Application" ItemStyle-Width="25%">
                    <ItemTemplate>                        
                        <%#Eval("App")%>
                    </ItemTemplate>                    
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edit" ItemStyle-Width="25%">
                    <ItemTemplate>
                        <input name="chkEdit" runat="server" value='<%# Eval("AppID") %>' class="checkbox" type="checkbox" checked='<%# Eval("Edit") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete" ItemStyle-Width="25%">
                    <ItemTemplate>                        
                        <input name="chkDelete" runat="server" value='<%# Eval("AppID") %>' class="checkbox" type="checkbox" checked='<%# Eval("Delete") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="View" ItemStyle-Width="25%">
                    <ItemTemplate>                        
                        <input name="chkView" runat="server" value='<%# Eval("AppID") %>' class="checkbox" type="checkbox" checked='<%# Eval("View") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>  

preguntado el 09 de marzo de 12 a las 22:03

@DeviantSeev I tried looping through Controls for checkbox but that did not work... also tried using Javascript where I can get all of the checkboxes but would rather do all the work in asp.net. I want to see if there is a way in asp.net to get all the Values and Checked/Unchecked other than javascript. -

Once to user clicks "Save" button I would need to go through all the checkboxes and save their current state into the table. -

2 Respuestas

string[] controls = new string[] {"chkEdit","chkDelete","chkView"};

foreach (GridViewRow r in YourGridView.Rows)
{
   foreach(string cnt in controls){
     int value =  Convert.ToInt32(((System.Web.UI.HtmlControls.HtmlInputCheckBox)r.FindControl(cnt)).Value);
     bool isChecked = ((System.Web.UI.HtmlControls.HtmlInputCheckBox)r.FindControl(cnt)).Checked;
     //now you have id and checked/unchecked. use your query to save it to database
   }
}

respondido 09 mar '12, 22:03

Thanks much for reply, this was something I was looking for. Is there a way to get the Checkbox dynamically other than Hard coding it? Is there a possibility to loop through Html Checkboxes and assign them to the FindControl? - Nick Manojlovic

in onclick event do something like this.

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

        //do something
     }
 }

respondido 09 mar '12, 22:03

Hey thank for reply. I have already dome something like this but behind the checkbox there is a value of an database ID how to I get through control: value='<%# Eval("AppID") %>' - Nick Manojlovic

Also is there a way of Dynamically finding the controls rather than Hard Coding checkbox name? - Nick Manojlovic

html input tag value can be retrieve by something like this. request.form('checkboxID'). can i ask you why you are using html tag and not asp.net checkbox? - AJP

Thanks. Yeah The reason I am using a html tag is because I found it useful to store a ID of a record into a value field. Initially what happens is I bind the checkboxes with the data in database and a user can change that data and when they click save it updates the original value thats in database. I am open to suggestions to do it differently? - Nick Manojlovic

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