Give asp:hyperlink in a repeater a url from code behind - c#

Okay so imagine next situation:
I have a .ascx file with a repeater and in this repeater there is a asp:hyperlink.
Is it possible to set the url of this hyperlink from the code behind?
In the end the link will always point towards the same url but it needs to be repeated x-times.
test code ascx:
<asp:Repeater runat="server" ID="repeater" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<div class="container">
<asp:HyperLink ID="hypUrl" runat="server" Text="Dit is een test link"> </asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
Normally in the code behind I would do something like:
hypUrl.NavigateUrl = Url;
But because the hyperlink is in a repeater it doesnt seem to find the ID.
Anyone knows what the best way is to achieve this?
Thanks in advance!

In repeater's ItemDataBound event. Do it like this
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink hypLink = (HyperLink)e.Item.FindControl("hypUrl");
hypLink.NavigateUrl = "http//www.stackoverflow.com";
}
}

Yes, in your repeater_ItemDataBound event:
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Find the hyperlink
HyperLink hypUrl = (HyperLink)e.Item.FindControl("hypUrl");
// Set the property
hypUrl.NavigateUrl = "foo";
}
}

Related

How to get element Id inside repeater control in ASpx page using C#

I have done some searching on this but i can't seem to find anything related to my issue but how do i get the element Id inside a repeater control? for example i have the following:
<asp:Repeater ID="Rpt" runat="server">
<ItemTemplate>
<p><%# Eval("Name") %>
<asp:HyperLink ID="Url" runat="server" Text ="<%# Eval("Url") %>"/> </span></p>
</ItemTemplate>
</asp:Repeater>
Behind the code i want to get and set Id (url) of the hyper link in code above but for some reason i can't access behind the code as it is not recognised. what is the best way to get the Id?
in my c# code , i just want to set url like so:
Url.NavigateUrl = 'https://stackoverflow.com';
edit
now it doesn't hit the code inside the foreach loop.
foreach (RepeaterItem item in Rpt.Items)
{
HyperLink Url = item.FindControl("Url") as HyperLink;
Url.NavigateUrl = link from db;
}
Many thanks
You can find using following ways:
Using ItemDataBound event:
protected void Rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink hplUrl = (HyperLink)e.Item.FindControl("Url");
hplUrl.NavigateUrl = 'https://stackoverflow.com';
}
}
Or using all rows
foreach (RepeaterItem item in Rpt.Items)
{
HyperLink Url = item.FindControl("Url") as HyperLink;
}
below code can help you:
foreach (RepeaterItem item in Rpt.Items)
{
HyperLink lnk = item.FindControl("Url") as HyperLink;
}

How can access Button in Inner DataList?

Comments DataList (Outer) and Replies DataList (Inner).
While in Inner DataList there is a LinkButton that gets text from TextBox.
How I can access Add event of that LinkButton?
.aspx code
<asp:DataList ID="dlComments" runat="server">
<asp:DataList ID="dlReplies" runat="server">
<asp:TextBox ID="txtReply" TextMode="MultiLine" Rows="3" Width="100%" runat="server"></asp:TextBox>
<asp:LinkButton ID="lbEdit" CommandName="Add" runat="server">Edit</asp:LinkButton>
</asp:DataList>
</asp:DataList>
You can add the OnItemCommand to the nested DataList.
<asp:DataList ID="dlReplies" runat="server" OnItemCommand="dlReplies_ItemCommand">
And then in code behind
protected void dlReplies_ItemCommand(object source, DataListCommandEventArgs e)
{
//check for the correct commandname
if (e.CommandName == "Add")
{
//use findcontrol to find the textbox and cast it back to one
TextBox tb = e.Item.FindControl("txtReply") as TextBox;
Label1.Text = tb.Text;
}
}
Add commandName="click" on you linkbutton.
private void DataList1_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) {
if ((e.Item.ItemType == ListItemType.AlternatingItem)
|| (e.Item.ItemType == ListItemType.Item)) {
DataList dtl2 = (DataList)(e.Item.FindControl("dlReplies"));
dtl2.ItemCommand += new System.EventHandler(this.dtl2_ItemCommand);
}
}
protected void dtl2_ItemCommand(object sender, System.Web.UI.WebControls.DataListCommandEventArgs e) {
if (e.CommandName == "click") {
// 'do what you want here
}
}

Error in getting Nullreferenceexception even though there is text assigned to the label

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

Issue Related to Binding Image in <asp:Repeater> Control

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.

Can't find control within asp.net repeater?

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

Categories