Solved

Collect current row values with Checkboxes click

Posted on 2006-06-28
31
460 Views
Last Modified: 2008-01-09
Hello I have a Gridview with a checkbox column and some other columns with numbers. When someone check the checkbox in the current row the one value of the column named TilleggIndex must be pushed to an arraylist. Please tell if there is a better way to do it.

The goal is to use the collected checked/unchecked answer in a summary later. Right now I know how to run a javascript when the checkbox is checked.

First I need help to find the value of the current column named TilleggIndex when the checkbox at this row is clicked.

This is my code:

<script type="text/javascript">  
    function saveSelection(obj)
    {        
        var checkBoxID = obj.id;
        alert(checkBoxID);
    }    
</script>

                            <asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource2" Width="222px"
                                AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
                                <Columns>
                                    <asp:BoundField DataField="TilleggIndex" HeaderText="TilleggIndex" SortExpression="TilleggIndex" />
                                    <asp:BoundField DataField="tillegg" HeaderText="Tillegg" SortExpression="tillegg" />
                                    <asp:TemplateField>
                                        <HeaderTemplate>
                                            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="Velg" runat="server" onclick="saveSelection(this)"/>
                                        </ItemTemplate>
                                    </asp:TemplateField>                                    
                                </Columns>
                                <RowStyle Height="23px" BackColor="#E3EAEB"></RowStyle>
                                <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                                <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                                <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                                <AlternatingRowStyle BackColor="White" />
                                <EditRowStyle BackColor="#7C6F57" />
                                <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                            </asp:GridView>
0
Comment
Question by:m-jansen
  • 17
  • 10
  • +2
31 Comments
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
The ID alone does not help because all CheckBoxes do have the same id.
Also storing checkbox checking on every click and unclick does not make sense.
I would recomend to use the <form onSubmit="getCheckboxSates(this)"> for storing the checkboxs state in a hidden input field value for use on server side.

 The function would work like this:
<script type="text/javascript">  
    function getCheckboxSates(theForm){      
       var cBox = theForm.Velg;
       var cState = "";
       for(var i=0;i<cBox.length;i++){
          cState += ","+(cBox[i].checked)?"1":"0";
       }
       cState = cState.substr(1);
       alert(cSate);
       theForm.checkBoxState.value=cSate;
    }    
</script>

You need also a hidden input field:
<form onSubmit="getCheckboxSates(this)">
<input type="hidden" name="checkBoxState">

And your checkboxes must NOT run on server side:
<asp:CheckBox ID="Velg" value="someValue" />

And at the end, why do you not have unique ids for every checkbox? Then you need no state collector. Even with same names, don't you get an Array for that input elements on server side?

0
 

Author Comment

by:m-jansen
Comment Utility
>The ID alone does not help because all CheckBoxes do have the same id.
>Unique checkboxes is makes it easier to spot out what I have clicked.
When run at the server all checkboxes get unique ID's like.... ID = Wizard1_GridView1_ctl02_Velg, ID = Wizard1_GridView1_ctl03, ID = _VelgWizard1_GridView1_ctl04_Velg
>don't you get an Array for that input elements on server side?
It's very important that I can get all values in a row when clicking the checkbox. These values are saved in this array I guess on server or client. How to get that array or source?
0
 
LVL 10

Expert Comment

by:orbulat
Comment Utility
maybe u can do sth. like this -->

1. <asp:CheckBox ID="Velg" runat="server" onclick="saveSelection(this)"/>   to
    <asp:CheckBox ID="Velg" runat="server" />  

2.  ItemDataBound event in your code behind, do sth. like this

     CheckBox Velg = (CheckBox)e.Item.FindControls("Velg");
     Velg.Attributes.Add("onClick","saveSelection(this,'"+e.Item.Cells[0].ClientID+"');"); //e.Item.Cells[0].ClientID will refer to your bound column

3. update your javascript function
   
   function saveSelection(obj, idxId)
    {        
        var checkBoxID = obj.id;
        alert(checkBoxID + idxId);
    }    
 
0
 

Author Comment

by:m-jansen
Comment Utility
Thanks orbulat.

I have some problems with creating the

    void Item_Bound(Object sender, DataGridItemEventArgs e)
    {
        CheckBox Velg = (CheckBox)e.Item.FindControl("Velg");
        Velg.Attributes.Add("onClick", "saveSelection(this,'" + e.Item.Cells[0].ClientID + "');"); //e.Item.Cells[0].ClientID will refer to your bound column
    }

I remeber when I created the Page_Load method I had to double click in the page in design view in order to get it to work. Is it somehting simlar with how to create the Item_Bound method?
0
 

