nested repeater pass value in header template - asp.net

I have a nested repeater and i want to pass value in its header. Here is my code so far..
The main problem is the id of the control in header template is also coming from code behind.
<asp:Repeater ID="RptrProgCategory" runat="server">
<ItemTemplate>
<asp:Repeater ID="RptrPrograms" runat="server">
<HeaderTemplate><input type="hidden" id="<%= questvalue%>"/></HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "cat") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I want value in questvalue from code behind. Any idea how to achieve this?
Edit: I wanted to put this value in a DataTable and bind that value in Repeater bcoz i want output like this may be <%# DataBinder.Eval(Container.DataItem, "questvalue") %> instead of <%= questvalue%>..but in tht case i am not able to find the control
Category1(id of hidden field )
subcat1
subcat2
subcat3
Category2(id of hidden field)
subcat4
subcat5..and so on..

Repeater mainRepeater = this.Page.FindControl("RptrProgCategory") as Repeater;
Repeater nestedRepeater = mainRepeater.FindControl("RptrProgCategory") as Repeater;
You can then do a FindControl in nestedRepeater for questValue.
Add a runat='server' to questvalue so that you can access it in code behind.
I am writing this from memory, syntax might not be correct but it should get you off in the right direction.

set the id of the control in the repeater to something like mycontrolId - and then on OnItemDataBound - (or even OnItemCreated) use findcontrol("mycontrolId") - and then change the id of the control to your questvalue param.

Related

If statement inside Repeater control

Although there are several questions on this topic, I haven't found any satisfactory answers.
I have a Repeater that needs to display complex content. An IF statement is required within the template. I can't move this to the code-behind as I need to have server controls and user controls registered within the repeater. Here is what I need:
<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
<ItemTemplate>
<%# If CBool(Eval("IsFix")) Then%>
<%-- HTML content including server and user controls --%>
<%Else%>
<%-- HTML content including server and user controls --%>
<%End If%>
</ItemTemplate>
</asp:Repeater>
The above throws a compiler error. Any idea on how to achieve this? I need to evaluate the IsFix field in the If statement.
I would put each set of content within a server side panel, then on ItemDataBound event, set the visibility of one panel to true, and the other to false.
If visibility is set server side, then the front end content never even gets rendered.
Remove # from the code nuggets which is having if statement:-
<asp:Repeater ID="rCom" runat="server" ClientIDMode="Static">
<ItemTemplate>
<% CBool(Eval("IsFix")) Then%>
<%-- HTML content including server and user controls --%>
<%Else%>
<%-- HTML content including server and user controls --%>
<%End If%>
</ItemTemplate>
</asp:Repeater>
Edit:
Try with conditional operator (I am not sure about the syntax in VB.NET, please check):-
<%# If(CBool(Eval("IsFix")), "Do Something", "Else do something" %>
It works perfectly!!
<%# If(CBool(Eval("bitExtemporaneo")) = True, "<img src=""Imagenes/Extempo.png"" alt=""Extemporaneo""/>", "")%>
Remove # from the code where is having if statement.
Then use the code like this.
<%
object objIsFix = ((System.Data.DataTable)rep.DataSource).Rows[_nIndex]["IsFix"];
if (bool(objIsFix)) {%>
<%-- HTML content including server and user controls --%>
<%}Else{%>
<%-- HTML content including server and user controls --%>
<%}%>
'_nIndex' is defined in the cs file

WHy am I unable to add text along with <%# Container.DataItem %> in repeater in user control

I have a User Control which is dynamically placed by CodeBehind as follows:
Dim myControl As Control = CType(Page.LoadControl("~/Controls/mainMenu.ascx"), Control)
If InStr(Request.ServerVariables("url"), "/Login.aspx") <= 0 Then
mainMenu.Controls.Add(myControl)
End If
As per an example from my previous question on here.
Within this Control is a repeater which calls a database to generate values.
My Repeater mark-up is as follows
<asp:Repeater runat="server" ID="locationRepeater" OnItemDataBound="getQuestionCount">
<ItemTemplate>
<p id='locationQuestions' title='<%# Container.DataItem %>' runat='server'></p>
</ItemTemplate>
</asp:Repeater>
The example above works fine, but I want to be able to prepend text to <%# Container.DataItem %> in the title attribute of that <p> to print to the browser like this is some text DATA_ITEM_OUTPUT
When I try to do that though, it prints this is some text <%# Container.DataItem %> exactly like that, ie, turning <%# Container.DataItem %> into text, NOT the value from the repeater code.
It was working fine before I made it into a dynamically inserted control, so I am thinking I might have something being generated in the wrong order, but given that it works without any prepended text, I am stumped to fix it!
I'm new to .net and using vb.net, please could someone point me in the right direction?
It would help to see the broken version of your code, but I gather you're doing this:
<p id='locationQuestions' title='This is some text <%# Container.DataItem %>' runat='server'></p>
If that's the case, try it like this:
<p id='locationQuestions' title='<%# "This is some text " & Container.DataItem %>' runat='server'></p>
Following Statement include the server side code in your page code.
<%# Container.DataItem %>
When you Server side code on the page with some manipulation then put
it in single quote and code as you do code behind.
E.g.
Text = '<%# "Employee Name:" & Eval("EmpName") %>'
so you title markup code should be like this:
title='<%# "This is Title " & DataBinder.Eval(Container.DataItem, "Name") & "Text." %>'
Best of all, the Databinder.Eval syntax is the same for VB and C#.
check this link for more details.

