我有一个GridView
其中我添加了一个额外的列详细信息,并且在GridView
的每一行中,我有一个名为LinkButton
的详细信息。因此,当我点击这个时,我希望触发一个事件。
asp.net代码是:
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" Text="Details"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
请帮助。谢谢。
答案 0 :(得分:3)
<asp:LinkButton ID="Details" OnClick="LinkClicked" runat="server" Text="Details">
代码隐藏中的:
protected void LinkClicked(Object sender, EventArgs e)
{
LinkButton link = (LinkButton)sender;
GridViewRow row = (GridViewRow)link.NamingContainer;
// assuming there's a label with ID=Label1 in another TemplateField:
Label label1 = (Label)row.FindControl("Label1");
label1.Text="You clicked the link button";
}
如果您需要用户点击该链接的GridViewRow
GridView
,例如要查找其他templateFields
中的控件,则可以使用NamingContainer
属性。
答案 1 :(得分:2)
处理 GridView.RowCommand事件
在GridView控件中单击按钮时发生。
<asp:LinkButton ID="Details" commandname="Details" runat="server" Text="Details"></asp:LinkButton>
void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Details")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
//Your Code
}
}
答案 2 :(得分:1)
roeyou可以有一个命令参数来确定单击了哪一行(假设你连续有多个按钮(标准按钮/链接按钮)
<asp:GridView OnRowCommand="GridViewOnItemCommand" runat="server">
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="btnDetails" CommandName="Details" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Details" runat="server"/>
<asp:LinkButton ID="btnDelete" CommandName="Delete" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Delete" runat="server"/>
<ItemTemplate>
</asp:TemplateField>
</asp:GridView>
在代码隐藏文件
中protected void GridViewOnItemCommand(object sender, GridViewCommandEventArgs e)
{
//you can determine which button was clicked (detail or delete)
var command = e.CommandName;
//you can determine which row was clicked
var arg = e.CommandArgument;
if(command == "Details")
ShowDetails(arg);
if(command == "Delete")
Delete(arg);
}
希望这会有所帮助