I use asp.net and c#4.
I have a repeater within inside a HyperLink Control.
I need to Find the HyperLink Control and change some of its properties with some logic before rendering it on the page.
With my code here posted I get a null value for the control so I'm not able to get it.
Any idea what I'm doing wrong? Thanks for your time on this.
<asp:Repeater ID="RepeaterEditorsChoice" runat="server" DataSourceID="ObjectDataSourceEditorsChoice"
OnItemCreated="RepeaterEditorsChoice_ItemCreated" OnItemDataBound="RepeaterEditorsChoice_ItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="uxLink" runat="server"></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
CODE BEHIND:
protected void RepeaterEditorsChoice_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
HyperLink myLink = (HyperLink)((Repeater)sender).FindControl("uxLInk"); // ERROR: it is null
dynamic o = e.Item.DataItem;
if (o.TypeContent == "AR")
{
myLink.Text = #"'<%# Eval(\""Title\"") %>'";
myLink.NavigateUrl = #"'<%# GetRouteUrl(""ArticleDetails"", new {ContentId = Eval(""ContentId""), TitleUrl = Eval(""TitleUrl"")}) %>'";
}
if (o.TypeContent == "BP")
{
myLink.Text = #"'<%# Eval(\""Title\"") %>'";
myLink.NavigateUrl = #"'<%# GetRouteUrl(""BlogPostDetails"", new {ContentId = Eval(""ContentId""), TitleUrl = Eval(""TitleUrl"")}) %>'";
}
}
}
On item data bound
then just (YourClass)e.Item.FindControl("its name");
Related
I have been getting the NullrefernceException on my aspx.cs page even though I already assigned a text for the label in my aspx page. At first i thought it could be the session but i log in and try i still get the same error. I have checked my database and the spelling and there is nothing wrong
My aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
Label Lefthowmanylabel =(Label)DataList1.FindControl("Lefthowmanylabel");
Label quantitylabel = (Label)DataList1.FindControl("quantitylabel");
if (int.Parse(quantitylabel.Text) < 11)
{
Lefthowmanylabel.Visible = true;
}
else
{
Lefthowmanylabel.Visible = false;
}
}
My datalist item in aspx page:
<asp:Label ID="Lefthowmanylabel" runat="server" Text="Only 10 Left!! While stock last!" Visible="False"/>
<asp:Label ID="quantitylabel" runat="server" Text='<%# Eval("Quantity") %>' Visible="False" />
</td>
If you want to access access controls inside DataList and manipulate them, you want to use ItemDataBound.
For example,
<asp:DataList ... OnItemDataBound="Item_Bound" runat="server">
</asp:DataList>
void Item_Bound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lefthowmanylabel =(Label)e.Item.FindControl("Lefthowmanylabel");
Label quantitylabel = (Label)e.Item.FindControl("quantitylabel");
if (int.Parse(quantitylabel.Text) < 11)
{
lefthowmanylabel.Visible = true;
}
else
{
lefthowmanylabel.Visible = false;
}
}
}
I am trying to get a page to display information in a row layout using repeaters. I have one working that allows me to dynamically create hyperlinks, however i cant get my nested repeater to work to display the date the file was created. Is it possible to use repeaters to dynamically display multiple variables from lists as i'm trying to do below?
.aspx
<asp:Repeater id="repLinks" runat="server">
<ItemTemplate>
<tr><td>
<asp:HyperLink runat="server" NavigateUrl='<%# Container.DataItem.ToString() %>' Text="<%# Container.DataItem.ToString().Split('\\').Last() %>" />
<td>
<asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound" >
<ItemTemplate>
<%# Container.DataItem.ToString()%>
</ItemTemplate>
</asp:Repeater>
</td>
<td>
Submitted By <!--add repeater-->
</td>
<td>
Mark as Billed <!--add repeater-->
</td>
</td></tr>
</ItemTemplate>
</asp:Repeater>
.aspx.cs
public List<string> CD = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
//Welcomes User
string Uname = Environment.UserName;
UserName.Font.Size = 17;
UserName.Text = "Welcome: " + Uname;
//gives path and constructs lists for directory paths and file links
string root = "C:\\Users\\James\\Documents\\Visual Studio 2015\\WebSites";
List<string> lLinks = new List<string>();
//adds files to list
foreach (var path in Directory.GetDirectories(#root))
{
foreach (var path2 in Directory.GetFiles(path))
{
lLinks.Add(path2);
CD.Add(File.GetCreationTime(path2).Date.ToString("yyyy-mm-dd"));
}
}
//Define your list contents here
repLinks.DataSource = lLinks;
repLinks.DataBind();
}
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
Repeater2.DataSource = CD;
Repeater2.DataBind();
}
}
The issue with your code is that you are binding the nested repeater control (Repeater2) in the ItemDataBound event of Repeater2 repeater itself which will never get fired because ItemDataBound event is fired for each item in collection when it is bounded to the repeater control.
You should write the logic in ItemDataBound event of your parent repeater like this:-
<asp:Repeater id="repLinks" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
Then, write the logic in this event handler:-
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater Repeater2 = (Repeater)(e.Item.FindControl("Repeater2"));
Repeater2.DataSource = CD;
Repeater2.DataBind();
}
}
Also, In the Page_Load event you should bind the Parent Repeater Repeater1 on just the initial page load so wrap it inside !IsPostBack and populate your datasource lLinks & CD from a separate method intead of doing it in Page_Load event.
I have asp.net Web Application. i have following Repeater control on the .aspx Page:
<asp:Repeater ID="repHomeList" runat="server" onitemdatabound="repHomeList_ItemDataBound">
<ItemTemplate>
<div class="pro-col">
<div class="p-img">
<asp:Image runat="server" ID="imgHomeImage" />
<asp:Label runat="server" ID="lblImageName" Visible="false" Text='<%#Eval("Images") %>'></asp:Label>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
and Following Event
protected void repHomeList_ItemDataBound(object sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
return;
Image imgHomeImage = (Image)e.Item.FindControl("imgHomeImage");
Label lblImageName = (Label)e.Item.FindControl("lblImageName");
if (imgHomeImage != null && lblImageName != null)
imgHomeImage.ImageUrl = Server.MapPath("PropertyImages/" + lblImageName.Text);
}
the above code compiles fine. but Image is not displaying when DataBinds in Repeater Control.
so how to set Image on ItemDatabound event ??
Thanks
Try changing it to this instead:
imgHomeImage.ImageUrl = Server.MapPath("~/PropertyImages/" + lblImageName.Text);
Please note the ~
Ps you can get the image name server side with the following:
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
YourObject anObject = (YourObject)dataItem.DataItem;
var images = anObject.Images;
imgHomeImage.ImageUrl = Server.MapPath("~/PropertyImages/" + images);
You'd have to change YourObject to your specific object.
This would avoid the need to write them out and retrieve them.
<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Cartridges_ItemCommand">
<ItemTemplate>
<p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
<hr class="hr4" />
<p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
<div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton></div>
</ItemTemplate>
</asp:Repeater>
How can I pass the textbox value within CommandArgument? Sorry totally lost...
Did you try: CommandArgument='<%#Eval("cartID") %>'
this is different from yours as it is surrounded by a single quote, I guess this is the correct syntax.
Use FindControl to get other items in the repeater Item. For Example:
protected void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
LinkButton lb = (LinkButton)e.CommandSource;
string textBoxValue = ((TextBox)lb.Parent.FindControl("cartQty")).Text;
}
you need to bind the cartId to the linkbutton onItemDataBound and then access it onItemCommand, I have modified code for you, give this a go
<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Repeater_OnItemCommand" OnItemDataBound="Repeater_OnItemDataBound">
<ItemTemplate>
<p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
<hr class="hr4" />
<p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
<div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" Text="Buy"></asp:LinkButton></div>
your onItemdatabound should look like this
protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//your code...
LinkButton add = (LinkButton)e.Item.FindControl("buy");
add.CommandArgument = cartID.ToString();
}
and then you can access the text box on item command like this
protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "AddtoCart")
{
LinkButton btnEdit = (LinkButton)e.CommandSource;
if (btnEdit != null)
{
string editId = btnEdit.CommandArgument;
string text = ((TextBox)e.Item.FindControl("cartQty")).Text;
//do some stuff with your cartid and quantity
}
}
}
You can also extend your code with edit/delete command arguments by adding more linkbuttons and binding them to the correct command and then accessing them in on item command
Thanks
I have the following repeater below and I am trying to find lblA in code behind and it fails. Below the markup are the attempts I have made:
<asp:Repeater ID="rptDetails" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><strong>A:</strong></td>
<td><asp:Label ID="lblA" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
First I tried,
Label lblA = (Label)rptDetails.FindControl("lblA");
but lblA was null
Then I tried,
Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");
but Items was 0 even though m repeater contains 1 itemtemplate
You need to set the attribute OnItemDataBound="myFunction"
And then in your code do the following
void myFunction(object sender, RepeaterItemEventArgs e)
{
Label lblA = (Label)e.Item.FindControl("lblA");
}
Incidentally you can use this exact same approach for nested repeaters. IE:
<asp:Repeater ID="outerRepeater" runat="server" OnItemDataBound="outerFunction">
<ItemTemplate>
<asp:Repeater ID="innerRepeater" runat="server" OnItemDataBound="innerFunction">
<ItemTemplate><asp:Label ID="myLabel" runat="server" /></ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
And then in your code:
void outerFunction(object sender, RepeaterItemEventArgs e)
{
Repeater innerRepeater = (Repeater)e.Item.FindControl("innerRepeater");
innerRepeater.DataSource = ... // Some data source
innerRepeater.DataBind();
}
void innerFunction(object sender, RepeaterItemEventArgs e)
{
Label myLabel = (Label)e.Item.FindControl("myLabel");
}
All too often I see people manually binding items on an inner repeater and they don't realize how difficult they're making things for themselves.
I just had the same problem.
We are missing the item type while looping in the items. The very first item in the repeater is the header, and header does not have the asp elements we are looking for.
Try this:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");}
Code for VB.net
Protected Sub rptDetails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptDetails.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim lblA As Label = CType(e.Item.FindControl("lblA"), Label)
lblA.Text = "Found it!"
End If
End Sub
Investigate the Repeater.ItemDataBound Event.
You should bind first.
for example)
rptDetails.DataSource = dataSet.Tables["Order"];
rptDetails.DataBind();