Getting the id of a repeater field in asp.net - asp.net

I am faceing a problem. I have a image view that contains a repeater. What i want to do is when the last image is shown the site needs to be redirected but i dont know how to get the current field that is shown. I was thinking that i could make a variable that holds the current id and compare it every time a new image is shown. And if the new images id is lower than the one in the other variable it has to redirect.
At the moment i dont really have any code to post because i really don know how to solve this. Hope some of you can help.
This is the code for the repeater on the html site
<div id="imgBack" runat="server" class="row" style="background-color: black;">
<div class="col-lg-12">
<div class="container">
<div class="fotorama" data-arrows="false" data-maxheight="750" data-allowfullscreen="native" data-loop="false" data-ratio="1024/750" data-nav="false" data-autoplay="5000" data-click="false" data-swipe="false" data-stopautoplayontouch="false" data-transition="crossfade" data-shuffle="true" data-fit="contain">
<asp:Repeater ID="repImgs" runat="server" OnItemDataBound="repImgs_ItemDataBound">
<ItemTemplate>
<asp:Image ID="imgs" ImageUrl='<%# string.Format("~/Images/{0}", Eval("FileName")) %>' runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</div>
</div>
And here is what i got on the back end so far
protected void repImgs_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.ItemIndex == repImgs.Items.Count - 1)
{
Response.Redirect("FinishedSession.aspx");
}
}
}

To get the last item in your repeater you could use ItemDataBound
protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.ItemIndex == MyRepeater.Items.Count - 1)
{
// Do something
}
}
}

Related

Cant find a Div inside of my child repeater

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;
...
}
}

Number of items in result in ASP repeater with Sitecore

I'm working with Sitecore ascx file, and got something like this:
<asp:Repeater ID="NavRepeater" runat="server">
<ItemTemplate>
<div class="item">
<img src="<%# ((Sitecore.Data.Items.Item)Container.DataItem).Fields["Image Url"] %>" width="360" height="420" alt="">
</div>
</ItemTemplate>
<SeparatorTemplate></SeparatorTemplate>
</asp:Repeater>
Can't figure out, how to get total number of elements from Sitecore.Data.Items.Item.
I was also wondering how to retrieve given item in collection - like item no 2 ?
It's on the data source of your repeater in C#:
So if your C# is:
NavRepeater.DataSource = someData;
Then the length is someData.Length or someData.Count() based on its type.
For getting a specific item at a location:
Item second = someData[1]; // per 0-indexing
Or based on type, can also be:
Item second = someData.ElementAt(1) // per 0-indexing
You can have something like :
<asp:Repeater ID="NavRepeater" runat="server" OnItemDataBound="rptGallery_ItemDataBound">
<ItemTemplate>
<div class="item">
<sc:fieldrenderer id="fldGalleryItemImageThumbnail" runat="server" fieldname="ImageUrl" disablewebediting="true" />
</div>
</ItemTemplate>
<SeparatorTemplate></SeparatorTemplate>
</asp:Repeater>
On Page_Load you will have:
protected void Page_Load(object sender, EventArgs e)
{
rptGallery.DataSource = listofitemyyouwanttoshow;
rptGallery.DataBind();
}
On rptGallery_ItemDataBound event you will have:
protected void rptGallery_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Item item = (Item)e.Item.DataItem;
//you can check index here of item
if (item != null)
{
var fldGalleryItemImageThumbnail = e.Item.FindControl("fldGalleryItemImageThumbnail") as FieldRenderer;
if (fldGalleryItemImageThumbnail != null)
{
fldGalleryItemImageThumbnail.DataSource = item.ID.ToString();
fldGalleryItemImageThumbnail.Parameters = String.Format("width={0}", 360);
}
}
}
}
}
I hope it helps you .
You would get these from the enumerable that you are binding you repeater to.

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);
}
}
}
}
}

Using event of control under datalist

I have a datalist control which some controls(ex: button) are in it. I want to write some code into click event of button which is in the datalist control. But in the code behind page I can't see the name of controls into datalist. How can I solve this problem?
Attach your event to the controls in the OnItemCreated event of the datalist.
EDITED TO ADD SAMPLE
private void DataList_ItemCreated(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button btn = (Button)e.Item.FindControl("btnWhatever");
if (btn != null) btn.Click += new EventHandler(SomHandler);
}
}
If you don't want to add a handler to all the child events, you could instead add your code to the OnItemCommand.
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<asp:Button ID="btnDoSomething" Runat=server CommandName="DoSomething"
CommandArgument="<%# DataBinder.Eval(Container.DataItem, "SomeID")
%>"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
protected void DataList1_ItemCommand(
object source, DataListCommandEventArgs e)
{
if (e.CommandName == "DoSomething")
{
//Do stuff
}
}

Resources