4

I have two columns one for id & other for checkboxes. i have taken checkboxes inside the gridview. i wanted to see the checked values inside the gridview , If checkboxes are checked then i want those values i.e id Asp.net

5
  • i didnt get answer yet i tried this earlier also but i am getting all values from database as i wanted only checked values. Suppose i checked only 3 values then i want only that 3 values not all the values Commented Apr 1, 2011 at 4:54
  • you just take value of checked checkboes and create the string of id, i think will work for you Commented Apr 1, 2011 at 6:57
  • i write the code for that but not getting values. here when i slect that checkboxes then also it gives false values. please help me Commented Apr 1, 2011 at 7:21
  • I am using following code, tell me its right or not : foreach (GridViewRow row in reviewgrid.Rows) { { CheckBox cb = (CheckBox)row.FindControl("chkmark"); if (cb.Checked) { string markid = reviewgrid.DataKeys[row.RowIndex].Value.ToString(); Response.Write(markid); } Commented Apr 1, 2011 at 7:31
  • i got the problem i didnt called ispostback method in pageload. now its working. Thank you. Commented Apr 1, 2011 at 9:36

4 Answers 4

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

   int uPrimaryid= gvr.cells["uPrimaryID"];
 }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think his problem is that with a normal asp:CheckBoxField you can't actually set it's ID, so you have to convert it to a template field?
i used the code but its not giving checked values ie id :foreach (GridViewRow row in reviewgrid.Rows) { CheckBox cb = (CheckBox)row.FindControl("chkmark"); if (cb.Checked == true) { int markid = Convert.ToInt32(reviewgrid.DataKeys[row.RowIndex].Value); } }
1

What you will have to do is use a template field:

<asp:TemplateField HeaderText="Field">
                            <ItemTemplate>
                                <div style="display: block">
                                    <asp:Checkbox Checked='<%# DataBinder.Eval(Container.DataItem,"Field") %>'
                                        runat="server" ID="chkField"></asp:Label>
                                </div>
                            </ItemTemplate>
                        </asp:TemplateField>

Then you can do:

foreach (DataGridRow dr in DataGrid1.Rows)
{
    ((CheckBox)gvr.FindControl("chkField")).Checked
}

to see if it's checked

Comments

1

in your aspx you have the following

<asp:GridView ID="gridViewID" runat="server" DataKeyNames="DataKey1,DataKey2,DataKey3" >
   <Columns>
      <asp:TemplateField HeaderText="selected">
         <ItemTemplate>
             <asp:CheckBox ID="checkBoxID" runat="server" 
                 Checked='<%# Bind("Selected") %>'
                 OnCheckedChanged="checkBoxID_CheckedChanged" 
                 AccessKey='<%# Container.DataItemIndex %>'  
                 AutoPostBack="True" />
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>

Then in your event handler you do something similar to this:

protected void checkBoxID_CheckedChanged(object sender, EventArgs e)
{
    var checkbox = (CheckBox)sender;
    var rowIndex = Convert.ToInt32(checkbox.AccessKey);
    var gridView = GetErhebungModulGridView();
    var dataKey = gridView.DataKeys[rowIndex];
    if (dataKey != null)
    {
        var dataKey1 = dataKey["DataKey1"];
        var dataKey2 = dataKey["DataKey2"];
        var dataKey3 = dataKey["DataKey3"];

        //Do something with the variables keys above

    }
}

2 Comments

what's GetErhebungModulGridView()?
GetErhebungModulGridView() is simply a FindControl("id") call that we've encapsulated in a method because it is used in multiple locations - sorry for the confusion
0
<asp:gridview id="gv" runat="server">
    <columns>
    <asP:TemplateField>
       <Asp:checkbox id="chk" runat="server" />
       <Asp:literal id="ltlID" runat="server" visible="false" text='<%#eval("ID")%>' />
    </asp:TemplateField>
    </columns>
</asp:gridview>

For each row as gridviewrow in gv.rows
    if ctype(row.findcontrol("chk"),checkbox).checked then
       Dim _ID as integer = ctype(row.findcontrol("ltlID"),literal).text
    end if
next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.