We took one bound checkboxlist and a TextBox. When the user checked a checkbox and clicks the button for it, the selected data will be entered into the TextBox.

Initial Chamber

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (CheckBoxList_demo).

Step 2

In Solution Explorer you get your empty website, then add two web forms and a SQL Server Database as in the following.

For Web Form:

CheckBoxList_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it Autocomplete_demo.aspx.

For SQL Server Database

CheckBoxList_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add a database inside the App_Data_folder).

DATABASE CHAMBER

Step 3

In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.

Table tbl_data (Don't forget to make ID as IS Identity -- True).

table design
We had embed some data in the table tbl_seo. To do that just go to your table then right-click then select Show Table Data then enter your data in the given field.

table

Design Chamber

Step 4

Open your CheckBoxList_demo.aspx file from Solution Explorer and start designing you're application as in the following:

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html
  4. xmlns="http://www.w3.org/1999/xhtml">
  5. <headrunat="server">
  6. <title></title>
  7. <styletype="text/css">
  8. .style2
  9. {
  10. width: 358px;
  11. }
  12. .style3
  13. {
  14. width: 44px;
  15. }
  16. .style4
  17. {
  18. width: 358px;
  19. text-decoration: underline;
  20. font-size: medium;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <formid="form1"runat="server">
  26. <div>
  27. <tablestyle="width:100%;">
  28. <tr>
  29. <tdclass="style3">
  30. </td>
  31. <tdclass="style4">
  32. <strong>CheckBox List to Textbox</strong>
  33. </td>
  34. <td>
  35. </td>
  36. </tr>
  37. <tr>
  38. <tdclass="style3">
  39. </td>
  40. <tdclass="style2">
  41. <asp:CheckBoxListID="CheckBoxList1"runat="server"DataTextField="category"
  42. DataValueField="category">
  43. </asp:CheckBoxList>
  44. </td>
  45. <td>
  46. <asp:TextBoxID="txtmsg"runat="server"TextMode="MultiLine">
  47. </asp:TextBox>
  48. </td>
  49. </tr>
  50. <tr>
  51. <tdclass="style3">
  52. </td>
  53. <tdclass="style2">
  54. </td>
  55. <td>
  56. </td>
  57. </tr>
  58. <tr>
  59. <tdclass="style3">
  60. </td>
  61. <tdclass="style2">
  62. <asp:ButtonID="Button1"runat="server"onclick="Button1_Click"
  63. Text="Submit your data to textbox"/>
  64. </td>
  65. <td>
  66. </td>
  67. </tr>
  68. </table>
  69. </div>
  70. </form>
  71. </body>
  72. </html>
Your design looks as in the image given below:

design

Code Chamber

Finally open your CheckBoxList_demo.aspx.cs file to write the code, so that the application works.

Here is the code:
  1. using System;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. usingSystem.Web.UI;
  6. usingSystem.Web.UI.WebControls;
  7. usingSystem.Data;
  8. usingSystem.Data.SqlClient;
  9. publicpartialclass_Default : System.Web.UI.Page
  10. {
  11. protectedvoidPage_Load(object sender, EventArgs e)
  12. {
  13. if (!Page.IsPostBack)
  14. {
  15. SqlConnection con = newSqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  16. SqlCommandcmd = newSqlCommand("select * from tbl_seo", con);
  17. SqlDataAdaptersda = newSqlDataAdapter(cmd);
  18. DataTabledt = newDataTable();
  19. sda.Fill(dt);
  20. CheckBoxList1.DataSource = dt;
  21. CheckBoxList1.DataBind();
  22. }
  23. }
  24. protectedvoid Button1_Click(object sender, EventArgs e)
  25. {
  26. for (inti = 0; i< CheckBoxList1.Items.Count; i++)
  27. {
  28. if (CheckBoxList1.Items[i].Selected)
  29. {
  30. txtmsg.Text += CheckBoxList1.Items[i].Text + ",";
  31. }
  32. }
  33. txtmsg.ForeColor = System.Drawing.Color.MediumVioletRed;
  34. }
  35. }
Output Chamber

output

checkbox

I hope you like it. Thank you for reading. Have a good day.