Nested CheckBoxList using DataSet and Repeaters - asp.net

I'm trying to wire up some CBL's using a repeater and dataset. The dataset contains 2 tables with the same schema and a (one, single) relation (in SQL land think of it as a self join).
When the control is rendered if I set my DataSource = the relationship, I am able to get the children elements to show; so I know the model is good, although misplaced (on purpose to test the model - see code below).
The problem is:
I am having difficulty getting the parent elements to show up at all
Not all parents have children, and the ones that don't still need to show up
Am I approaching this in the right frame of mind? i.e. Am I missing something fundamental? Implementation outside of CBL(plain text) works fine per this article
<asp:Repeater ID="ParentRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="ParentCBL" runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem,"Joined") %>'
DataTextField="TextProperty"
DataValueField="ValueProperty">
</asp:CheckBoxList>
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="ChildCBL" runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem, "Joined") %>'
DataTextField="TextProperty"
DataValueField="ValueProperty">
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Page Load is nothing spectacular
DataSet ds = Foo.foo();
ParentRepeater.DataSource = ds.Tables["Parent"];
ParentRepeater.DataBind();

In your code you don't bind ChildRepeater
If you want just change plain text to Checkbox in according to https://support.microsoft.com/en-us/kb/306154
then you don't need to use CheckBoxList. But you should only use single checkbox inside repeater.

Related

ASP literal inside a repeater footer template

I think this is not possible by trying to add a literal into a footer template of a repeater so that I can fill it later on...
<FooterTemplate>
<asp:Literal ID="panelFooter" runat="server"></asp:Literal>
</FooterTemplate>
You can data bind the Text attribute to the result of a method in your code-behind. In this example we are going to display the total number of products in the footer:
<FooterTemplate>
Total number of products:
<asp:Literal runat="server" Text='<%# GetTotalNumberOfProducts() %>' />
</FooterTemplate>
In the code-behind we are going to create a method that calculates the number and returns it for the Literal control.
protected string GetTotalNumberOfProducts()
{
return 42.ToString();
}
Notice that we don't need to find the control by its ID. We are just returning s string and the data binding syntax will call our method in code-behind and put the result inside the control.

asp.net repeater with usercontrol - setting commandargument

I have the following repeater code:
<asp:Repeater ID="repMain" runat="server" OnItemCommand="repMain_ItemCommand" EnableViewState="false">
<ItemTemplate>
<dmg:testcontrol runat="server" MyData=<%#Container.DataItem %>>
</dmg:testcontrol>
</ItemTemplate>
</asp:Repeater>
The testcontrol usercontrol looks like:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestRepeater.TestControl" %>
<asp:Literal runat="server" ID="litMain" Text="<%#MyData.MyValue %>"></asp:Literal>
<asp:DropDownList runat="server" ID="dropdownMain"></asp:DropDownList>
<asp:Button runat="server" ID="btnMain" Text="Click Me" CommandName="Update" CommandArgument="<%#dropdownMain.SelectedValue%>"/>
Is it possible for me to send through the dropdownMain.SelectedValue as the CommandArgument?
Just now it is an empty string.
Thanks
Duncan
PS This is related to ASP.NET Repeater not binding after ItemCommand but I thought the two sufficiently different to keep apart.
A bit old question but I just my self found the answer to this one.
CommandArgument="<%#dropdownMain.SelectedValue%>"
Needs to look like this instead with single quotes! All inline codes within a asp.net controls have to be done this way instead.
CommandArgument='<%#dropdownMain.SelectedValue%>'
Why not get the selected value, and use it inside the Command function ?
(why to try to send it as argument, from the moment you can get it inside the command called function and its the same)

Moving repeater in form removes serverside declaration

I have two repeaters bound to object collections. They both work fine. But when I move repeater B to repeater A's FooterTemplate the serverside declaration of repeater B disappear.
This is not a parent/child relation.
Any ideas why this happens?
Update:
<asp:Repeater ID="myOuterRepeater" runat="server">
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
<asp:Repeater ID="myInnerRepeater" runat="server">
<ItemTemplate>
<asp:Button OnCommand="btnRemove_Click" ID="btnRemove" RunAt="server" />
<%#Eval("ItemId")%>
<%#String(Eval("Amount"))%>
<ItemTemplate>
</asp:Repeater>
</FooterTemplate>
</asp:Repeater>
When I bind:
myOuterRepeater.DataSource = myCollection
myOuterRepeater.DataBind()
Dim innerRepeater As Repeater =
myOuterRepeater.Controls(myOuterRepeater.Controls.Count - 1).Controls(0).FindControl("myInnerRepeater")
innerRepeater.DataSource = myInnerCollection
innerRepeater.DataBind()
When nesting a control (including a second repeater) inside a repeater, it is not longer part of the page but part of the enclosing repeater.
You will need to use FindControl(string) to get a reference to the nested repeater:
Repeater nested = enclosingRep.FindControl("nestedRepeaterId");

How do I data bind a drop down list in a gridview from a database table using VB?

