• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

Collect current row values with Checkboxes click

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
m-jansen
Asked:
m-jansen
  • 17
  • 10
  • +2
1 Solution
 
ZvonkoSystems architectCommented:
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
 
m-jansenAuthor Commented:
>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
 
orbulatCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
m-jansenAuthor Commented:
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
 
m-jansenAuthor Commented:
Right now I have just pasted it in the code but no alert comes up when I click a checkbox.
0
 
m-jansenAuthor Commented:
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
 
m-jansenAuthor Commented:
I use VS2005 and ASP.NET 2.x
0
 
Hamed ZaghaghiCommented:
use

e.Row.Cells[0].ClientId

instead of
e.Row.DataItemIndex
0
 
m-jansenAuthor Commented:


    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
 
m-jansenAuthor Commented:
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
 
Hamed ZaghaghiCommented:
no this is not the problem, because for each row in grdiview you generate a new script code by using its client id,
0
 
Hamed ZaghaghiCommented:
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
 
Hamed ZaghaghiCommented:
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
 
m-jansenAuthor Commented:
<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
 
m-jansenAuthor Commented:
yahooooo!! I figured out by my self

e.Row.Cells[0].Text

gives me the value
0
 
Hamed ZaghaghiCommented:
do you want use javascript to get the values?
0
 
m-jansenAuthor Commented:
I don't have to. Is there another way?
0
 
Hamed ZaghaghiCommented:
if you want to use javascript you don't use e.Row.Cells[0].Text
0
 
m-jansenAuthor Commented:
I see there is something called OnCheckedChanged
0
 
m-jansenAuthor Commented:
> 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
 
Hamed ZaghaghiCommented:
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
 
m-jansenAuthor Commented:
>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
 
Hamed ZaghaghiCommented:
you should pass all checkbox clientids in one row to the java script function
0
 
m-jansenAuthor Commented:
and then do what?
0
 
Hamed ZaghaghiCommented:
then in java script function you have all client ids and can get all values, and process they and store in another control
0
 
m-jansenAuthor Commented:
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
 
Hamed ZaghaghiCommented:
i think idxid.value
0
 
m-jansenAuthor Commented:
idxid.value gives me undefined
0
 
m-jansenAuthor Commented:
Feel free to tell me, but I've found another solution to my problem.
0
 
NetminderCommented:
Closed, 150 points refunded.
Netminder
Site Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 17
  • 10
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now