Link to home
Start Free TrialLog in
Avatar of m-jansen
m-jansen

asked on

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>
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

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?

Avatar of m-jansen
m-jansen

ASKER

>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?
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);
    }    
 
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?
Right now I have just pasted it in the code but no alert comes up when I click a checkbox.
I got a solution how to make the Item_Bound event. Right here.
https://www.experts-exchange.com/questions/21903157/Item-Bound-won't-run.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?

I use VS2005 and ASP.NET 2.x
use

e.Row.Cells[0].ClientId

instead of
e.Row.DataItemIndex


    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.
        }        
    }
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?
no this is not the problem, because for each row in grdiview you generate a new script code by using its client id,
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

}

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
<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?
yahooooo!! I figured out by my self

e.Row.Cells[0].Text

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial