简体   繁体   English

如何在使用C#和ASP.NET 4.5的转发器中使用linkbutton

[英]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. 在asp.net中,我在数据库中有一个包含问题,提交日期和答案的表。在页面加载时只显示问题。现在我想使用任何链接,点击显示表格数据中指定的答案和日期,在文本框或标签中并再次单击该链接答案再次消失,如单击时展开和缩小。 So what coding should i use for this in C#? 那么我应该在C#中使用什么编码呢?

I believe you could handle the ItemCommand event of the Repeater. 我相信你可以处理Repeater的ItemCommand事件。

Place a LinkButton control for the link that you want the user to click in the Repeater's item template. 为您希望用户在Repeater的项目模板中单击的链接放置一个LinkBut​​ton控件。 Set the CommandName property of this to something meaningful, like "ShowAnswers". 将此CommandName属性设置为有意义的属性,如“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. 此外,将Label或TextBox控件添加到Repeater的项模板中,但在aspx标记中将其Visible属性设置为false。

In the code-behind, within the ItemCommand event handler check if the value of e.CommandName equals your command ("ShowAnswers"). 在代码隐藏中,在ItemCommand事件处理程序中检查e.CommandName的值e.CommandName等于您的命令(“ShowAnswers”)。 If so, then find the Label or TextBox controls for the answers and date within that Repeater item (accessed via e.Item ). 如果是,则在该Repeater项目中找到答案和日期的Label或TextBox控件(通过e.Item访问)。 When you find them, set their Visible property to true. 找到它们后,将其Visible属性设置为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. 注意:您可以采用不同的方法使用AJAX为用户提供更加无缝的体验,但这种方式最初可能更简单。

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: ASPX加价:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Repeater Asp.Net C#中的LinkBut​​ton中更改图像源 - How to change image source inside LinkButton in Repeater Asp.Net C# 使用C#将asp.net LinkBut​​ton添加到文字 - Add asp.net LinkButton to a Literal using c# 如何在ASP.NET C#的中继器控件内使用子中继器控件 - How to use Child Repeater Control inside a Repeater Control in asp.net c# 如何在ASP.Net C#中使用ModalPopupExtender连接GridView中的LinkBut​​ton? - How to hookup the LinkButton in a GridView with a ModalPopupExtender in ASP.Net C#? 如何将 LinkButton InnerHTML 从 C# 更改为 asp.net? - How to change LinkButton InnerHTML from C# asp.net? 如何在HTML中创建链接,与在ASP.NET中的中继器中创建链接按钮相同 - How to create links in html same as Linkbutton as in a Repeater in ASP.NET 在Repeater内部的Linkbutton用于分页ASP.Net - Linkbutton inside Repeater for paging ASP.Net 如何使用c#asp.net在LinkBut​​ton的click事件中获取gridview的DataKeyNames值 - How to get DataKeyNames value of gridview inside LinkButton's click event using c# asp.net 如何使用/不使用ASP.net C#中的CodeBehind将DropDownList中的SelectedValue作为查询字符串传递给LinkBut​​ton - How to pass the SelectedValue from a DropDownList as a query string in a LinkButton with/without using CodeBehind in ASP.net C# 使用C#的ASP.NET,在Repeater中自动刷新div - ASP.NET using C#, auto refresh div in a Repeater
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM