?
Solved

datagrid

Posted on 2006-11-02
19
Medium Priority
?
2,745 Views
Last Modified: 2010-05-18
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.FindControl("EmpId");
            if (chkBx != null && chkBx.Checked)
            {
               Response.Write(chkBx.Value + "<br>");
            }
        }
0
Comment
Question by:service2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
19 Comments
 

Author Comment

by:service2001
ID: 17861039

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
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17861121
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.
0
 

Author Comment

by:service2001
ID: 17861258
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17861356
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.

0
 

Author Comment

by:service2001
ID: 17861506
thats my old code i was using and i a have <asp:checkbox......

 foreach (DataGridItem item in dg.Items)
        {
            CheckBox box = (CheckBox)item["TemplateColumn"].FindControl("checkBox1");
            if (box.Checked == true)
            {
                //get the checked orderid
                object _OrderID = dataItem["un_OrderID"].Text;
            }
           
        }

 still i'm getting only check box from the current page.
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17861566
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].Controls)
            if (c.GetType() == TypeOf(CheckBox))
            {
            CheckBox box = (CheckBox)c;
            if (box.Checked == true)
            {
                //get the checked orderid
                object _OrderID = dataItem["un_OrderID"].Text;
            }
            }
        }

--Nauman.
0
 

Author Comment

by:service2001
ID: 17861831
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"].Text;
                     }
                 }
         }
}

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="AccessDataSource1" OnPageIndexChanged="dg_PageIndexChanged" PageSize="5"
   >
            <Columns>
                <asp:TemplateColumn  HeaderText="ProductName" SortExpression="ProductName">
                    <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" />&nbsp; -->
    <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>
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17862146
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.
0
 

Author Comment

by:service2001
ID: 17862216
>> if (c.GetType() == typeOf("HtmlInputCheckBox"))

error:
Error      1      The name 'typeOf' does not exist in the current context       
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17862515
woops $$ typeOf should be typeof

--Nauman.
0
 

Author Comment

by:service2001
ID: 17862615
i realized after i post but still getting the error
 
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17862755
What is the error?

--Nauman.
0
 

Author Comment

by:service2001
ID: 17864063
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;
                     }
                 }
         }


0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17866092
              if (c.GetType() == typeof(HtmlInputCheckBox))

Bob
 
0
 

Author Comment

by:service2001
ID: 17866229
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 :(
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 18461905
PAQed with points refunded (125)

DarthMod
Community Support Moderator
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question