How to get LinkButton text in nested Repeaters - c#

I have problem getting the LinkButton text in nested Repeaters
<div>
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" > <%# Container.DataItem %></asp:LinkButton>
</h6>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkBtnTags = (LinkButton)rp_tagsTopics.FindControl("lnkBtnTags");
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}

Or you can make use of the ItemCommand event by specifying the CommandName and CommandArgument parameters of the LinkButton
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" CommandName="Redirect" CommandArgument='<%# Container.DataItem %>' > <%# Container.DataItem %></asp:LinkButton>
And in the handler use the parameters:
protected void rp_tagsTopics_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if( e.CommandName == "Redirect" )
{
Response.Redirect("~/WebsofWonder.aspx?tag=" + e.CommandArgument);
}
}

What you should do is use the sender argument in the LinkButton_Click handler to get access to the instance of LinkButton that was actually clicked, and has raised the Click event:
protected void LinkButton1_Click(object sender, EventArgs e)
{
// Use sender instead of trying to find the control within the Repeater
LinkButton lnkBtnTags = (LinkButton) sender;
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}

Related

Repeater does not appear

the following code should show the list of websites in a repeater but it does not (though I get no errors):
In the aspx file I have:
<div>
<asp:Repeater ID="Rpt1" Visible="true" runat="server"></asp:Repeater>
<ItemTemplate>
<asp:HyperLink runat="server" Text = '<%# DataBinder.Eval(Container.GetDataItem(), "Key") %>'
NavigateUrl= '<%#DataBinder.Eval(Container.GetDataItem(), "Value") %>'>
</asp:Hyperlink>
</ItemTemplate>
</div>
In the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
CaricaSiti();
}
protected void CaricaSiti()
{
ListDictionary L = new ListDictionary();
L.Add("google", #"http://google.com");
L.Add("microsoft", #"http://microsoft.com");
L.Add("yahoo", #"http://yahoo.com");
Rpt1.DataSource = L;
Rpt1.DataBind();
}
The repeater's ItemTemplate must be inside of the repeater :-)
<asp:Repeater ID="Rpt1" Visible="true" runat="server">
<ItemTemplate>
<asp:HyperLink runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "Key") %>'
NavigateUrl= '<%#DataBinder.Eval(Container.DataItem, "Value") %>'>
</asp:Hyperlink>
</ItemTemplate>
</asp:Repeater>

OnLoad method DataList item`s control

I am building a website right now using DataLists and I want to ask a question:
When using OnLoad on specific control in DataList item, the DataList has 5 items, but the function is called only 4 times (does what it needs to do but not on the last item)
C# code:
protected void ibDeleteAlbum_Load(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
ImageButton btnDelAlbum = item.FindControl("ibDeleteAlbum") as ImageButton;
btnDelAlbum.Visible = true;
}
}
HTML code:
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="4" >
<ItemTemplate>
<asp:Label ID="lblAlbumID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id") %>' Visible="false"></asp:Label>
<asp:ImageButton ID="ibDeleteAlbum" type="image" runat="server" style="position:absolute; margin-right: 1.4vw; margin-top: 2.4vh;" Visible="false" ImageUrl="~/Images/ic_delete.png" OnClick="ibDeleteAlbum_Click" ToolTip="מחיקה" OnLoad="ibDeleteAlbum_Load" />
<asp:ImageButton ID="ibShowAlbums"
runat="server"
class="album"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "coverPhoto") %>'
CommandName="album"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "ID") %>'
OnCommand="ibShowAlbums_Command" />
<div class="albumNameShow" runat="server"><%#DataBinder.Eval(Container.DataItem, "albumName") %></div>
</ItemTemplate>
</asp:DataList>
Thanks ahead!

How to pass data to Link button onclick event

/*help me i am stuck here on how to pass <%# DataBinder.Eval(Container.DataItem, "Site_No") %> as parm to link button when clicked */
<ItemTemplate >
<!-- small box -->
<div class='<%# DataBinder.Eval(Container.DataItem, "Status") %>'>
<div class="inner">
<h3><asp:Label ID="lblSiteNo" runat="server"><%# DataBinder.Eval(Container.DataItem, "Site_No") %><sup style="font-size: 20px"></sup></asp:Label></h3>
<p><%# DataBinder.Eval(Container.DataItem, "Site_Size") %></p>
</div>
<asp:LinkButton ID="LinkButton1" CssClass="small-box-footer" OnClick="" runat="server">More info</asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>
Instead of using OnClick event, use the CommandName and CommandArgument properties.
<asp:LinkButton ID="LinkButton1" CssClass="small-box-footer" CommandName="MoreInfo_Command" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "PARAM_NAME") %>' runat="server">More info</asp:LinkButton>
In your code behind:
void MoreInfo_Command(Object sender, CommandEventArgs e)
{
string parameter = e.CommandArgument;
}

placing link button inside update panel not firing the click event of link button

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager<asp:UpdatePanel ID="UpdatePanel1"runat="server">
<ContentTemplate>
<asp:Timer runat="server" ID="Timer2" Interval="60" ontick="Timer1_Tick"/>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="1" onitemcommand="DataList1_ItemCommand">
<ItemTemplate>
<b>Test Name:</b> <%# DataBinder.Eval(Container.DataItem, "Name")%> <br />
<b>Test Phone:</b> <%# DataBinder.Eval(Container.DataItem, "Phone")%> <br />
<asp:LinkButton ID="btnView" runat="server" Text="View" CommandName="ShowDetails" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Name")%>' OnCommand="btnView_Command"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Panel ID="panel2" runat="server" Visible="false"></asp:Panel>
Code Behind :
protected void dlBundleRequests_ItemCommand(object source, DataListCommandEventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
if (e.CommandName == "ShowDetails")// null)
{
Session["Name"] = e.CommandArgument.ToString();
//Show Panel2
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//Binding DataList1
}
protected void btnView_Command(Object sender, CommandEventArgs e)
{
//set visibility true for Panel2
}
The necessity of adding updatepanel & timer control is to autorefresh the datalist1 for every 5 minutes.Please help me out of this.After adding these two controls ,link button stops working.
Try this..
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnView"/>
</Triggers>
you must set CommandArgument and CommandName in linkbutton tag, then use Command event not Click
<ItemTemplate>
<b>Test Name:</b> <%# DataBinder.Eval(Container.DataItem, "Name")%> <br />
<b>Test Phone:</b> <%# DataBinder.Eval(Container.DataItem, "Phone")%> <br />
<asp:LinkButton ID="btnView" runat="server" Text="View" OnCommand="btnView_Click" CommandName="ShowDetails" CommandArgument='%# DataBinder.Eval(Container.DataItem, "Phone")%' ></asp:LinkButton>
</ItemTemplate>
then handle that argument in codebehind..
CommandArgument in MSDN

working of linkbutton in repeater

I used a linkbutton in repeater which on click shows data in a label.Now i want that clicking again the same linkbutton hide that data,means same button for showing and hiding data.
there is a database with a table which contains ques-description,date,sub. by and ans.
On page load only question appears.
Now this is the design code:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "showanswers")
{
Control control;
control = e.Item.FindControl("date");
if(control!=null)
control.Visible = true;
control = e.Item.FindControl("subby");
if(control!=null)
control.Visible = true;
control = e.Item.FindControl("ans");
if(control!=null)
control.Visible = true;
}
And this is the html code i used:
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<table>
<b>Question<%#Container.ItemIndex + 1%>:</b><%#Eval("qstdsc") %><br />
<asp:linkbutton ID="Button1" Text="Ans." commandname="showanswers" runat ="server" /><br />
</table>
<table>
<asp:Label id="date" Text='<%# Eval("qstdat")%>' Visible="false" runat="server"/>
</table>
<table>
<asp:Label id="subby" runat="server" Text='<%# Eval("qstsubby")%>' Visible="false" />
</table>
<table>
<asp:Label id="ans" runat="server" Text='<%# Eval("qstans")%>' Visible="false" />
</table>
</ItemTemplate>
</asp:Repeater>
But i don't know how to hide data again clicking the same linkbutton.
Is it possible with a single button?
What hinders you to check if the label is visible and hide/show it accordingly?
protected void lnkBtnShowDataLabel_Click(Object sender, EventArgs e)
{
lblData.Visible = !lblData.Visible;
}

Categories

Resources