Cant find a Div inside of my child repeater - asp.net

Im trying to change the Visible of my Div, but im not being able to find it. Its seems because its inside of another Repeater.
<asp:Repeater ID="uxPesquisaList" runat="server" OnItemDataBound="uxQuestList_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<strong>
<%# DataBinder.Eval(Container.DataItem,"Descricao")%></strong>
</td>
</tr>
<%-- Listagem de Respostas --%>
<asp:Repeater ID="uxRespList" runat="server">
<ItemTemplate>
<tr>
<div id="uxRespostaText" visible="false" runat="server"> ***I want to display this Div***
<td>
<asp:TextBox ID="uxResposta" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Descricao")%>' />
</td>
</div>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
My Code Behind
protected void uxQuestList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptRespostas = (Repeater)(e.Item.FindControl("uxRespList"));
QuestionarioPergunta pergunta = (QuestionarioPergunta)e.Item.DataItem;
System.Web.UI.HtmlControls.HtmlContainerControl uxRespostaText = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("uxRespostaText"); **I try this, but it always return NullExpection**
if (pergunta.TipoPergunta == "Dissertativa")
{
uxRespostaText.Visible = true;
}
rptRespostas.DataSource = ctx.QuestionarioRespostas.Where(x => x.PergId == pergunta.Id).ToList();
rptRespostas.DataBind();
}
}

You have to look for the TextBox in each item of the inner Repeater. You can set the ItemDataBound event handler of uxRespList in the markup:
<asp:Repeater ID="uxRespList" runat="server" OnItemDataBound="uxRespList_ItemDataBound">
In code-behind, you bind the data of that inner Repeater in the event handler of the outer Repeater (as you already do in your code):
protected void uxQuestList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptRespostas = e.Item.FindControl("uxRespList") as Repeater;
...
rptRespostas.DataSource = ...
rptRespostas.DataBind();
}
}
And you process each inner Repeater item after its data is bound:
protected void uxRespList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox uxRespostaText = e.Item.FindControl("uxRespostaText") as TextBox;
...
}
}

Related

How do I display child footer of a repeater if the child repeater is empty?

I have two repeaters nested in my application which is working fine. I would love to display the footer if the child repeater is empty. Due to my code my long html, i will just drop a sample of what my html looks like and post my full code for better understanding. Everything works fine though unless when the child repeater is empty i want to display the footer message
<asp:Repeater ID="ProductRepeater" runat="server" OnItemDataBound="ProductRepeater_ItemDataBound">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%#Eval("Name")%>
</td>
<tr>
</table>
<pre>
<asp:Repeater ID="ChildRepeater" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%#Eval("Description")%>
<td>
</tr>
<table>
</itemTemplate>
<FooterTemplate>
<div>
<h5>
<asp:Label ID="lblDefaultMessage" runat="server" Text="This is empty. " Visible="false" ForeColor="Red" Font-Size="Large">
</asp:Label>
</h5>
</div>
</FooterTemplate>
</asp:Repeater>
</pre>
</ItemTemplate>
</asp:Repeater>
<protected void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string category = "Value"
Repeater rptRelated = e.Item.FindControl("ChildRepeater") as Repeater;
rptRelated.DataSource = getChild(category);
rptRelated.DataBind();
if (rptRelated.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblDefaultMessage = (Label)e.Item.FindControl("lblDefaultMessage");
lblDefaultMessage.Visible = true;
}
}
}
}
Repeater has no EmptyDataTemplate like GridView.
You should place your footer content next to Repeater and change your "databinding" code like this:
Repeater rptRelated = e.Item.FindControl("ChildRepeater") as Repeater;
Label lblDefaultMessage = (Label)e.Item.FindControl("lblDefaultMessage");
var ds = getChild(category);
lblDefaultMessage.Visible = ds != null && ds.Rows.Count != 0;
if (rptRelated != null)
{
rptRelated.DataSource = ds;
rptRelated.DataBind();
}
Find Repeater like:
// find Child Repeater
Control ctrl = (sender) as Control;
Repeater rptRelated = ctrl.Parent.NamingContainer as Repeater;
And Find control from Repeater's Footer like:
Label lblDefaultMessage =
(Label)rptRelated.Controls[rptRelated.Controls.Count-1].FindControl("lblDefaultMessage");
Add this into child Repeater's OnItemDataBound event instead of parent.
protected void ChildRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Control ctrl = (sender) as Control;
Repeater rptRelated = ctrl.Parent.NamingContainer as Repeater;
if (rptRelated.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblDefaultMessage = (Label)rptRelated.Controls[rptRelated.Controls.Count-1].FindControl("lblDefaultMessage");
lblDefaultMessage.Visible = true;
}
}
}
Event is:
<asp:Repeater ID="ChildRepeater" runat="server"
OnItemDataBound="ChildRepeater_ItemDataBound">

