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

In asp.net, I have a table in a database containing questions, date of submission, and responses. Only questions are loaded on the page. Now I want to use any link that, when clicked, shows the responses and the date given in the table data, or in a textbox or shortcut, and when clicking on that link again, the response disappears again, how to expand and compress. So what coding should I use for this in C #?

+3


source to share


1 answer


I believe you can handle the ItemCommand event of the relay.

Place a LinkButton control for the link you want the user to click in the Repeater control template. Set this CommandName property to something meaningful, such as "ShowAnswers". Also, add a Label or TextBox to the Repeater template, but set the Visible property to false in the aspx markup.

In the code, in the ItemCommand event handler, check if the value matches e.CommandName

your command ("ShowAnswers"). If so, find Label or TextBox controls for responses and dates in that Repeater (accessed via e.Item

). When you find them, set the Visible property to true.

Note. You can take a different approach using AJAX to provide a smoother user experience, but this is probably easier to implement initially.

I think the implementation would look something like this. Disclaimer: I have not 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>

      

+7


source







All Articles