Iterate Over Controls in Repeater - asp.net

I have a bit of code that determine whether or not a control (within a repeater) should be visible or not and I want to call this on Page_Load but I can't seem to get the Controls inside a repeater.
<asp:Repeater ID="repreat" runat="server" >
<HeaderTemplate>
<asp:PlaceHolder runat="server" ID="thActivePrimary">Blah</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="PlaceHolder1">Blah</asp:PlaceHolder>
</HeaderTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="trActivePrimary">Blah</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="thActivePrimary2">Blah</asp:PlaceHolder>
</ItemTemplate>
</asp:Repeater>
repreat.Controls is always empty.
How do I achieve this?

foreach (RepeaterItem ri in repeat.Items)
ri.FindControl("thActivePrimary").Visible = false;
This should work

The controls are not created at page load, They are created when databind is called. If you want to access each item as they are created have a look at the DataBound event of the repeater.
Or bind the visible attribute to your datasource

Related

AsyncPostBackTrigger in nested repeater

Good day everyone,
This is my first post in here and I would like to thank you all for the great efforts in this forum by which I have already gaind a lot of skills.
I have a smalle issue with two nested repeaters. Basically, I have a dropdownlist in a child repeater which contains rating values and every time the dropdownlist is changed in the child repeater the new percentange is calculated and presented in a label in the parent repeater. This will cause full postback which is really frustrating when going through too many dropdownlists. My question is how to reflect the new calculated percentange in the label without postback. I have tried to use AsyncPostBackTriggers but no luck. Any suggestions would be appreiciated
<asp:Repeater ID="rptParent" runat="server">
<ItemTemplate>
<asp:Label ID="lblAvg" runat="server" Text='<%# Eval("TrialScore")%>'></asp:Label>
<asp:Repeater ID="rptChild" runat="server">
<ItemTemplate>
<asp:DropDownList ID="lstRate" runat="server" OnSelectedIndexChanged="lstRate_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Wrap your aspx mark up inside update panel like this.
<asp:UpdatePanel runat="sever" ID="upParentChild" >
<ContentTemplate>
<asp:Repeater ID="rptParent" runat="server">
<ItemTemplate>
<asp:Label ID="lblAvg" runat="server" Text='<%# Eval("TrialScore")%>'></asp:Label>
<asp:Repeater ID="rptChild" runat="server">
<ItemTemplate>
<asp:DropDownList ID="lstRate" runat="server" OnSelectedIndexChanged="lstRate_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
This will make sure that only controls inside update panel are posted back and not the whole page.

Asp.net page still flickers with UpdatePanel

I have a page which contains a Listview,. The ItemTemplate will contain many Checkboxes for each returned value from it's datasource. These Checkboxes has a OnCheckedChanged function and when a user selects the checkbox, the page flickers...I have investigated UpdatePanels, but to no avail. Also, this page contains a MasterPage.
Code:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListView ID="lvTypes" runat="server" DataSourceID="XXX" GroupItemCount="4">
<ItemTemplate>
<td style="background-color: #4b6c9e;" align="left">
<asp:CheckBox ID="Type" runat="server" Text='<%# Eval("Type") %>' ForeColor="White" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="True" CssClass="myCheck" Font-Size="45px" />
</td>
...
...
...
Obviously, I am not grasping the concept of the UpdatePanel. Can you direct me on how to implement this for this scenario"?
Thanks.
If you use UpdateMode = "Conditional" it'll do a partial refresh.
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode = "Conditional" runat="server">
Read MSDN to get an idea of the UpdatePanel control.
And, also have a look at this comprehensive explanation in MSDN

cant create a SelectedIndexChanged event for dropdownlist in gridview FooterTemplate

I have a gridview with a dropdownlist for the products description in the footer template.
There is no way to create a SelectedIndexChanged in the IDE and writing it out manually produces an error? How to create code to handle the Selection change? I need to populate the product ids when the product description is selected.
"EDITED"
I tried using the gridview rowediting event assuming that if a row item was changed (ie, a new selection in the dropdown, it would fire, but it doesn't) It appears a gridview event has to be fired when that dropdown list changes, that's where i need the code to go. Any ides on what event?
Here is what the template field markup is:
<asp:TemplateField HeaderText="description" SortExpression="description">
<FooterTemplate>
<asp:DropDownList ID="ddlProductDesc" runat="server" DataSourceID="edsProductDesc" DataTextField="description"
OnSelectedIndexChanged="ddlProductDesc_SelectedIndexChanged">
</asp:DropDownList>
<%--<asp:TextBox ID="tbInsertdescriptiton" Width="350" runat="server"></asp:TextBox>--%>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblProdDesc" runat="server" Text='<%# Bind("description")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Take a look at this:
http://www.c-sharpcorner.com/blogs/7228/asp-net-gridview-dropdown-with-related-records-in-textbox.aspx

How to obtain Container.DataItem from parent control?

Suppose that I create an ASP.net repeater. Suppose further that I put an ASP.net GridView in the ASP.net repeater's ItemTemplate.
Suppose that it looks like this:
<asp:Repeater runat='server' id='myRepeater'>
<ItemTemplate>
<%# "This Repeater DataSource is " + Container.DataItem.ToString() %>
<asp:GridView runat='server' id='repeaterGridView'>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<% // I would like to refer to the Repeater Data Source here, but I only know
// how to refer to the GridView Data Source here.
<ItemTemplate>
</asp:templateField>
</Columns>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I would like to refer to the Repeater DataSource inside a TemplateField for the GridView. How can I do this?
Container refers to the TemplateField, so try calling
((RepeaterItem)Container.NamingContainer.NamingContainer).DataSource

ASP.NET change a Repeater's DataSource without a postback?

I have a repeater that displays some data from a SQL query:
<asp:Repeater runat="server" ID="damQuickList" OnItemDataBound="damQuickList_OnItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink runat="server" ID="damAnchor" /></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
In the codebehind:
damQuickList.DataSource = (Data.RunSelectQuery("SELECT * FROM Table ORDER BY " + radioButton.Value));
damQuickList.DataBind();
Is there a way to change the DataSource and have it update in the Repeater, without having to do a postback in the page (like how AJAX does it)? I've been using the Async controls I found here: http://www.asynccontrols.com/, but there's some issues using them with IE6/7.
Use the ASP.NET AJAX components. Put a ScriptManager on your page, then put a UpdatePanel on your page. Inside the update panels ContentTemplate put your repeater.
A quick example would look something like this...
ASPX Markup
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td>
<%# Eval("Data") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
C# Code Behind
protected void Button1_Click(object sender, EventArgs e)
{
Repeater1.DataSource = yourDataSource;
Repeater1.DataBind();
}
Note the button that 'refreshes' your data source is also inside the content template. Hope it helps.
Off-topic: I hope that's not your real code; I'm pretty sure a malicious user could put anything they wanted into the radioButton.Value field and SQL-Inject you.
Do you mean a full round trip? Can you not put the repeater inside an UpdatePanel?

Resources