service2001
asked on
datagrid
i have a datagrid and in that i have checkbox, everything is working fine except i found one thing that really bothering me.
when i check the checkbox like (1 or 2 or 5) and i click on save button then it gives me the value of whatever i have checked which is great but you will be only able to get the value of a current page not the next page
what i mean by that.
let say page one, i have check 3 checkbox and i go to page two and check 2 more checkbox so total is 5 correct, so when i hit the save button it give me the values for 2 not total 5 ? because it look for the current page only.
so my question is: how do i loop through all the RadGrid pages and get whatever is checked?
i'm using something like this now...
foreach (DataGridItem item in dg.Items)
{
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)item.Fi ndControl( "EmpId");
if (chkBx != null && chkBx.Checked)
{
Response.Write(chkBx.Value + "<br>");
}
}
when i check the checkbox like (1 or 2 or 5) and i click on save button then it gives me the value of whatever i have checked which is great but you will be only able to get the value of a current page not the next page
what i mean by that.
let say page one, i have check 3 checkbox and i go to page two and check 2 more checkbox so total is 5 correct, so when i hit the save button it give me the values for 2 not total 5 ? because it look for the current page only.
so my question is: how do i loop through all the RadGrid pages and get whatever is checked?
i'm using something like this now...
foreach (DataGridItem item in dg.Items)
{
HtmlInputCheckBox chkBx = (HtmlInputCheckBox)item.Fi
if (chkBx != null && chkBx.Checked)
{
Response.Write(chkBx.Value
}
}
You will need to write code so that the values for the checkbox is retained during postbacks. Take a look at the following article:
Persisting checked state across pages in a DataGrid
http://www.codeproject.com/aspnet/CheckStatePersisting.asp
--Nauman.
Persisting checked state across pages in a DataGrid
http://www.codeproject.com/aspnet/CheckStatePersisting.asp
--Nauman.
ASKER
i have read that and i guess my question is more into grabbing the check-box checked.
have a data-grid which has many pages (10 rows per page) and when I loop through the rows, I only get 10 rows (the rows that are currently showed in the current page) How can I get the total number of rows in all the pages?
have a data-grid which has many pages (10 rows per page) and when I loop through the rows, I only get 10 rows (the rows that are currently showed in the current page) How can I get the total number of rows in all the pages?
just noticed: you are using HtmlInputCheckbox rather than using <asp:CheckBox>. For HTML controls being used in dg, you will need to have runat=server attribute in order to retrieve the value on runtime. I will recommend you change Html checkbox to asp:CheckBox controls. See if it works.
--Nauman.
--Nauman.
ASKER
thats my old code i was using and i a have <asp:checkbox......
foreach (DataGridItem item in dg.Items)
{
CheckBox box = (CheckBox)item["TemplateCo lumn"].Fin dControl(" checkBox1" );
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = dataItem["un_OrderID"].Tex t;
}
}
still i'm getting only check box from the current page.
foreach (DataGridItem item in dg.Items)
{
CheckBox box = (CheckBox)item["TemplateCo
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = dataItem["un_OrderID"].Tex
}
}
still i'm getting only check box from the current page.
Make sure that you have asp:checkbox in datagrid column and try the following code:
foreach (DataGridItem item in dg.Items)
{
foreach(Control c in item.Cells[CHECKBOX_COLUMN _INDEX].Co ntrols)
if (c.GetType() == TypeOf(CheckBox))
{
CheckBox box = (CheckBox)c;
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = dataItem["un_OrderID"].Tex t;
}
}
}
--Nauman.
foreach (DataGridItem item in dg.Items)
{
foreach(Control c in item.Cells[CHECKBOX_COLUMN
if (c.GetType() == TypeOf(CheckBox))
{
CheckBox box = (CheckBox)c;
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = dataItem["un_OrderID"].Tex
}
}
}
--Nauman.
ASKER
salam ahmed,
I'm getting few errors now:
public void View_Selected(object sender, EventArgs e)
{
foreach (DataGridItem item in dg.Items)
{
foreach (Control c in item.Cells[0].Controls)
if (c.GetType() == TypeOf(CheckBox))
{
CheckBox box = (CheckBox)c;
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = dataItem["un_OrderID"].Tex t;
}
}
}
}
errors://
Error 1 The name 'TypeOf' does not exist in the current context
Error 2 'System.Web.UI.WebControls .CheckBox' is a 'type' but is used like a 'variable'
Error 3 The name 'dataItem' does not exist in the current contet
<asp:DataGrid ID="dg" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="AccessDataSo urce1" OnPageIndexChanged="dg_Pag eIndexChan ged" PageSize="5"
>
<Columns>
<asp:TemplateColumn HeaderText="ProductName" SortExpression="ProductNam e">
<HeaderTemplate>
<input type="checkbox" id="checkAll" onclick="CheckAll(this);" runat="server" name="checkAll">
</HeaderTemplate>
<ItemTemplate>
<!-- <input type="checkbox" runat="server" id="EmpId1" value='<%# Bind("Discontinued") %>' checked=<%# DataBinder.Eval(Container. DataItem, "Discontinued") %> onclick="CheckChanged();" unselectable=off name="EmpId" /> -->
<asp:CheckBox runat="server" id="EmpId" onclick="CheckChanged();" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></td>
</tr>
<tr>
<td style="width: 76px">
<asp:Button ID="selBtn" OnClick="View_Selected" runat="server" Text="Show Selection" /></td>
I'm getting few errors now:
public void View_Selected(object sender, EventArgs e)
{
foreach (DataGridItem item in dg.Items)
{
foreach (Control c in item.Cells[0].Controls)
if (c.GetType() == TypeOf(CheckBox))
{
CheckBox box = (CheckBox)c;
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = dataItem["un_OrderID"].Tex
}
}
}
}
errors://
Error 1 The name 'TypeOf' does not exist in the current context
Error 2 'System.Web.UI.WebControls
Error 3 The name 'dataItem' does not exist in the current contet
<asp:DataGrid ID="dg" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="AccessDataSo
>
<Columns>
<asp:TemplateColumn HeaderText="ProductName" SortExpression="ProductNam
<HeaderTemplate>
<input type="checkbox" id="checkAll" onclick="CheckAll(this);" runat="server" name="checkAll">
</HeaderTemplate>
<ItemTemplate>
<!-- <input type="checkbox" runat="server" id="EmpId1" value='<%# Bind("Discontinued") %>' checked=<%# DataBinder.Eval(Container.
<asp:CheckBox runat="server" id="EmpId" onclick="CheckChanged();" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></td>
</tr>
<tr>
<td style="width: 76px">
<asp:Button ID="selBtn" OnClick="View_Selected" runat="server" Text="Show Selection" /></td>
w/s :)
One change: In datagrid, use <input type=CheckBox runat=server value='<%#DataBinder.Eval( Container. DataItem," un_OrderID ")%>'> instead of asp:CheckBox. This is needed since asp:CheckBox do not have a value attribute. You can add it on runtime but it is time consuming.
foreach (DataGridItem item in dg.Items)
{
foreach (Control c in item.Cells[0].Controls)
if (c.GetType() == typeOf("HtmlInputCheckBox" ))
{
HtmlInputCheckBox box = (HtmlInputCheckBox)c;
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = box.Value;
}
}
}
--Nauman.
One change: In datagrid, use <input type=CheckBox runat=server value='<%#DataBinder.Eval(
foreach (DataGridItem item in dg.Items)
{
foreach (Control c in item.Cells[0].Controls)
if (c.GetType() == typeOf("HtmlInputCheckBox"
{
HtmlInputCheckBox box = (HtmlInputCheckBox)c;
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = box.Value;
}
}
}
--Nauman.
ASKER
>> if (c.GetType() == typeOf("HtmlInputCheckBox" ))
error:
Error 1 The name 'typeOf' does not exist in the current context
error:
Error 1 The name 'typeOf' does not exist in the current context
woops $$ typeOf should be typeof
--Nauman.
--Nauman.
ASKER
i realized after i post but still getting the error
What is the error?
--Nauman.
--Nauman.
ASKER
Error 1 Type expected
Error 2 ;
Error 3 Invalid expression term ')'
this is what the code i have, same exact code you have suggested
foreach (DataGridItem item in dg.Items)
{
foreach (Control c in item.Cells[0].Controls)
if (c.GetType() == typeof("HtmlInputCheckBox" )) <<<<ERROR HERE
{
HtmlInputCheckBox box = (HtmlInputCheckBox)c;
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = box.Value;
}
}
}
Error 2 ;
Error 3 Invalid expression term ')'
this is what the code i have, same exact code you have suggested
foreach (DataGridItem item in dg.Items)
{
foreach (Control c in item.Cells[0].Controls)
if (c.GetType() == typeof("HtmlInputCheckBox"
{
HtmlInputCheckBox box = (HtmlInputCheckBox)c;
if (box.Checked == true)
{
//get the checked orderid
object _OrderID = box.Value;
}
}
}
if (c.GetType() == typeof(HtmlInputCheckBox))
Bob
Bob
ASKER
the error is fixed but again... its reading the checkbox from the current page only and if i go back to previous page then i dont see my checkbox is checked :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
have a datagrid which has many pages (10 rows per page) and when I loop through the rows, I only get 10 rows (the rows that are currently showed in the current page) How can I get the total number of rows in all the pages?
hope i'm clear to my quesiton