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="AccessDataSo urce2" Width="222px"
AutoGenerateColumns="False " CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:BoundField DataField="TilleggIndex" HeaderText="TilleggIndex" SortExpression="TilleggInd ex" />
<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(thi s)"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle Height="23px" BackColor="#E3EAEB"></RowS tyle>
<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>
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="AccessDataSo
AutoGenerateColumns="False
<Columns>
<asp:BoundField DataField="TilleggIndex" HeaderText="TilleggIndex" SortExpression="TilleggInd
<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(thi
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle Height="23px" BackColor="#E3EAEB"></RowS
<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>
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_Ve lg, ID = Wizard1_GridView1_ctl03, ID = _VelgWizard1_GridView1_ctl 04_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?
>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_Ve
>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(thi s)"/> to
<asp:CheckBox ID="Velg" runat="server" />
2. ItemDataBound event in your code behind, do sth. like this
CheckBox Velg = (CheckBox)e.Item.FindContr ols("Velg" );
Velg.Attributes.Add("onCli ck","saveS election(t his,'"+e.I tem.Cells[ 0].ClientI D+"');"); //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);
}
1. <asp:CheckBox ID="Velg" runat="server" onclick="saveSelection(thi
<asp:CheckBox ID="Velg" runat="server" />
2. ItemDataBound event in your code behind, do sth. like this
CheckBox Velg = (CheckBox)e.Item.FindContr
Velg.Attributes.Add("onCli
3. update your javascript function
function saveSelection(obj, idxId)
{
var checkBoxID = obj.id;
alert(checkBoxID + idxId);
}
ASKER
Thanks orbulat.
I have some problems with creating the
void Item_Bound(Object sender, DataGridItemEventArgs e)
{
CheckBox Velg = (CheckBox)e.Item.FindContr ol("Velg") ;
Velg.Attributes.Add("onCli ck", "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?
I have some problems with creating the
void Item_Bound(Object sender, DataGridItemEventArgs e)
{
CheckBox Velg = (CheckBox)e.Item.FindContr
Velg.Attributes.Add("onCli
}
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?
ASKER
Right now I have just pasted it in the code but no alert comes up when I click a checkbox.
ASKER
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(obj ect sender, GridViewRowEventArgs e)
{
CheckBox Velg = (CheckBox)e.Row.FindContro l("Velg");
Velg.Attributes.Add("oncli ck", "saveSelection(this,'" + e.Row.DataItemIndex + "');"); // Here I get a NullReferenceExeption was unhandled by code
}
What do I have to modify?
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(obj
{
CheckBox Velg = (CheckBox)e.Row.FindContro
Velg.Attributes.Add("oncli
}
What do I have to modify?
ASKER
I use VS2005 and ASP.NET 2.x
use
e.Row.Cells[0].ClientId
instead of
e.Row.DataItemIndex
e.Row.Cells[0].ClientId
instead of
e.Row.DataItemIndex
ASKER
protected void GridView1_RowDataBound(obj
{
try
{
CheckBox Velg = (CheckBox)e.Row.FindContro
Velg.Attributes.Add("oncli
}
catch (Exception ex)
{
Label2.Text = ex.Message; // Object reference not set to an instance of an object.
}
}
ASKER
When run at the server all checkboxes get unique ID's like.... ID = Wizard1_GridView1_ctl02_Ve lg, ID = Wizard1_GridView1_ctl03, ID = _VelgWizard1_GridView1_ctl 04_Velg
Maybe that is a problem?
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
}
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
ASKER
<script type="text/javascript">
function saveSelection(obj, idxId)
{
alert("obj: "+obj + ", idexId: " + idxId);
}
</script>
protected void GridView1_RowDataBound(obj ect sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
CheckBox Velg = (CheckBox)e.Row.FindContro l("Velg");
Velg.Attributes.Add("oncli ck", "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_ct l00
in the alert box. So, how to get the value of the current row in column TilleggIndex?
function saveSelection(obj, idxId)
{
alert("obj: "+obj + ", idexId: " + idxId);
}
</script>
protected void GridView1_RowDataBound(obj
{
if (e.Row.RowType == DataControlRowType.DataRow
{
CheckBox Velg = (CheckBox)e.Row.FindContro
Velg.Attributes.Add("oncli
}
}
Great now things start to work. With this javascript and c# code I get
obj: obj [object], idexId: Wizard1_GridView1_ctl02_ct
in the alert box. So, how to get the value of the current row in column TilleggIndex?
ASKER
yahooooo!! I figured out by my self
e.Row.Cells[0].Text
gives me the value
e.Row.Cells[0].Text
gives me the value
do you want use javascript to get the values?
ASKER
I don't have to. Is there another way?
if you want to use javascript you don't use e.Row.Cells[0].Text
ASKER
I see there is something called OnCheckedChanged
ASKER
> if you want to use javascript you don't use e.Row.Cells[0].Text
why not?
Velg.Attributes.Add("oncli ck", "saveSelection(this,'" + e.Row.Cells[0].Text + "');");
this gave me the value of column 0 and the row I clicked
why not?
Velg.Attributes.Add("oncli
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
ASKER
>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?
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
ASKER
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
ASKER
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?
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
ASKER
idxid.value gives me undefined
ASKER
Feel free to tell me, but I've found another solution to my problem.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also storing checkbox checking on every click and unclick does not make sense.
I would recomend to use the <form onSubmit="getCheckboxSates
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":
}
cState = cState.substr(1);
alert(cSate);
theForm.checkBoxState.valu
}
</script>
You need also a hidden input field:
<form onSubmit="getCheckboxSates
<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?