Author Comment

by:m-jansen
Comment Utility
Right now I have just pasted it in the code but no alert comes up when I click a checkbox.
0
 

Author Comment

by:m-jansen
Comment Utility
I got a solution how to make the Item_Bound event. Right here.
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21903157.html
So this is my guess how my code would look like

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        CheckBox Velg = (CheckBox)e.Row.FindControl("Velg");
        Velg.Attributes.Add("onclick", "saveSelection(this,'" + e.Row.DataItemIndex + "');");  // Here I get a NullReferenceExeption was unhandled by code
    }

What do I have to modify?

0
 

Author Comment

by:m-jansen
Comment Utility
I use VS2005 and ASP.NET 2.x
0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
use

e.Row.Cells[0].ClientId

instead of
e.Row.DataItemIndex
0
 

Author Comment

by:m-jansen
Comment Utility


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            CheckBox Velg = (CheckBox)e.Row.FindControl("Velg");
            Velg.Attributes.Add("onclick", "saveSelection(this,'" + e.Row.Cells[0].ClientID + "');"); // // StillI get a NullReferenceExeption was unhandled by code

        }
        catch (Exception ex)        
        {
            Label2.Text = ex.Message; // Object reference not set to an instance of an object.
        }        
    }
0
 

Author Comment

by:m-jansen
Comment Utility
When run at the server all checkboxes get unique ID's like.... ID = Wizard1_GridView1_ctl02_Velg, ID = Wizard1_GridView1_ctl03, ID = _VelgWizard1_GridView1_ctl04_Velg

Maybe that is a problem?
0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
no this is not the problem, because for each row in grdiview you generate a new script code by using its client id,
0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
Ooooops,

in RowDataBound you must check the type of row binding,
so you must insert this:

if( e.Row.RowType == DataControlRowType.DataRow)
{

   paste your code here

}

0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
RowDataBound event call for all rows including header and footer, and in header or footer there is no checkboxes that you can change its attributes, and get that error
0
 

Author Comment

by:m-jansen
Comment Utility
<script type="text/javascript">
   function saveSelection(obj, idxId)
    {        
        alert("obj: "+obj + ", idexId: " + idxId);
    }  

</script>

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox Velg = (CheckBox)e.Row.FindControl("Velg");
            Velg.Attributes.Add("onclick", "saveSelection(this,'" + e.Row.Cells[0].ClientID + "');");
        }
    }

Great now things start to work. With this javascript and c# code I get

obj: obj [object], idexId: Wizard1_GridView1_ctl02_ctl00

in the alert box. So, how to get the value of the current row in column TilleggIndex?
0
 

Author Comment

by:m-jansen
Comment Utility
yahooooo!! I figured out by my self

e.Row.Cells[0].Text

gives me the value
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
do you want use javascript to get the values?
0
 

Author Comment

by:m-jansen
Comment Utility
I don't have to. Is there another way?
0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
if you want to use javascript you don't use e.Row.Cells[0].Text
0
 

Author Comment

by:m-jansen
Comment Utility
I see there is something called OnCheckedChanged
0
 

Author Comment

by:m-jansen
Comment Utility
> if you want to use javascript you don't use e.Row.Cells[0].Text
why not?
Velg.Attributes.Add("onclick", "saveSelection(this,'" + e.Row.Cells[0].Text + "');");
this gave me the value of column 0 and the row I clicked
0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
because you want the text of that cell in client not in the server side, this code cause usage of server side value instead of client side
0
 

Author Comment

by:m-jansen
Comment Utility
>because you want the text of that cell in client not in the server side, this code cause usage of server side value instead of client side
That sound good. I think it would be a good thing in my case to save it on on the client. If the server crash or something the user still have what they have selected so far.
How to do the same thing in javascript?
0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
you should pass all checkbox clientids in one row to the java script function
0
 

Author Comment

by:m-jansen
Comment Utility
and then do what?
0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
then in java script function you have all client ids and can get all values, and process they and store in another control
0
 

Author Comment

by:m-jansen
Comment Utility
how to get the values then in javascript?

idxId.something; // I guess

I don't see what's supported with javascript in VS2005. Do you know about a better method to make the javascrips than using VS2005?
0
 
LVL 9

Expert Comment

by:zaghaghi
Comment Utility
i think idxid.value
0
 

Author Comment

by:m-jansen
Comment Utility
idxid.value gives me undefined
0
 

Author Comment

by:m-jansen
Comment Utility
Feel free to tell me, but I've found another solution to my problem.
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
Comment Utility
Closed, 150 points refunded.
Netminder
Site Admin
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now