473,770 Members | 6,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get/Retrieve values of dynamically generated checkboxlist in datalist

6 New Member
Hi

I have a datalist which loops through the categories and within the datalist I have a CheckBoxList which are bound on the DataList's OnItemDataBound .

The only problem I have is retrieving the values that have been checked when the user submits the form.

This code works as far as presentation goes but I can't seem to retrieve the checked values in the various checkboxlists that have been generated on the datalist's OnItemDataBound

Below is part of what my page is doing

<asp:DataList ID="dtlSpeciali zationCat" OnItemDataBound ="SpecCat_OnIte mDataBound" runat="server" RepeatColumns=" 4" RepeatDirection ="Horizontal ">
<ItemTemplate >
<h3 class="specList ItemHeader"><%# DataBinder.Eval (Container.Data Item, "CategoryNa me") %></h3>
<asp:CheckBoxLi st ID="chkListSpec ializations" runat="server">
</asp:CheckBoxLis t>
</ItemTemplate>
</asp:DataList>


Code Behind for ItemDataBound and binding the DataList
protected void SpecCat_OnItemD ataBound(object sender, DataListItemEve ntArgs e)
{
DataListItem item = e.Item;
if (item.ItemType == ListItemType.It em || item.ItemType == ListItemType.Al ternatingItem)
{
CheckBoxList checkBox = (CheckBoxList)i tem.FindControl ("chkListSpecia lizations");
System.Data.Com mon.DbDataRecor d record = (System.Data.Co mmon.DbDataReco rd)item.DataIte m;
int id = 0;
int.TryParse(re cord["SpecCategoryID "].ToString(),out id);
if (id > 0)
{
using (SqlConnection conn = new SqlConnection(C onfigurationMan ager.Connection Strings["conn"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("pro c_GetSpecsByCat ", conn))
{
cmd.CommandType = CommandType.Sto redProcedure;
SqlParameter catIDParam = new SqlParameter("@ CatID", SqlDbType.Int);
catIDParam.Valu e = id;
cmd.Parameters. Add(catIDParam) ;
conn.Open();
using (SqlDataReader rdr = cmd.ExecuteRead er())
{
while (rdr.Read())
{
ListItem li = new ListItem();
li.Value = rdr["Specialization ID"].ToString();
li.Text = rdr["Specialization Name"].ToString();
checkBox.Items. Add(li);
}
}
}
}
}
}
item.Dispose();
}

protected void bindSpecializat ionCategories()
{
using (SqlConnection conn = new SqlConnection(C onfigurationMan ager.Connection Strings["conn"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("pro c_GetCategories ", conn))
{
cmd.CommandType = CommandType.Sto redProcedure;
conn.Open();
dtlSpecializati onCat.DataSourc e = cmd.ExecuteRead er();
dtlSpecializati onCat.DataBind( );
}
}
}
Nov 7 '07 #1
1 7938
terminul
6 New Member
Hi again,

after a brief struggle with the code and a few readings, I managed to get the right set of code to retrieve the items selected....

"dtlSpecializat ionCat" is the datalist, I found that I had to reiterate thru the controls within the datalist to retrieve the checked ListItem values.

as all the CheckBoxLists have the ID "chkListSpecial izations".. so all I had to do was find that control since I had looped through the controls and the ids in the datalist and the CheckBoxLists all had the same name.

This method is invoked on clicking a register button.

protected void Register_ClickS ubmit(object sender, EventArgs e)
{
string specializations = "";
foreach (DataListItem dli in dtlSpecializati onCat.Items)
{
CheckBoxList cbL = (CheckBoxList)d li.FindControl( "chkListSpecial izations");
if (cbL != null)
{
foreach (ListItem li in cbL.Items)
{
if (li.Selected)
{
specializations += li.Text + " (" + li.Value + ")<br>";
}
}
}
else
{
specializations += "null :( - bad ";
}
}
litTest.Text += specializations ;
}

So there, answered my own question.
Nov 7 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2885
by: Ed West | last post by:
Hello I am trying to put a CheckBoxList in a DataList but it's not working too great. Is this possible? <asp:datalist id=DataList1 runat="server" RepeatColumns="4"> <HeaderTemplate> <asp:CheckBoxList id=states runat=server> </HeaderTemplate> <ItemTemplate>
4
6550
by: krzysiek | last post by:
hello, i have several radiolists and checkboxlist that are generated dinamicly based on datasource. So frankly speaking i don't know names and number of that web controls cose they are always different. How can i retreive selected values from those controls ?
0
2531
by: Boris | last post by:
When I dynamically create CheckButtonList, I add ListItem(s) to my CheckButtonList object chkList chkList.Items.Add(new ListItem("My Text", "My Value")); The resulting HTML doesn't contain value="My Value". Therefore, I can't access "My Value" with Request.Form on post back. I do get the desired outcome with my RadioButtonList object radioList when I do
2
2097
by: Patrick.O.Ige | last post by:
Is it possible to bind a checkboxlist if this checkboxlist is in a datalist Bcos when i add a CheckBoxlist to a Datalist i get :- Object reference not set to an instance of an object error at:- checkboxlist1.DataSource = myDataSet Why?
5
13394
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able to get a Boolean value TRUE or FALSE when a checkBox is selected. When the checkBoxList was out of the DataList i used "OnSelectedIndexChanged" and it was returning what i wanted but if its in a
4
4043
by: Patrick.O.Ige | last post by:
I have a CheckBoxList in a DataList and i'm trying to get item Selected after doing a postBack. I have set my CheckBoxlist AutoPostBack="True" Any ideas what 'm doing wrong? It seems not to work:( Thanks My CheckBoxList in the DataList Below
1
2199
by: Mike | last post by:
I have a Datalist control on my page that I have binded to a table of available categories. In the Item Template I have a CheckboxList control bound to the products available in each Category. I need to pull out of the page which products the user has selected. I have bound the products to the CheckboxList control in the ItemDataBound event for the Datalist. How can I extract what checkboxes have been selected inside the Datalist?
0
1488
by: KBuser | last post by:
I'm building an internal site which will allow for extremely customizable queries to be run against our SQL Server (2000) DB. The page is done in ASP .net 2.0, with C# code behind. The initial page has a checkboxlist which is generated from a query which returns all the user tables in our database. This is checkboxlistTables. For each table selected in this list I want to dynamically generate a new checkboxlist in a (HTML) table on the...
1
2837
by: KBuser | last post by:
I'm building an internal site which will allow for extremely customizable queries to be run against our SQL Server (2000) DB. The page is done in ASP .net 2.0, with C# code behind. The initial page has a checkboxlist which is generated from a query which returns all the user tables in our database. This is checkboxlistTables. For each table selected in this list I want to dynamically generate a new checkboxlist in a (HTML) table on the...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.