C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




CheckBoxList with DropDownList in ASP Net 2.0


Posted Date: 29 Aug 2008      Total Responses: 21

Posted By: Asad Moosa       Member Level: Silver     Points: 1


If there are 4 rows in the database, so the checkboxlist will show 4 checkboxes, and I also want to get dropdownlist including 5 or 7 items along with this.

It means we have 4 rows including checkboxes and dropdownlist, so i can get a value from Checkbox and Dropdownlist.Item.

Like you want to check on the "News Subscribe" and then select which contact method (Pager, or Email or Phone number). Another "Weather Alert" then select which contact method. Make sense? I want this code please.






Responses

Author: Ritesh N. Jain    29 Aug 2008Member Level: GoldRating:     Points: 2
wondering why dont you use GridView or similar datalisting control where you can add checkbox,dropdownlist and other control into Template column?


Author: Asad Moosa    29 Aug 2008Member Level: SilverRating:     Points: 4
Yes i did use GridView, but DataKeyNames become confused.

I put CheckBox_ID in DataKeyNames. It works well, and i try to get dropdownlist to populate from the table. It won't work after i put DataFieldText and DataFieldValue in Dropdownlist.

What is wrong with that?


Author: Ritesh N. Jain    29 Aug 2008Member Level: GoldRating:     Points: 5
can you post your aspx's html code and show us how you are trying to bind dropdownlist.
The simple logic will be
1>Add 3 column PK Value,Template col that will hold CheckBox, and one more template column for DropDownlist
2>You can bind checkbox's Text data to column coming from DB.
3>In gridvew's rowdatabound event simply capture the 1 col value i.e PK or simply use DateKey and fill Datatable From DB based on this value that will going to get bind with DropDownlist.


Author: Asad Moosa    29 Aug 2008Member Level: SilverRating:     Points: 6
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BorderStyle="Solid" CellPadding="4" DataKeyNames="NotificationType_ID" BorderColor="Silver" BorderWidth="1px" Width="300px">
<Columns>
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<asp:CheckBox ID="chkboxnotify" runat="server" Text='<%# Eval("NotificationType") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order Status">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddltest" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Left" />

</asp:GridView>

This is for Checkbox:
private void ListTypes()
{
clsNotifications notifications = new clsNotifications();
GridView2.DataSource = notifications.ListNotificationTypes();
GridView2.DataBind();

}
It works, but not

clsContact contact= new clsContact();
ddltest.DataSource = contact.ContactTypes();
ddltest.DataBind();




Author: Ritesh N. Jain    29 Aug 2008Member Level: GoldRating:     Points: 5
for ddlTest you have to use GridView2's RowDatabound event to first fetch the ddltest or simply loop through all the rows of Gridview to indvidually fill the Dropdownlist...something like this



clsNotifications notifications = new clsNotifications();
GridView2.DataSource = notifications.ListNotificationTypes();
GridView2.DataBind();


DropDownList ddltest;

foreach (GridViewRow grdRow in GridView2.Rows)
{

ddltest = (DropDownList)( GridView2.Rows[grdRow.RowIndex].Cells[2].FindControl("ddltest"));

clsContact contact= new clsContact();
ddltest.DataSource = contact.ContactTypes();
//dont't forget to set below two property as per your column name
ddltest.DataTextField = "Col1";
ddltest.DataValueField = "Col2";
ddltest.DataBind();

}





Author: Arun    30 Aug 2008Member Level: BronzeRating:     Points: 6
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BorderStyle="Solid" CellPadding="4" DataKeyNames="NotificationType_ID" BorderColor="Silver" BorderWidth="1px" Width="300px">
<Columns>
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<asp:CheckBox ID="chkboxnotify" runat="server" Text='<%# Eval("NotificationType") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order Status">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddltest" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Left" />

</asp:GridView>


regards
ARUN



Author: Asad Moosa    02 Sep 2008Member Level: SilverRating:     Points: 4
Hey Ritish Jain, I'm very happy that is very very helpful and answer. I am just wondering if you know how to set the value to each row?