Repeater inside repeater not binding properly

I have a repeater inside another. Like so :
<asp:Repeater ID="CategoryRepeater" runat="server" OnItemDataBound="ItemBound">
<ItemTemplate>
<div class="groupbox">
<fieldset>
<legend><%# Container.DataItem %></legend>
<table>
<asp:Repeater ID="ItemRepeater" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox id="chkItem" runat="server" Text='<%# Eval("Text")%>' />
<asp:Button ID="btnXRefs" Text="x-refs" runat="server" CssClass="xRefButton" OnClick="btnSelectXRefs_Click" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</fieldset>
</div>
</ItemTemplate>
</asp:Repeater>
CODE BEHIND :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (var db = new DbContext())
{
CategoryRepeater.DataSource = db.Categories.OrderBy(x => x.Heading).Select(x => x.Heading).Distinct();
CategoryRepeater.DataBind();
}
}
}
protected void ItemBound(object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item)
{
Repeater childRepeater = (Repeater)args.Item.FindControl("ItemRepeater");
var item = args.Item as RepeaterItem;
using (var db = new DbContext())
{
childRepeater.DataSource = db.Categories.Where(x => x.Heading == item.DataItem).OrderBy(x => x.Text);
childRepeater.DataBind();
}
}
}
My goal is to first make many groupboxes using the top level, then bind items into each one. So I end up with many small stacked lists of checkboxes.
Problem is, all the top level boxes appear, yet only the first one contains checkbox items, ie. only the first one is bound internally, and the ItemBound method is only called once for the first one.
Any ideas why?
This line
if (args.Item.ItemType == ListItemType.Item)
Should be like this
if(args.Item.ItemType = ListItemType.Item ||
args.Item.ItemType == ListItemType.AlternatingItem)

how to find repeater inside another repeater

I want to find repeater inside the another repeater. But i m not able to find. My code is
<asp:Repeater ID="rep_test" runat="server">
<ItemTemplate>
<div id='h<%# DataBinder.Eval(Container, "ItemIndex") %>' class="header" onclick='ToggleDisplay(<%# DataBinder.Eval(Container, "ItemIndex") %>);'>
<%#DataBinder.Eval(Container.DataItem, "ID")%>
</div>
<div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">
<asp:Repeater ID="rep_hello" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "batchid")%><br />
<%#DataBinder.Eval(Container.DataItem, "ts")%><br />
</ItemTemplate>
</asp:Repeater>
<%-- <%#DataBinder.Eval(Container.DataItem, "batchid")%><br />
<%#DataBinder.Eval(Container.DataItem, "ts")%><br />--%>
</div>
</ItemTemplate>
</asp:Repeater>
If you put a repeater inside an item template of another repeater that means that every item of the main repeater (rep_test) will have a repeater inside it (rep_hello). So you actually need to find the repeater inside a repeaterItem. You can iterate trough all the nested repeaters like this:
foreach (RepeaterItem item in rep_test)
Repeater rptr = (Repeater)item.FindControl("rep_hello");
Example:
In ItemDataBound event handler:
protected void rep_test_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
(e.Item.FindControl("rep_hello") as Repeater).DataSource = YourOtherDataSource;
}
}
You can try using .FindControl(). In VB, it would be something like
Dim rpt as Repeater = rep_test.FindControl("rep_hello")
Usually when I see this kind of thing, you want to perform some event on all of the inner repeaters. What I usually do is handle this kind of thing inside the ItemDataBound event.
Add an OnItemDataBound attribute to your Repeater.
<asp:Repeater ID="rep_test" runat="server"
OnItemDataBound="rep_test_ItemDataBound">
Then in the back end, add a handler, with a FindControl call.
protected void rptBasket_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater innerRepeater = (Repeater)e.Item.FindControl("rep_hello");
// Now your have your repeater...do what you want with it.
}
}

