473,706 Members | 2,543 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 7932
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
6548
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
2529
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
13392
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
4038
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
1486
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
2835
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
8772
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8692
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
9276
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
8982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5934
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4705
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3140
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
2
2493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.