Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested Control with Checkbox select???

Status
Not open for further replies.

baden

Programmer
Feb 6, 2002
125
US
I need help with the following:

The code below (and code-behind) creates a nested Repeater control, that looks like this:

Desserts
[ ] Cakes
[ ] Cookies
[ ] Ice Cream
[ ] Mousse
[ ] Pastries
[ ] Pies

Dining
[ ] African
[ ] Chinese
[ ] Contemporary
[ ] French

Instead of using a repeater inside, would it be better to use a DataGrid control?

Also, I want to be able to select all items in a given category (adding a checkbox beside the category), and entering into the db. (later, I'll want to read the saved values and populate the checkboxes accoringly).

For the checkbox ID (for sub-categories - the only ones I'm interested in collecting) I tried id="chkbox_<%# DataBinder.Eval(Container.DataItem, "[\"iscID\"]")%>" which returns the error, "Parser Error Message: The server tag is not well formed."



Code:
<asp:repeater id="parentRepeater" runat="server">
   <itemtemplate>
   	<span class="contentHeader1">
   		<%# DataBinder.Eval(Container.DataItem, "icDescription") %>
   	</span><br>
   	<table>
   		<!-- start child repeater -->
   		<asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server">
   			<itemtemplate>
   				<tr>
   					<td width="10px">&nbsp;</td>
   					<td>
   						<input type="checkbox" id="checkbox_ID" runat="server">
   					</td>
   					<td><%# DataBinder.Eval(Container.DataItem, "[\"iscDescription\"]")%> </td>
   				</tr>
   			</itemtemplate>
   		</asp:repeater>
   		<!-- end child repeater -->
   	</table>
   	<br>
   </itemtemplate>
</asp:repeater>


The Code-Behind:

Code:
		public void BindData()
		{
			// using nested repeater
			SqlConnection objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * " +
													 "FROM InterestCategory " +
													 "ORDER BY icDescription ASC", objConn);
													 
			DataSet ds = new DataSet();
			cmd1.Fill(ds, "InterestCategory");

			//Create a second DataAdapter for the Titles table.
			SqlDataAdapter cmd2 = new SqlDataAdapter("SELECT * " +
													 "FROM InterestSubCategory " +
													 "ORDER BY iscDescription ASC", objConn);
			cmd2.Fill(ds,"InterestSubCategory");

			//Create the relation bewtween the Authors and Titles tables.
			ds.Relations.Add("myrelation",
				ds.Tables["InterestCategory"].Columns["icID"],
				ds.Tables["InterestSubCategory"].Columns["icID"]);
			

			parentRepeater.DataSource = ds.Tables["InterestCategory"];
			Page.DataBind();
			//myRepeater.DataSource = ds;
			//myRepeater.DataBind();

			objConn.Close();
		}
 
Well, I suppose I will continue to answer my own questions in order to help those who may experience the same issues... but I'm stuck again: Checkboxes - selecting all items in a given category, and retrieving those for database storage on submit.

First of all - Success! I've adapted code from to C# and added a TemplateColumn containing a checkbox:

The ID is just there for now to show, as I'm going to want to retrieve those (when they're applied in the checkbox ID's later).

The resultant page shows:
Code:
Preference       ID   
Bar / Night Club        []
Dance Club       16  []
Lounge           17  []
Pre-Club Hangout 18  []  
Pubs             15  []  
Sports Bar       14  []  
Desserts                []
Cakes            106 []   
Cookies          110 []

Where [] is a checkbox.

Code:
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" OnItemDataBound="DataGrid1_ItemDataBound"Cellpadding="3">
<Columns>

	<asp:BoundColumn DataField="iscDescription" HeaderText="Preference"></asp:BoundColumn>
	<asp:BoundColumn DataField="iscID" HeaderText="ID"></asp:BoundColumn>
	
	<asp:TemplateColumn HeaderText="ChkBox">
		<HeaderTemplate>
			<asp:CheckBox id="chkAll" 
				AutoPostBack="false" ToolTip="Select/Deselect All"
				onClick="javascript:SelectAllCheckboxes(this);"
				runat="server">
			</asp:CheckBox>
		</HeaderTemplate>
		<ItemTemplate>
			<asp:CheckBox id="chkSelect" 
				AutoPostBack="false" ToolTip="Select/Deselect"
				onClick="javascript:HighlightRow(this);"
				runat="server">
			</asp:CheckBox>
		</ItemTemplate>
	</asp:TemplateColumn>																							
</Columns>
</asp:datagrid>

I added the Javascript code found at and the checkbox stuff from there to try to get this going - obviously it won't work yet (won't do what I need it to do).


Code-behind:

Code:
		public void BindData()
		{
			//using a DataGrid, adapted from [URL unfurl="true"]http://aspnet.4guysfromrolla.com/articles/072603-1.aspx[/URL]
			SqlConnection objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

			string CommandText = "SELECT icDescription, iscDescription, CAST(iscID AS varchar(50)) AS iscID " +
								 "FROM InterestSubCategory INNER JOIN InterestCategory ON InterestSubCategory.icID = InterestCategory.icID " +
								 "ORDER BY icDescription ASC, iscDescription ";

			SqlDataAdapter cmd1 = new SqlDataAdapter(CommandText, objConn);
			
			DataSet ds = new DataSet();
			cmd1.Fill(ds);


			string curCat = "";
			string prevCat = "";
			TableRow row = new TableRow();
			DataRow shRow;
			int i = 0;

			while (i <= ds.Tables[0].Rows.Count -1)
			{
				curCat = ds.Tables[0].Rows[i].ItemArray[0].ToString();
				
				if (curCat != prevCat)
				{
					prevCat = curCat;
					//shRow = new DataRow();
					shRow = ds.Tables[0].NewRow();
					shRow["iscDescription"] = ds.Tables[0].Rows[i].ItemArray[0];

					shRow["iscID"] = "SubHead";
					ds.Tables[0].Rows.InsertAt(shRow, i);

					i += 1;
				}

				i+=1;
			}


			DataGrid1.DataSource = ds;
			DataGrid1.DataBind();


		}

		public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.AlternatingItem | e.Item.ItemType == ListItemType.Item)  //check the row type
			{
				if (e.Item.Cells[1].Text.Equals("SubHead"))
				{
					// format data and align text to left cell to the left

					// set colspan=3
					e.Item.Cells[0].ColumnSpan = 3;

					// remove right two cells
					e.Item.Cells.RemoveAt(1);
					//e.Item.Cells.RemoveAt(0);
	
					//align to left and format text to bold and set bgColor
					e.Item.Cells[0].Attributes.Add("align", "Left");
					e.Item.Cells[0].Font.Bold = true;
					e.Item.BackColor = Color.FromArgb(204,0,0);

				}
			}

		}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top