| | |
Datalist issue with checkbox
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 10
Reputation:
Solved Threads: 0
Hi
I am trying to bind two checkboxes in a datagrid...The 1st Checkbox is category and the 2nd Checkbox is sub category... A category can have several sub categories. When i bind the 1st checkbox the category name is repeated more than once and I need to have only one checkbox for one category. Please find the code below:
<asp
ataList ID="title_dl" runat="server" DataSourceID="title_dss" DataKeyField="M_CATPAGES_ID" >
<ItemTemplate>
<td><asp:Checkbox ID ="cat_title_chk" AutoPostBack="true" runat="server" Text='<%# Container.DataItem("M_CATPAGES_CAT") %>'></asp:Checkbox> </td>
<td><asp:CheckBox ID="sub_cat_chk" runat="server" Text='<%# Container.DataItem("M_PAGES_PAGES") %>'></asp:Checkbox> </td>
</ItemTemplate>
</asp
ataList>
<asp
qlDataSource ID="title_dss" runat="server" ConnectionString="Driver={MySQL ODBC 3.51 Driver};DATABASE=laqualite;option=0;pwd=cadt12;port=0;server=10.20.18.50;uid=root" DataSourceMode="DataReader" ProviderName="System.Data.Odbc" SelectCommand="Select M_PAGES_ID_PK,M_PAGES_PAGES,M_CATPAGES_ID,M_CATPAGES_CAT From mast_pages_tbl,mast_catpages_tbl Where M_CATPAGES_ID = M_PAGES_CATID and M_PAGES_ID_PK <> '27' Order By M_CATPAGES_CAT"></asp
qlDataSource>
I am trying to bind two checkboxes in a datagrid...The 1st Checkbox is category and the 2nd Checkbox is sub category... A category can have several sub categories. When i bind the 1st checkbox the category name is repeated more than once and I need to have only one checkbox for one category. Please find the code below:
<asp
ataList ID="title_dl" runat="server" DataSourceID="title_dss" DataKeyField="M_CATPAGES_ID" ><ItemTemplate>
<td><asp:Checkbox ID ="cat_title_chk" AutoPostBack="true" runat="server" Text='<%# Container.DataItem("M_CATPAGES_CAT") %>'></asp:Checkbox> </td>
<td><asp:CheckBox ID="sub_cat_chk" runat="server" Text='<%# Container.DataItem("M_PAGES_PAGES") %>'></asp:Checkbox> </td>
</ItemTemplate>
</asp
ataList><asp
qlDataSource ID="title_dss" runat="server" ConnectionString="Driver={MySQL ODBC 3.51 Driver};DATABASE=laqualite;option=0;pwd=cadt12;port=0;server=10.20.18.50;uid=root" DataSourceMode="DataReader" ProviderName="System.Data.Odbc" SelectCommand="Select M_PAGES_ID_PK,M_PAGES_PAGES,M_CATPAGES_ID,M_CATPAGES_CAT From mast_pages_tbl,mast_catpages_tbl Where M_CATPAGES_ID = M_PAGES_CATID and M_PAGES_ID_PK <> '27' Order By M_CATPAGES_CAT"></asp
qlDataSource> well i can tell you right now why.
you are retreiving category and sub category and the same resultset and your data is show as example.
Category Sub_Categor
Network CCNA
Network CCNP
Programming C#
Programming VB
and when you pass that to the datalist is going to embed the data as show above. and what you need is
Network CCNA
CCNP
Programming C#
VB
now are you doing that because somebody can select multiple sub_category at the same time?
you are retreiving category and sub category and the same resultset and your data is show as example.
Category Sub_Categor
Network CCNA
Network CCNP
Programming C#
Programming VB
and when you pass that to the datalist is going to embed the data as show above. and what you need is
Network CCNA
CCNP
Programming C#
VB
now are you doing that because somebody can select multiple sub_category at the same time?
If your already resolved your issue, flag it as solved.
José Bisonó
José Bisonó
•
•
Join Date: May 2009
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
well i can tell you right now why.
you are retreiving category and sub category and the same resultset and your data is show as example.
Category Sub_Categor
Network CCNA
Network CCNP
Programming C#
Programming VB
and when you pass that to the datalist is going to embed the data as show above. and what you need is
Network CCNA
CCNP
Programming C#
VB
now are you doing that because somebody can select multiple sub_category at the same time?
ok, i do not know if this is the best solution but it work.
I test this try to replace your statement in values, notice the event OnItemDataBound that makes the trick.
aspx
Now in the onload page i just generate the first checkbox which is the category one. notice in the select statement the function DISTINCT.
now i create another function while the datalist is bounded this is the function.
i bet you there are a few ways to do this but, this is the first one that comes to my mind.
Regards
I test this try to replace your statement in values, notice the event OnItemDataBound that makes the trick.
aspx
<table>
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="bounditem">
<ItemTemplate>
<tr><td>
<asp:CheckBox ID="chkCategory" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "OPTION_FNCTN").ToString() %>' /></td><td>
<asp:CheckBoxList ID="ckhSubCategory" runat="server"></asp:CheckBoxList></td></tr>
</ItemTemplate>
</asp:DataList>
</table>Now in the onload page i just generate the first checkbox which is the category one. notice in the select statement the function DISTINCT.
protected void Page_Load(object sender, EventArgs e)
{
string st = "SELECT distinct OPTION_FNCTN FROM MENUOPTS_T WHERE (OPTION_FNCTN <> '') ORDER BY OPTION_FNCTN";
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter(st, MyConn);
MyConn.Open();
adap.Fill(ds);
MyConn.Close();
DataList1.DataSource = ds;
DataList1.DataBind();
}now i create another function while the datalist is bounded this is the function.
protected void bounditem(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string stGet = "SELECT OPTION_DESC FROM MENUOPTS_T WHERE (OPTION_FNCTN = '"+((CheckBox)e.Item.FindControl("chkCategory")).Text+"')";
DataSet dsGet = new DataSet();
SqlDataAdapter adapGet = new SqlDataAdapter(stGet, MyConn);
MyConn.Open();
adapGet.Fill(dsGet);
MyConn.Close();
((CheckBoxList)e.Item.FindControl("chkSubCategory")).DataSource = dsGet;
((CheckBoxList)e.Item.FindControl("chkSubCategory")).DataTextField = "OPTION_DESC";
((CheckBoxList)e.Item.FindControl("chkSubCategory")).DataValueField = "OPTION_DESC";
((CheckBoxList)e.Item.FindControl("chkSubCategory")).DataBind();
}
}i bet you there are a few ways to do this but, this is the first one that comes to my mind.
Regards
If your already resolved your issue, flag it as solved.
José Bisonó
José Bisonó
•
•
Join Date: May 2009
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
ok, i do not know if this is the best solution but it work.
I test this try to replace your statement in values, notice the event OnItemDataBound that makes the trick.
aspx
<table> <asp:DataList ID="DataList1" runat="server" OnItemDataBound="bounditem"> <ItemTemplate> <tr><td> <asp:CheckBox ID="chkCategory" runat="server" Text='<% #DataBinder.Eval(Container.DataItem, "OPTION_FNCTN").ToString() %>' /></td><td> <asp:CheckBoxList ID="ckhSubCategory" runat="server"></asp:CheckBoxList></td></tr> </ItemTemplate> </asp:DataList> </table>
Now in the onload page i just generate the first checkbox which is the category one. notice in the select statement the function DISTINCT.
protected void Page_Load(object sender, EventArgs e) { string st = "SELECT distinct OPTION_FNCTN FROM MENUOPTS_T WHERE (OPTION_FNCTN <> '') ORDER BY OPTION_FNCTN"; DataSet ds = new DataSet(); SqlDataAdapter adap = new SqlDataAdapter(st, MyConn); MyConn.Open(); adap.Fill(ds); MyConn.Close(); DataList1.DataSource = ds; DataList1.DataBind(); }
now i create another function while the datalist is bounded this is the function.
protected void bounditem(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { string stGet = "SELECT OPTION_DESC FROM MENUOPTS_T WHERE (OPTION_FNCTN = '"+((CheckBox)e.Item.FindControl("chkCategory")).Text+"')"; DataSet dsGet = new DataSet(); SqlDataAdapter adapGet = new SqlDataAdapter(stGet, MyConn); MyConn.Open(); adapGet.Fill(dsGet); MyConn.Close(); ((CheckBoxList)e.Item.FindControl("chkSubCategory")).DataSource = dsGet; ((CheckBoxList)e.Item.FindControl("chkSubCategory")).DataTextField = "OPTION_DESC"; ((CheckBoxList)e.Item.FindControl("chkSubCategory")).DataValueField = "OPTION_DESC"; ((CheckBoxList)e.Item.FindControl("chkSubCategory")).DataBind(); } }
i bet you there are a few ways to do this but, this is the first one that comes to my mind.
Regards
Hi Thank you very much for the solution...You have been very helpful..... I have a small problem in running the code due to an issue in the query in bounditem... I have reworked on the query as follows :
SELECT a.SubCat_Name FROM SubCategory Table as a,Category Table as b WHERE Cat_Name= '" & DirectCast(e.Item.FindControl("chkCategory"), CheckBox).Text & "') and a.SubCat_Cat_id=b.Cat_Id
I am binding the Category and Sub category from different tables
Category Table
Cat_Id
Cat_Name
SubCategory Table
SubCat_Id
SubCat_Name
SubCat_Cat_id
The SubCat_Cat_id from the SubCategory table is referred to the Cat_Id in Category Table.... But I am unable to get the results displayed but when i run the query in Sql Server it works fine...Please help me out....
Try this
I do not know if your really need DirectCast cause you are just asking for the checkbox label text which is string.
regards.
Select a.SubCat_Name from SubCategory as a inner join category as b on a.SubCat_Cat_id = b.Cat_id
where b.Cat_name = '" & DirectCast(e.Item.FindControl("chkCategory"), CheckBox).Text & "'I do not know if your really need DirectCast cause you are just asking for the checkbox label text which is string.
regards.
If your already resolved your issue, flag it as solved.
José Bisonó
José Bisonó
![]() |
Similar Threads
- insert checkbox values to database (MySQL)
- check\uncheck all checkboxes in datalist control (ASP.NET)
- checkbox checkchanged event not firing in datalist control (ASP)
- Checkbox in DataGridView (VB.NET)
- creation of checkbox in javascript (JavaScript / DHTML / AJAX)
- checkbox as a itemtemplate in a datalist (ASP.NET)
- Edit item event handler issue (ASP.NET)
- Display formatted dataList based on events.. (ASP.NET)
Other Threads in the VB.NET Forum
- Previous Thread: How to create excel sheet ?
- Next Thread: Vb[2008] Populating combo box from textfile (over 45,000 lines)
Views: 802 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2005 30minutes 2005 2008 access application arithmetic array basic binary bing button buttons c# center check code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview design designer dissertation dissertations dissertationthesis dll dropdownlist error excel file-dialog folder ftp google hardcopy highlighting image images inline installer listview login mobile ms navigate net networking opacity output peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project reports" save savedialog searchbox serial server soap sorting sql string studio syntax tcp text textbox timer toolbox trim updown usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