So in this gridview, there is a column for status and I want to have a drop down list with Pass, Pending, Fail appear when the edit button is clicked. These values are already in a table, so I need to somehow bind from this table to each ddl for every row.
Here is the column from the gridview. As you can see, I would like to just have a label showing when not in edit mode, and a ddl when the edit button is pressed
<asp:TemplateField HeaderText="During Production Status" SortExpression="DuringProductionStatus">
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" datavaluefield="Name"
datatextfield="Name" DataSource="*What goes here?*"> />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text='I don't understand how to get this from the ddl' />
</ItemTemplate>
</asp:TemplateField>
For clarity's sake, my table is named Status, and the database is named DirectImport
There's a few steps to go through here - none of them are particularly difficult, but they can be a bit fiddly (IMHO). The good news is once you've got this working once, it gets easier to do it again!
I'm assuming you've got a <asp:*DataSource> control on your page - my preference is for an ObjectDataSource but I don't think it matters, I think a SqlDataSource works equally well. I've never tried doing this with GridView.DataSource = MyDataSet in code-behind, so I don't know whether that would work or not, but my assumption is that it wouldn't as you wouldn't get the proper two-way binding that you want. This data source feeds your grid with your main data. The key point here is that your database query must return both the Status text field and the Status id.
So your gridview will now look something like:
<asp:objectdatasource runat="server" id="MainDataSource" ... />
<asp:gridview runat="server" id="MyGridView" DataSourceID="MainDataSource">
<Columns>
<asp:TemplateField HeaderText="During Production Status" SortExpression="DuringProductionStatus">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text="<%# Bind('Status') %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
The Text="<%# Bind('Status') %>" is the bit you're missing to get the status text into the grid.
Now add a second data source into your markup that reads in the set of values from the Status table.
<asp:objectdatasource runat="server" id="StatusObjectDataSource" ... />
And add the EditItemTemplate into the GridView, which is bound to the Status DataSource.
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" datavaluefield="StatusID"
datatextfield="Status" DataSourceID="StatusObjectDataSource"
SelectedValue="<%# Bind('StatusId') %>" />
</EditItemTemplate>
The SelectedValue="<%# Bind('StatusId') %>" is what connects up the two datasets so that when you flip a row into Edit mode, the dropdownlist has the correct item already selected, and when you then save it you've got the Status ID to put into your database.
And you're done.
I have used the RowDataBound event. Here is a small code snippet. HTH
you would have an ItemTemplate in your aspx/ascx
<asp:TemplateField HeaderText="Column Headings">
<ItemTemplate>
<asp:DropDownList ID="ddlName" runat="server" Width="150"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
and in your code behind, you will have
protected void grdDataMap_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlName");
ddl.DataSource = someList;//the source of your dropdown
ddl.DataBind();
}
}
so when you bind your grid with grdDataMap.Databind (assuming your grid id is grdDataMap), row databound event will be called for each row (including header/footer, and thats the reason you check RowType)
so you can probably decide what controls/columns to hide/show/bind inside this row databound event
In the winforms world I pull my objects from the DB into a List(Of Whatever) and use the list as the datasource.
This also lets me add extra "convenience" fields in the object so that I can populate it with stuff from other tables.
I don't know asp.net at all so if you can do something similar, it might help.
A really quick solution is to create a custom web control for the status dropdown. The control will always contain the same data. When it renders you populate the datasource. When it gets added to the gridview, the data will be in the drop down. Hope that helps!

ASP.NET Event Handler for label in Datalist

I'm working on an ASP.NET page, and have a DataList with a non-visible label, ID, inside of it. The Datalist is populated by Query A.
I'm trying to add a handler in my VB code where it will run Query B, populating a different label in the Item Template, after ID is changed. I figure I need to go somewhere in the DataList, but poking through Intellisense and a google search weren't successful. Does anyone know how I get to that label? Sorry if this is a dumb question, and thanks for the help.
Edit: I see how I can access the datalist items while inside the function, but how do I do it for only one part of the DataList control in the Event Handler? All the options I'm seeing are related to full events involving the Data List, not a single label changing. Thanks.
Edit 2:
I figured I'd add some code, to better explain exactly the exact issue I'm having.
<asp:DataList ID="DataList1" runat="server" DataSourceID="Omitted,Ilikemyjob">
<ItemTemplate>
<asp:Label ID="FromLabel" runat="server" Font-Size="Small" Text='<%# Eval("IncdntDate") %>'></asp:Label><br />
<asp:Label ID="ToLabel" runat="server" Font-Size="Small" Text='<%# Eval("Roadway") %>'></asp:Label><br />
<asp:Label ID = "lblCrossroad" runat ="server" Font-Size = "Small" Text = '<%# Eval("Crossroad") %>'></asp:Label><br />
<asp:Label ID = "lblRdwyID" runat ="server" Font-Size="Small" Visible = "false" Text = '<%# Eval ("RdwyID") %>'></asp:Label>
<asp:Label ID = "DistanceLabel" runat ="server" Font-Size = "Small" Text = '<%# Eval("RptTime") %>'></asp:Label><br />
<asp:Label ID = "lblTTime" runat ="server" Font-Size = "small" visible ="false"></asp:Label>
<hr />
</ItemTemplate>
</asp:DataList><br />
There's the DataList thing I'm doing, and I have a different query that I want to store the value of to lblTTime if lblRdwyID is set to a certain range of values, thought it will not always be set to a value.
I'm trying to set up an event that will trigger when lblRdwyID is set, that will launch the other query and set the value of that templated items lblTTime and make it visible. Is there a good way to do this? I tried messing around with DataList1's events, but couldn't get anything to do the trick.
If your label has its visibility set to false, it won't render on the screen (as opposed to having a div block with visibility set to hidden), and so I wager that is why you can't actually find the control after the binding has occurred.
Have you tried using the DataKey property to store the ID instead? It is easily accessible at the row level and you can store additional data if needs be.
Assuming you have the row, you use:
row.FindControl("mylabelid")

Resources