我是一个菜鸟,并尝试使用commandname和commandargument进行演示。请告诉我如何使用asp:LinkButton
<table>
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate>
<tr>
<th> FirstName </th>
<th> LastName </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# Eval("FirstName")%></td>
<td> <%# Eval("LastName")%></td>
<asp:LinkButton ID="EditButton" runat="server" Text="Edit" CommandName="EditName" CommandArgument="EditArgument" ></asp:LinkButton>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
答案 0 :(得分:1)
您需要为链接按钮创建一个单击事件处理程序,如下所示:
标记:
<table>
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate>
<tr>
<th> FirstName </th>
<th> LastName </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# Eval("FirstName")%></td>
<td> <%# Eval("LastName")%></td>
<asp:LinkButton ID="EditButton" runat="server" Text="Edit"
CommandName="EditName"
CommandArgument="EditArgument"
OnClick="OnClickHandler" >
</asp:LinkButton>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
代码隐藏:
protected void OnClickHandler(object sender, EventArgs e)
{
var theLinkButton = sender as LinkButton;
// The as operator returns null if the cast fails
// Check to see if link button exists before we try to use it
if(theLinkButton != null)
{
// Check for CommandName from markup
if(theLinkButton.CommandName = "EditName")
{
// Do logic here to edit name
}
// Other CommandName values could be handled here
}
}
注意:如果您有其他按钮,例如Delete
,那么您可以将其OnClick
值指向同一方法OnClickHandler
,然后传递不同的CommandName
使它具有不同的值,从而为多个动作提供一个处理程序。
答案 1 :(得分:0)
只需为您的转发器itemcommand事件添加事件处理程序
protected void Repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "EditName")
{
//TODO something
}
}