Access a control inside a the LayoutTemplate of a ListView - c#

How do I access a Control in the LayoutTemplate of a ListView control?
I need to get to litControlTitle and set its Text attribute.
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Any thoughts? Perhaps via the OnLayoutCreated event?

Try this:
((Literal)lv.FindControl("litControlTitle")).Text = "Your text";

The complete solution:
<asp:ListView ID="lv" OnLayoutCreated="OnLayoutCreated" runat="server">
<LayoutTemplate>
<asp:Literal ID="lt_Title" runat="server" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
In codebehind:
protected void OnLayoutCreated(object sender, EventArgs e)
{
(lv.FindControl("lt_Title") as Literal).Text = "Your text";
}

This technique works for template layout; use the init event of the control:
<asp:ListView ID="lv" runat="server" OnDataBound="lv_DataBound">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" OnInit="litControlTitle_Init" />
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
And capture a reference to the control for use in the code-behind (e.g) in the ListView's DataBound event:
private Literal litControlTitle;
protected void litControlTitle_Init(object sender, EventArgs e)
{
litControlTitle = (Literal) sender;
}
protected void lv_DataBound(object sender, EventArgs e)
{
litControlTitle.Text = "Title...";
}

For Nested LV Loop:
void lvSecondLevel_LayoutCreated(object sender, EventArgs e)
{
Literal litText = lvFirstLevel.FindControl("lvSecondLevel").FindControl("litText") as Literal;
litMainMenuText.Text = "This is test";
}

In case you need the VB version, here it is
Dim litControl = CType(lv.FindControl("litControlTitle"), Literal)
litControl.Text = "your text"

Related

Get data from from repeater via Request.Form to another page

I have a simple repeater to list textbox.
try.aspx
<form id="form1" runat="server" action="try_send.aspx" method="get">
<div>
<asp:Repeater ID="ClRpt" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Kod" runat="server" Text='<%#Eval("KOD") %>'></asp:Label>
<asp:TextBox ID="Amount" runat="server"></asp:TextBox>
<asp:Button ID="ButtonClick" CssClass="bt" runat="server" Text="SEPETE EKLE" Width="100%" UseSubmitBehavior="false" />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
try.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string Firma = Session["FirmaID"].ToString();
string Pryt = "01";
string GRID = "KATALOG";
Page.Title = GRID + " ÜRÜN LİSTESİ";
string SQLGrup = "SQL Here";
SqlCommand rsCl = new SqlCommand(SQLGrup, bag.Bagla());
SqlDataReader ClOku = rsCl.ExecuteReader();
ClRpt.DataSource = ClOku;
ClRpt.DataBind();
ClOku.Close();
}
I want to send textbox value named Amount to try_send.aspx file
try_send.aspx:
<body>
<asp:Label ID="Amount" runat="server" Text="Label"></asp:Label>
</body>
try_send.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
decimal Amnt = Convert.ToDecimal(Request.Form["Amount"]);
Amount.Text = Amnt.ToString();
}
Amount is null when I send the form.
Thanks...
In try.aspx, you just show a bunch of information in your Repeater.
You don't have a button to submit the action.
<form id="form1" runat="server">
<asp:Repeater ID="ClRpt" runat="server">
//content
</asp:Repeater>
<asp:Button Id="xx" runat="server" OnClick="xx_OnClick"/>
</form>
Then, you redirect and pass the amount you want in the event click
public void xx_Onclick(object sender, EventArgs e)
{
//redirect to try_send with the value amount
}

How to get LinkButton text in nested Repeaters

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

Bind a repeater dynamically inside another repeater?

I have this code:
<asp:Repeater id="repeaterCategories" runat="server">
<ItemTemplate>
<div class="categorie-item">
...
<asp:Repeater id="repeaterSubCategories" runat="server">
<ItemTemplate>
...
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
and repeaterSubCategories must be repeaterCategories.SubCategories, for each repeaterCategories. So I have to bind dynamically (for each first repeater iteration) a list of sub categories.
Can I do it? How?
If you have a nested repeater like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="Repeater2" runat="server"></asp:Repeater>
</ItemTemplate>
</asp:Repeater>
You can use this to bind to it:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var data = ((MyClass)e.Item.DataItem).Subcategories;
var repeater2 = (Repeater)e.Item.FindControl("Repeater2");
repeater2.DataSource = data;
repeater2.DataBind();
}

how to get value of textbox in listview?

<asp:ListView runat="server" ID="lvAttachments" ItemPlaceholderID="ph" OnItemDataBound="lvAttachments_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="btnReject" OnClick="btnReject_Click"></asp:LinkButton>
<asp:TextBox runat="server" ID="tbReason" CssClass="textbox" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:ListView>
My question is: how to get text from textbox, while btnReject click action?
protected void btnReject_Click(object sender, EventArgs e)
{
LinkButton btnReject = (LinkButton)sender;
// how to get tbReason.Text from this item?
}
Regards
edit:
Problem resolved !
We just need to add in Page_Load code to prevent re-load listview and clear textboxes:)
http://forums.asp.net/t/1712482.aspx/1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvAttachments.DataSource = tAttachmentBO.getAttachmentsToAccept();
lvAttachments.DataBind();
}
}
Something like this should work
I'm on ipad so apologize for any mistakes
TextBox txt = (TextBox)btnReject.Parent.FindControl("tbReason")

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