add style for item in datalist

I had Datalist and I want to give item style when I click on it to show user the he select this item I did my code but when I selected item It didnot have any style
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
e.Item.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
e.Item.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Item.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.DataList3, "Select$" + e.Item.ItemIndex);
}
e.Item.Attributes are not rendered as html, they just don't show up. Add some kind of Control, a panel for example:
<asp:DataList ID="DataList3" runat="server">
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server">
<%#Container.DataItem%>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
And then alter your ItemDataBound event
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel p = (Panel)e.Item.FindControl("Panel1");
p.Attributes.Add("onmouseover", "this.style.cursor='hand';this.style.textDecoration='underline';");
p.Attributes.Add("onmouseout", "this.style.textDecoration='none';");
// ?? p.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.DataList3, "Select$" + e.Item.ItemIndex);
}
}
Or an even better solution for the style part of your question is using css and a classname, add <ItemStyle CssClass="item" /> to your datalist.
css:
.item{cursor:pointer;text-decoration:underline;}
.item:hover{text-decoration:none;}
The "onlick" should be handled by a LinkButton if you ask me, but I don't exactly understand what you are trying to do with that.

How can I obtain a reference to a control created inside a repeater?

I have one control named thumbviewer inside repeater. I want to set its imageurl in
code. Currently it's done in aspx itself as
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<span style="padding:2px 10px 2px 10px">
<bri:ThumbViewer Id="Th1" runat="server" ImageUrl='<%# Eval("Name", "images/{0}") %>' Height="100px" Width="100px"/>
</span>
</ItemTemplate>
</asp:Repeater>
How can i set ImageUrl in code?
protected void rpter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
ThumbViewControl control = e.Item.FindControl("Th1") as ThumbViewControl;
if (control != null)
{
control.ImageUrl = "";
}
}
}
and on the aspx
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="rpter_ItemDataBound" >
<ItemTemplate>
<span style="padding:2px 10px 2px 10px">
<bri:ThumbViewer Id="Th1" runat="server" Height="100px" Width="100px"/>
</span>
</ItemTemplate>
</asp:Repeater>
Is how I would personally do it.
If you wish to get the data for it, at that point, I believe e.Item.DataItem (or something similar) get its.
Cheers,
T
Your repeater has a onitemdatabound event.
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"></asp:Repeater>
In your code behind you can have an Event handler called
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// ensure that we only look in the item template for our control
if( e.Item.ItemType == ListItemType.Item)
{
ThumbViewer tv = (ThumbViewer)e.Item.FindControl("Th1");
tv.ImageUrl = "images/"+((<The object type you are binding>)e.Item.DataItem).Name;
}
}
This is the most orthodox way to access data being bound to a repeater. IMHO
HTH
You need to find the repeater and then look through the controls:
I do a similar thing here and load a control into a placeholder inside of a repeater..
if (ResultRepeater != null && ResultRepeater.HasControls())
{
foreach (Control oControl in ResultRepeater.Controls)
{
if (oControl != null && oControl is RepeaterItem)
{
PlaceHolder oSuggestMorePlaceholder = (PlaceHolder) oControl.FindControl("SuggestMorePlaceholder");
if (oSuggestMorePlaceholder != null)
{
SuggestMoreTabbedControl oTabbedControl = (SuggestMoreTabbedControl) Page.LoadControl("controls/SuggestMoreControl.ascx");
if (oTabbedControl != null)
{
oSuggestMorePlaceholder.Controls.Add(oTabbedControl);
}
}
}
}
}

Resources