For example.
Subscribe Method (Drop down list)
1. E-News Email (it is already chosen as "0" in Index)
2. Whatever Pager (I want to set it to "1" in Index) POSSIBLE?

Thanks so much for your awesome help.


Author: Asad Moosa    02 Sep 2008Member Level: SilverRating:     Points: 4
Hey Ritesh,

how to get a value from checkbox list? I can get a value from the checkbox itself, but not value.

Example:
checkbox Enews (ID=1)
checkbox Whatever (ID=2)
checkbox Whatever1 (ID=3)

I got a "true/false" value but i want to get ID instead of "true/false".

Thank you.




Author: Asad Moosa    02 Sep 2008Member Level: SilverRating:     Points: 3
I finally got how to get ID instead of "true/false". The problem is that I have saved ID in the database. And then when i want to edit the checkbox with method. how can i get a value to put in the checkbox? I'm a little bit confused. :(


Author: Ritesh N. Jain    02 Sep 2008Member Level: GoldRating:     Points: 6
sorry for late reply,In your database just store the Value of CheckBox i.e True or False,and in Grid simply bing the Checked property of the checkbox control that you might have added ad Template column to This Column value from Database.

something like this


<asp:TemplateColumn HeaderText="Test">
<ItemTemplate>
<asp:CheckBox id=CheckBox1 runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "CoulmnName") %>'>
</asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox id="CheckBox2" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "CoulmnName") %>'>
></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateColumn>



Author: Asad Moosa    02 Sep 2008Member Level: SilverRating:     Points: 5
Hey Ritish,

First of all, please don't have to be sorry. Your life is very important so always busy. hehe!

By the way, i got an ID from the checkbox instead of true or false.
when you check on the CheckBoxList. I got a value when a check is on. Then store value in the database.

When a value exists, then check box should be on, and when a value does not exist, but there are other check boxes then appear with "No Check on".

Got what i talk about? If you are confused, i may give you the code.

Thank you.


Author: Ritesh N. Jain    02 Sep 2008Member Level: GoldRating:     Points: 4
hmm i was under impression that you are using CheckBox instead of CheckListBox (as per post # 1 and 3),anyway replace checklist box with Simple checkbox control and As i mentioned above bind the Checked property of CheckBox to Database coulmn that hold it's status/Value as shown above....

I hope i am getting you right or i simply overlooked your problem.


Author: Asad Moosa    02 Sep 2008Member Level: SilverRating:     Points: 5
You mean
<ItemTemplate>
<asp:CheckBoxList id=CheckBox1 runat="server" Checked='<%# Eval("CoulmnName") %>'>
</asp:CheckBoxList>
</ItemTemplate> ?

If yes, what about DropDownList? Similar to above. Like
<ItemTemplate>
<asp:DropDOwnList id=ddltest runat="server" DataValueField='<%# Eval("CoulmnName") %>'>
</asp:DropDOwnList >

Correct?

Thanks again.
</ItemTemplate>


Author: Ritesh N. Jain    03 Sep 2008Member Level: GoldRating:     Points: 6
it should have both CheckBox as well as Dropdownlist as you want checkBox for all your rows and Dropdownlist right?

<asp:TemplateColumn HeaderText="Test">
<ItemTemplate> <asp:CheckBox id=CheckBox1 runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "CoulmnName") %>'> </asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox id="CheckBox2" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "CoulmnName") %>'>></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateColumn>

<ItemTemplate>
<asp:DropDOwnList id=ddltest runat="server" DataValueField='<%# Eval("CoulmnName") %>'>
</asp:DropDOwnList >
</ItemTemplate>




