manu009
asked on
Checkbox control inside a repeater control.. how to collect data?? help!
Hello experts;
I am using ASP.net, C#.
On a webpage I list some records in a repeater control (from database).
One of the columns is a checkbox control (On or Off depending on the relevant value pulled from database)
NOW, I need the user to be able to change the state of the checkbox
When he clicks SUBMIT, I want to update all selected records in the database.
NOW how do I find out....
which checkboxes the user selected????
which id they are associated with???????????
I believe in ASP.net there should be a simple solution....
---> I also noticed that, ASP.net changes my checkbox ids to something like repeaterElements_ctl00_Che ckBox1, repeaterElements_ctl01_Che ckBox1 etc...
Any help will be appreciated!!!!!!!!!!!!!!! !!!!!!!!!! !!!!!!!
Thanks a in advance...
I am using ASP.net, C#.
On a webpage I list some records in a repeater control (from database).
One of the columns is a checkbox control (On or Off depending on the relevant value pulled from database)
NOW, I need the user to be able to change the state of the checkbox
When he clicks SUBMIT, I want to update all selected records in the database.
NOW how do I find out....
which checkboxes the user selected????
which id they are associated with???????????
I believe in ASP.net there should be a simple solution....
---> I also noticed that, ASP.net changes my checkbox ids to something like repeaterElements_ctl00_Che
Any help will be appreciated!!!!!!!!!!!!!!!
Thanks a in advance...
ASKER
Could you please post the code (preferably in C#... or even VB) for that..
Here is what I use
<asp:repeater id="repeater1" runat="server">
<ITEMTEMPLATE>
<asp:CheckBox id="chk" runat="server" />
</ITEMTEMPLATE>
</asp:repeater>
Much appreciated....
I am new to this ASP.net :-) cheers
Here is what I use
<asp:repeater id="repeater1" runat="server">
<ITEMTEMPLATE>
<asp:CheckBox id="chk" runat="server" />
</ITEMTEMPLATE>
</asp:repeater>
Much appreciated....
I am new to this ASP.net :-) cheers
Are you using the SqlDataSource control? I still haven't found a way to get the ID because i found out the DataItem of a repeater is set to null other than a few events of the repeater (http://www.netnewsgroups.net/group/microsoft.public.dotnet.framework.aspnet/topic4049.aspx).
[code]
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item)
{
bool isChecked = ((item.FindControl("chk") as CheckBox).Checked);
// --> this needs fixing:
//int id = item.DataItem;
}
}
[/code]
[code]
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item)
{
bool isChecked = ((item.FindControl("chk") as CheckBox).Checked);
// --> this needs fixing:
//int id = item.DataItem;
}
}
[/code]
From what i have looked at i cannot see a way to get the bound data from the repeater. I haven't yet had a need to use a repeater so i didn't relise it was so basic,
The best solution i can think of is to make a hidden field and then use findcontrol to get the id out of it. Add a hidden field to your ItemTemplate:
<asp:HiddenField ID="HiddenField1" runat="server" />
Jono
The best solution i can think of is to make a hidden field and then use findcontrol to get the id out of it. Add a hidden field to your ItemTemplate:
<asp:HiddenField ID="HiddenField1" runat="server" />
Jono
ASKER
Well I just included a hidden field with its value set to record id.
However, the record_ids returned are the ones that were already in the database.
Whatever the user selectes/deselects is ignored...
Any ideas???
Thanks for your help...
However, the record_ids returned are the ones that were already in the database.
Whatever the user selectes/deselects is ignored...
Any ideas???
Thanks for your help...
please explain?
However, the record_ids returned are the ones that were already in the database.
where else do they come from?
However, the record_ids returned are the ones that were already in the database.
where else do they come from?
ASKER
Say, I get 3 records listed
and say, the first checkbox is selected, while other two are not (obviously which checkbox is selected depends upon the relevant value in database).
Now the user deselects the first checkbox
and selects third checkbox instead.
And clicks on the Submit button, so that the ON/OFF values in the db will be changed.
BUT after the submit is clicked, the array that holds values returned from above action
still has record_id 1 as ON and other two as OFF.
Pl let me know if I should explain in more detail...
Cheers for your help buddy. :-)
and say, the first checkbox is selected, while other two are not (obviously which checkbox is selected depends upon the relevant value in database).
Now the user deselects the first checkbox
and selects third checkbox instead.
And clicks on the Submit button, so that the ON/OFF values in the db will be changed.
BUT after the submit is clicked, the array that holds values returned from above action
still has record_id 1 as ON and other two as OFF.
Pl let me know if I should explain in more detail...
Cheers for your help buddy. :-)
do you use an sql command or statement to update the database base on what check box is checked?
The easiest way to conserve updating the database with the checked state it has is to make another hiddenfield with the checked state at the beginning. Use findcontrol again and then if the value is different to the checkbox then update it in the database.
ASKER
[deanvanrooyen ]
Yes I am using an SQL statement, to update the db, based on what checkbox is selected!
[jonorossi ]
The hidden field will give me existing values in the database.
The same thing, I am getting in the array that holds what is returned from the form (checkboxes) after submit button is clicked.
On comparision, I will always get YES.
Any ideas???
Thanks a lot for your help. :-)
Yes I am using an SQL statement, to update the db, based on what checkbox is selected!
[jonorossi ]
The hidden field will give me existing values in the database.
The same thing, I am getting in the array that holds what is returned from the form (checkboxes) after submit button is clicked.
On comparision, I will always get YES.
Any ideas???
Thanks a lot for your help. :-)
I don't understand what the problem is. What array are you referring to?
ASKER
Pl see below
User visits the page
Original Selections (pulled from database) When page loads
checkbox1 ON
checkbox2 OFF
checkbox3 OFF
-> now the user changes them to
checkbox1 OFF
checkbox2 ON
checkbox3 ON
and hits the submit button.
NOW I, ofcourse, need the new values so that I can change the database as per user's new selections.
Whatever user submits, I store in an array.
That array should contain new values selected by user
BUT it does not
it still contains, the old original values that were already in the database, which are useless.
I hope this explanation makes more sense.
Thanks guys....
:-)
User visits the page
Original Selections (pulled from database) When page loads
checkbox1 ON
checkbox2 OFF
checkbox3 OFF
-> now the user changes them to
checkbox1 OFF
checkbox2 ON
checkbox3 ON
and hits the submit button.
NOW I, ofcourse, need the new values so that I can change the database as per user's new selections.
Whatever user submits, I store in an array.
That array should contain new values selected by user
BUT it does not
it still contains, the old original values that were already in the database, which are useless.
I hope this explanation makes more sense.
Thanks guys....
:-)
Are you rebinding the repeater on the postback? Basically do you have a if(!IsPostBack) around the binding code? Ignore if that if you do, it is just that this is the most common reason for this type of thing.
ASKER
No buddy... there is no if(!IsPostBack).... in the script...
@ @
£
~~
scratching my head...
@ @
£
~~
scratching my head...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this is how I do it, I used a update button next to every row, but you could use one update button and search through every row to repeat update command...
I am using sql express.
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_I temCommand " >
<ItemTemplate>
<asp:Label ID="label1" Text='<%# Eval("ProductID") %>' runat="server" Width="50"></asp:Label>
<asp:CheckBox id="chk" runat="server" Text="Discontinued?" Checked='<%# System.Convert.ToBoolean(E val("Disco ntinued")) %>' Width="50"/>
<asp:Button id="btnUpdate" Runat="server" Text="Update" CommandName="Update" Font-Size="12px" Width="50"></asp:Button>
<br />
<br />
</ItemTemplate>
</asp:Repeater>
public void Repeater1_ItemCommand(obje ct source, RepeaterCommandEventArgs e)
{
Int32 id = System.Convert.ToInt32(((L abel)e.Ite m.FindCont rol("label 1")).Text) ; //record id key
Int16 check = System.Convert.ToInt16(((C heckBox)e. Item.FindC ontrol("ch k")).Check ed);
//update the db
string strcon = SqlDataSource1.ConnectionS tring;
System.Data.SqlClient.SqlC onnection sqlCon = new System.Data.SqlClient.SqlC onnection( strcon);
System.Data.SqlClient.SqlC ommand sqlCmd = new System.Data.SqlClient.SqlC ommand("up date products set discontinued = @check where productid = @id", sqlCon);
sqlCmd.Parameters.Add("@id ", SqlDbType.Int).Value = id;
sqlCmd.Parameters.Add("@ch eck", SqlDbType.Int).Value = check;
try
{
sqlCon.Open();
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
//rebind the repeater here to reflect updated record - this might not be required though as the checkbox should remain checked with view state
}
catch(Exception me)
{
}
}
hope this helps
I am using sql express.
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_I
<ItemTemplate>
<asp:Label ID="label1" Text='<%# Eval("ProductID") %>' runat="server" Width="50"></asp:Label>
<asp:CheckBox id="chk" runat="server" Text="Discontinued?" Checked='<%# System.Convert.ToBoolean(E
<asp:Button id="btnUpdate" Runat="server" Text="Update" CommandName="Update" Font-Size="12px" Width="50"></asp:Button>
<br />
<br />
</ItemTemplate>
</asp:Repeater>
public void Repeater1_ItemCommand(obje
{
Int32 id = System.Convert.ToInt32(((L
Int16 check = System.Convert.ToInt16(((C
//update the db
string strcon = SqlDataSource1.ConnectionS
System.Data.SqlClient.SqlC
System.Data.SqlClient.SqlC
sqlCmd.Parameters.Add("@id
sqlCmd.Parameters.Add("@ch
try
{
sqlCon.Open();
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
//rebind the repeater here to reflect updated record - this might not be required though as the checkbox should remain checked with view state
}
catch(Exception me)
{
}
}
hope this helps
ASKER
[jonorossi]
I tried both, with IsPostBack and with !IsPostBack... nothing works...
[deanvanrooyen]
Thanks for the code, but it is far away from what I am looking for.
Any other suggestions??
Thanks to all efforts!
I tried both, with IsPostBack and with !IsPostBack... nothing works...
[deanvanrooyen]
Thanks for the code, but it is far away from what I am looking for.
Any other suggestions??
Thanks to all efforts!
Can you post your code from the aspx and cs files that are applicable to this issue.
you say you store the checked option in an array?
why? and can you post the code.
why? and can you post the code.
ASKER
// Here is the code for aspx.cs file (only relevent section)
////////////////////////// ////////// ////////// ////////// //////////
<asp:Repeater ID="repeaterElements" runat="server">
<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container, "DataItem.label_name") %>
</td>
<td>
<%# DataBinder.Eval(Container, "DataItem.element_name") %>
</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Checked=<%# DataBinder.Eval(Container, "DataItem.default_on_off") %> />
</td>
<td>
<asp:HiddenField ID="HiddenField1" value=<%# DataBinder.Eval(Container, "DataItem.label_id") %> runat="server" />
<A href=labels.aspx?action=ed it&element _id=<%# DataBinder.Eval(Container, "DataItem.element_id") %>&label_name=<%# DataBinder.Eval(Container, "DataItem.label_name") %>>Edit</A>
/
<A href=labels.aspx?action=de lete&label _id=<%# DataBinder.Eval(Container, "DataItem.label_id") %>>Delete</A>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Button_Save2" runat="server" Text="Save" OnClick="Button_Save2_Clic k" />
</FooterTemplate>
</asp:Repeater>
// Here is the code for aspx.cs file (only relevent section)
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// ////
protected void Button_Save2_Click (object sender, System.EventArgs e)
{
ArrayList arrList = new ArrayList();
foreach (RepeaterItem item in repeaterElements.Items)
{
CheckBox cb = (CheckBox)item.FindControl ("CheckBox 1");
if (cb.Checked)
{
HiddenField label_id = (HiddenField)item.FindCont rol("Hidde nField1");
arrList.Add(label_id.Value );
arrList.TrimToSize();
}
}
}
//////////////////////////
<asp:Repeater ID="repeaterElements" runat="server">
<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container,
</td>
<td>
<%# DataBinder.Eval(Container,
</td>
<td>
<asp:CheckBox ID="CheckBox1" runat="server" Checked=<%# DataBinder.Eval(Container,
</td>
<td>
<asp:HiddenField ID="HiddenField1" value=<%# DataBinder.Eval(Container,
<A href=labels.aspx?action=ed
/
<A href=labels.aspx?action=de
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Button_Save2" runat="server" Text="Save" OnClick="Button_Save2_Clic
</FooterTemplate>
</asp:Repeater>
// Here is the code for aspx.cs file (only relevent section)
//////////////////////////
protected void Button_Save2_Click (object sender, System.EventArgs e)
{
ArrayList arrList = new ArrayList();
foreach (RepeaterItem item in repeaterElements.Items)
{
CheckBox cb = (CheckBox)item.FindControl
if (cb.Checked)
{
HiddenField label_id = (HiddenField)item.FindCont
arrList.Add(label_id.Value
arrList.TrimToSize();
}
}
}
I cannot see any problems with that code at the moment; can you show the code in your Page_Load that binds the repeater.
Whay do you store it in an array instead of writting the update to the database and rebind the repeater?
I think he is just doing that for now while testing, he probably has a breakpoint set and he checks the values in the array.
aha - that why the values are always the same in the array
check this out :
<asp:HiddenField ID="HiddenField1" value=<%# DataBinder.Eval(Container,
then
HiddenField label_id = (HiddenField)item.FindCont
arrList.Add(label_id.Value
nothing is actually been updated it is always the value from the database that is stored....
does this make sense?
I am still confused though
User visits the page
Original Selections (pulled from database) When page loads
checkbox1 ON (database field = true)
checkbox2 OFF (database field = false)
checkbox3 OFF (database field = false)
-> now the user changes them to
checkbox1 OFF (database field still = true)
checkbox2 ON (database field still = false)
checkbox3 ON (database field still = false)
and hits the submit button.
he adds the hidden field to array which still contains the original db field value
so array = ( false,false) although
but he thinks it should be (true,true) because the check boxes are now checked .....
see what i mean kindof? am I on the right track here - I feel that I am answering what he has put in writing but the issue is somthing else ... anyway....
User visits the page
Original Selections (pulled from database) When page loads
checkbox1 ON (database field = true)
checkbox2 OFF (database field = false)
checkbox3 OFF (database field = false)
-> now the user changes them to
checkbox1 OFF (database field still = true)
checkbox2 ON (database field still = false)
checkbox3 ON (database field still = false)
and hits the submit button.
he adds the hidden field to array which still contains the original db field value
so array = ( false,false) although
but he thinks it should be (true,true) because the check boxes are now checked .....
see what i mean kindof? am I on the right track here - I feel that I am answering what he has put in writing but the issue is somthing else ... anyway....
I think you miss read it:
CheckBox cb = (CheckBox)item.FindControl ("CheckBox 1");
if (cb.Checked)
{
HiddenField label_id = (HiddenField)item.FindCont rol("Hidde nField1");
arrList.Add(label_id.Value );
arrList.TrimToSize();
}
You will see that it gets the checkbox, then if it is checked it inserts the value into into the list. So you will end up with a list (the ID) of the checked items. This is not what you want to execute something in the database but if you check something that wasn't previously checked then it should be in the list. manu009 was however saying that it was always the same.
What manu009 needs is something like this:
Add another hidden field like this:
<asp:HiddenField ID="HiddenField2" value=<%# DataBinder.Eval(Container, "DataItem.default_on_off") %> runat="server" />
Then code like this:
CheckBox cb = (CheckBox)item.FindControl ("CheckBox 1");
bool originalCheckedValue = (HiddenField)item.FindCont rol("Hidde nField2"). Checked;
if (cb.Checked != originalCheckedValue)
{
HiddenField label_id = (HiddenField)item.FindCont rol("Hidde nField1");
// execute your database code to update the row with 'label_id' with the value of cb.Checked
}
CheckBox cb = (CheckBox)item.FindControl
if (cb.Checked)
{
HiddenField label_id = (HiddenField)item.FindCont
arrList.Add(label_id.Value
arrList.TrimToSize();
}
You will see that it gets the checkbox, then if it is checked it inserts the value into into the list. So you will end up with a list (the ID) of the checked items. This is not what you want to execute something in the database but if you check something that wasn't previously checked then it should be in the list. manu009 was however saying that it was always the same.
What manu009 needs is something like this:
Add another hidden field like this:
<asp:HiddenField ID="HiddenField2" value=<%# DataBinder.Eval(Container,
Then code like this:
CheckBox cb = (CheckBox)item.FindControl
bool originalCheckedValue = (HiddenField)item.FindCont
if (cb.Checked != originalCheckedValue)
{
HiddenField label_id = (HiddenField)item.FindCont
// execute your database code to update the row with 'label_id' with the value of cb.Checked
}
that looks better
if (((CheckBox)Repeater1.Item
Then you get the ID from item you use the DataItem and cast it to either a business object or a DataRow or whatever you are binding to the repeater:
Repeater1.Items[0].DataIte
Hope that helps, Jono