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
-
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 valuesIshika– Ishika2011-04-01 04:54:15 +00:00Commented Apr 1, 2011 at 4:54
-
you just take value of checked checkboes and create the string of id, i think will work for youPranay Rana– Pranay Rana2011-04-01 06:57:06 +00:00Commented 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 meIshika– Ishika2011-04-01 07:21:39 +00:00Commented 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); }Ishika– Ishika2011-04-01 07:31:11 +00:00Commented Apr 1, 2011 at 7:31
-
i got the problem i didnt called ispostback method in pageload. now its working. Thank you.Ishika– Ishika2011-04-01 09:36:58 +00:00Commented Apr 1, 2011 at 9:36
Add a comment
|
4 Answers
foreach(Gridviewrow gvr in Gridview1.Rows)
{
if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
{
int uPrimaryid= gvr.cells["uPrimaryID"];
}
}
2 Comments
TBohnen.jnr
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?
Ishika
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); } }
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
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
Kardo
what's GetErhebungModulGridView()?
msgrockclimber
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
<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