Author: Asad Moosa    03 Sep 2008Member Level: SilverRating:     Points: 6
So, there will be 4 procedures to get one gridview?

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BorderStyle="Solid" CellPadding="4" DataKeyNames="NotificationType_ID" BorderColor="Silver" BorderWidth="1px" Width="300px">
<asp:TemplateColumn HeaderText="Test">
<ItemTemplate> <asp:CheckBox id=CheckBox1 runat="server" Checked='<%# Eval("CoulmnName") %>'></asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox id="CheckBox2" runat="server" Checked='<%# Eval("CoulmnName") %>'></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateColumn>

<ItemTemplate>
<asp:DropDOwnList id=ddlcontacttypes runat="server" DataValueField='<%# Eval("CoulmnName") %>'>
</asp:DropDOwnList >
</ItemTemplate>
</GridView>

1st Procedure to get the number of rows of Notifications:

clsNotifications notifications = new clsNotifications();
GridView2.DataSource = notifications.ListNotificationTypes();
GridView2.DataBind();

DropDownList ddlcontacttypes;

foreach (GridViewRow grdRow in GridView2.Rows)
{
ddlcontacttypes = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[0].FindControl("ddlcontacttypes"));
clsContacts contact = new clsContacts();
ddlcontacttypes.DataSource = contact.ListContactTypes();
ddlcontacttypes.DataTextField = "ContactType";
ddlcontacttypes.DataValueField = "ContactType_ID";
ddlcontacttypes.DataBind();
}

2nd: To get the value of checkbox that is stored in the database:
clsNotifications notifications = new clsNotifications();
CheckBox1.DataSource = notifications.ListNotificationTypes(Customer_ID);
CheckBox1.DataBind();

3rd: To get the value of checkbox that is stored in the database for edit:
clsNotifications notifications = new clsNotifications();
CheckBox2.DataSource = notifications.ListNotificationTypes(Customer_ID);
CheckBox2.DataBind();

4th: To get the value of drop down list of contact that is stored in the database:
clsContacts contact = new clsContacts();
ddlcontacttypes.DataSource = contact.ListContactTypes(Customer_ID);
ddlcontacttypes.DataTextField = "ContactType";
ddlcontacttypes.DataValueField = "ContactType_ID";
ddlcontacttypes.DataBind();

Right?



Author: Asad Moosa    03 Sep 2008Member Level: SilverRating:     Points: 2
Totally forget to change the ColumnNames.

CheckBox1 = NotificationType_ID
CheckBox2 = NotificationType_ID
ddlcontacttypes = ContactType_ID


Author: Ritesh N. Jain    04 Sep 2008Member Level: GoldRating:     Points: 5
as far as dropdownList is concenr your doing correct,but i doubt if CheckBox really support any sort of DataSource property !!,As in your aspx page you are already binding Checked property to appropriate control so no need to do anything in CodeBehind.and whenever you want to retireve value of any checkbox you can use Update or Loop through all rows and find using ctype(e.Rows.FindConrol("CheckBox2),checkbox).checked.


Author: Asad Moosa    04 Sep 2008Member Level: SilverRating:     Points: 2
Got it! I truly appreciate your great help! I have another question, but am going to post a new question. Smile!




Author: Asad Moosa    17 Sep 2008Member Level: SilverRating:     Points: 3
It must be very difficult than I expect. I tried to write a stored procedure that can store a value of check box such as 1 and 0, but There are 2 rows instead of 4 rows so all rows shows true without showing other 2 rows. I'm not sure what to do.


Author: Asad Moosa    17 Sep 2008Member Level: SilverRating:     Points: 3
Hey I finally got a right stored procedure with 4 rows instead of 2 rows. Now, i have stored a value of "true" (1) or "false" (0) as bit. And then the checkbox code you gave me and won't recognize a bit value.


Author: Asad Moosa    17 Sep 2008Member Level: SilverRating:     Points: 3
Hey again, I've successfully got a value of true or false in the check box in the Gridview. Now, i try to figure it out how to get a selected value for DropDownList. Can you help with that?


Post Reply
You must Sign In to post a response.
Next : how to catch error in asp.net raised by oracle sp
Previous : Athira appukuttan ????
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use