Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use linkbutton in repeater using C# with ASP.NET 4.5

In asp.net I have a table in database containing questions,date of submission and answers.On page load only questions appear.now i want to use any link which on clicking show answers and date specified in table data, either in textbox or label and clicking again on that link answer disappear again like expand and shrink on click. So what coding should i use for this in C#?

like image 304
user2069465 Avatar asked Feb 13 '13 19:02

user2069465


People also ask

How do I use a repeater link button?

Place a LinkButton control for the link that you want the user to click in the Repeater's item template. Set the CommandName property of this to something meaningful, like "ShowAnswers".

How to use repeater in in asp net?

The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items. Repeater is a Data Bind Control. Data Bind Controls are container controls.


1 Answers

I believe you could handle the ItemCommand event of the Repeater.

Place a LinkButton control for the link that you want the user to click in the Repeater's item template. Set the CommandName property of this to something meaningful, like "ShowAnswers". Also, add a Label or TextBox control into the Repeater's item template, but set their Visible property to false within the aspx markup.

In the code-behind, within the ItemCommand event handler check if the value of e.CommandName equals your command ("ShowAnswers"). If so, then find the Label or TextBox controls for the answers and date within that Repeater item (accessed via e.Item). When you find them, set their Visible property to true.

Note: you could take a different approach using AJAX to provide a more seamless experience for the user, but this way is probably simpler to implement initially.

I think the implementation would look something like this. Disclaimer: I haven't tested this code.

Code-Behind:

void Repeater_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "ShowAnswers")
    {
        Control control;

        control = e.Item.FindControl("Answers");
        if (control != null)
            control.Visible = true;

        control = e.Item.FindControl("Date");
        if (control != null)
            control.Visible = true;
    }
}

ASPX Markup:

<asp:Repeater id="Repeater" runat="server" OnItemCommand="Repeater_ItemCommand">
  <ItemTemplate>
    <asp:LinkButton id="ShowAnswers" runat="server" CommandName="ShowAnswers" />
    <asp:Label id="Answers" runat="server" Text='<%# Eval("Answers") %>' Visible="false" />
    <asp:Label id="Date" runat="server" Text='<%# Eval("Date") %>' Visible="false" />
  </ItemTemplate>
</asp:Repeater>
like image 174
Dr. Wily's Apprentice Avatar answered Oct 08 '22 09:10

Dr. Wily's Apprentice