Dynamically change the number of header cells in listview LayoutTemplate

I'm trying to dynamically create a listview. on reports.aspx user selects a bunch of checkboxes. On the next page, user sees reports.aspx, and should see a table with columns of the checkboxes he selected. My idea was to create a listview, then dynamically change the header row of the LayoutTemplate, then change the select statement depending on which columns selected. This is what I have:
<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
<table runat="server">
<tr runat="server">
<%
' Doesn't work because code blocks (<%%>) aren's allowed inside <LayoutTemplate> blocks
'For Each i As String In Request.Form
'Response.Write("<th>" & Request.Form(i) & "</th>")
'Next
%>
</tr>
</table>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</LayoutTemplate>
...
Problem is that this doesn't work because i can't put a code block (<%%>) inside the LayoutTemplate. Is there a way in the code behind to edit the LayoutTemplate, or another way to cycle through the Request.Form vars and build the table header row with it?
Thanks for any advice/direction!
-Russ
Try using the ItemTemplate for the binding syntax instead of the layout template. I believe the layout template is strictly for layout.
Also, it looks like you're using classic ASP code blocks. ASP.NET code blocks look like this:
For data binding:
<%# Eval("<COLUMN NAME>")%>
For other cases not involving data binding:
<%= Request.QueryString["Hello"] %>
Since the control is already a server side control, try giving the and id and then modifying the header on pre-render:
<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
<table runat="server">
<tr id='trCustomHeader" runat="server">
Then in your code behind, attach this logic to the listview's pre-render
ReportListView_PreRender(...)
{
TableRow tr = ReportListView.FindControl("trCustomerHeader");
TableCell tempCell = new TableCell();
tempCell.Text = ...
tr.Cells.Add(tempCell);
}
I just created a separate table on the page, outside of the listview, that was the simple way of doing it.
<asp:Table ID="HeaderTable" runat="server">
<asp:TableHeaderRow ID="HeaderTableHeaderRow" runat="server" />
</asp:Table>
<asp:ListView ...>
...
</asp:ListView>
Then in the code behind:
For Each i As String In Request.Form
If i.IndexOf("checkbox_") = 0 Then
Dim c As New TableHeaderCell()
Dim l As New LinkButton()
l.Text = i.Substring(Len("checkbox_"))
c.Controls.Add(l)
c.CssClass = "customreport"
HeaderTableHeaderRow.Cells.Add(c)
End If
Next
Pretty simple. So I didn't have to use a LayoutTemplate at all.

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)

Totally lost – data binding expressions inside GridView’s template

1) On aspx page we define GridView control named gvwPolls, and inside its template we define a user control named pollBox1
<asp:GridView ID="GridView1" DataSourceID="objPolls" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
Question is : <%# Eval("QuestionText") %> <br />
<mb:PollBox ID="PollBox1" runat="server" PollID='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="objPolls" ...></asp:ObjectDataSource>
a) I assume that inside gvwPolls’s template, the gvwPollBox1.DataBind is called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?!
b) Can someone offer some explanation how or why is gvwPollBox1.DataBind called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?
2) Continuing with the above example:
-- pollBox1 user control defines a Repeater control named rptOptions:
<asp:Repeater runat="server" ID="rptOptions">
<ItemTemplate>
<%# Eval("pollBoxTitle") %>
</ItemTemplate>
</asp:Repeater>
-- In pollBox1’s code-behind file we bind rptOptions to a data source inside DoBinding() method.
-- We also override pollBox1’s DataBind() method:
public override void DataBind()
{
base.DataBind();
DoBinding();
}
a) I assume that due to overriding pollBox1.DataBind(), the data binding expression <%# Eval("pollBoxTitle") %> ( defined inside rptOptions’s template ) will get evaluated prior to a call to DoBinding method? If so, won’t then <%# Eval("pollBoxTitle") %> get evaluated before rptOptions is actually bound to a data source?
b) If that is the case, how then is rptOptions able to extract value ( from data source’s pollBoxtitle property) from a data source, if at the time the <%# Eval("pollBoxTitle") %>
expression got evaluated, rptOptions wasn’t yet bound to any data source?
thanx
I can't explain why the page life cycle is the way that is, probably has something to do with rendering childs before the parent object. When exactly do you call .DataBind() in the PollBox control? Try to move it into an event that is later in the life cycle, like PreRender.
There is also another way to ensure it is working the way you want to:
Subscribe to the RowDataBound Event, use .FindControl("YourPollBoxID") to get the instance of the control current bound row, set the properties and perform a manuall .DataBind();

Resources