Solved

Collect current row values with Checkboxes click

Posted on 2006-06-28
31
462 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
ID: 17007142
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
ID: 17007278
>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
ID: 17007699
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:m-jansen
ID: 17007797
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
ID: 17007810
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
ID: 17008182
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
ID: 17008308
I use VS2005 and ASP.NET 2.x
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17008360
use

e.Row.Cells[0].ClientId

instead of
e.Row.DataItemIndex
0
 

Author Comment

by:m-jansen
ID: 17008417


    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
ID: 17008432
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
ID: 17008483
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
ID: 17008534
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
ID: 17008552
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
ID: 17008736
<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
ID: 17008770
yahooooo!! I figured out by my self

e.Row.Cells[0].Text

gives me the value
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17008783
do you want use javascript to get the values?
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:m-jansen
ID: 17008850
I see there is something called OnCheckedChanged
0
 

Author Comment

by:m-jansen
ID: 17008887
> 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
ID: 17009113
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
ID: 17009149
>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
ID: 17009577
you should pass all checkbox clientids in one row to the java script function
0
 

Author Comment

by:m-jansen
ID: 17009600
and then do what?
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 17009690
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
ID: 17010975
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
ID: 17012311
i think idxid.value
0
 

Author Comment

by:m-jansen
ID: 17012378
idxid.value gives me undefined
0
 

Author Comment

by:m-jansen
ID: 17018988
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
ID: 17157395
Closed, 150 points refunded.
Netminder
Site Admin
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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