943,584 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 1500
  • VB.NET RSS
May 18th, 2009
0

Datalist issue with checkbox

Expand Post »
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:

<aspataList 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>

</aspataList>

<aspqlDataSource 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"></aspqlDataSource>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dot_net_rookie is offline Offline
10 posts
since May 2009
May 18th, 2009
0

Re: Datalist issue with checkbox

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?
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
431 posts
since May 2009
May 18th, 2009
0

Re: Datalist issue with checkbox

Click to Expand / Collapse  Quote originally posted by jbisono ...
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?
Exactly that was the output I am looking for as I can select multiple sub categories under a category and the Category should not be repeated more than once
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dot_net_rookie is offline Offline
10 posts
since May 2009
May 18th, 2009
0

Re: Datalist issue with checkbox

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
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
431 posts
since May 2009
May 19th, 2009
0

Re: Datalist issue with checkbox

Click to Expand / Collapse  Quote originally posted by jbisono ...
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....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dot_net_rookie is offline Offline
10 posts
since May 2009
May 19th, 2009
0

Re: Datalist issue with checkbox

Try this

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.
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
431 posts
since May 2009
May 20th, 2009
0

Re: Datalist issue with checkbox

Thank you very much for the help provided ...The issue is fixed and its working fine...............
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dot_net_rookie is offline Offline
10 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: move picture based on row column direction and letters picked
Next Thread in VB.NET Forum Timeline: Vb[2008] Populating combo box from textfile (over 45,000 lines)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC