C# 将客户端id传递给代码隐藏
是否有方法将唯一的客户端id传递给codebehind?我在gridview中有一个imagebutton,我希望这样做:C# 将客户端id传递给代码隐藏,c#,asp.net,C#,Asp.net,是否有方法将唯一的客户端id传递给codebehind?我在gridview中有一个imagebutton,我希望这样做: <asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/View.png" CommandName="wView" CommandArgument='#<%=imbView.ClientID%>' /> 在调试时,虽然我
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/View.png" CommandName="wView" CommandArgument='#<%=imbView.ClientID%>' />
在调试时,虽然我看到我的CommandArgument
是
指定:我想传递唯一标识生成元素的东西(我认为ClientID是标识它的好方法)。嗯
假设你有
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/View.png" CommandName="wView" OnCommand="aaa" />
嗯
假设你有
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/View.png" CommandName="wView" OnCommand="aaa" />
下面是如何在内部检索CommandArgument 您还可以在RowCommand事件中使用
e.CommandSource作为ImageButton
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Detail">
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server"
ToolTip="View details" ImageUrl="~/css/images/View.png"
CommandName="wView"
CommandArgument='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Name" DataField="Name">
</asp:BoundField>
</Columns>
</asp:GridView>
代码隐藏
公共类项目
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
GridView1.DataSource=新列表
{
新项{Id=1,Name=“John”},
新项{Id=2,Name=“Eric”},
};
GridView1.DataBind();
}
}
受保护的void GridView1_row命令(对象发送方,GridViewCommandEventArgs e)
{
如果(e.CommandName==“wView”)
{
var imageButton=e.CommandSource作为imageButton;
字符串clientId=imageButton.clientId;
int id=Convert.ToInt32(e.CommandArgument);
}
}
以下是如何在内部检索CommandArgument
您还可以在RowCommand事件中使用e.CommandSource作为ImageButton
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Detail">
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server"
ToolTip="View details" ImageUrl="~/css/images/View.png"
CommandName="wView"
CommandArgument='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Name" DataField="Name">
</asp:BoundField>
</Columns>
</asp:GridView>
代码隐藏
公共类项目
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
GridView1.DataSource=新列表
{
新项{Id=1,Name=“John”},
新项{Id=2,Name=“Eric”},
};
GridView1.DataBind();
}
}
受保护的void GridView1_row命令(对象发送方,GridViewCommandEventArgs e)
{
如果(e.CommandName==“wView”)
{
var imageButton=e.CommandSource作为imageButton;
字符串clientId=imageButton.clientId;
int id=Convert.ToInt32(e.CommandArgument);
}
}
#
不是有效的语法。它应该是
;即使如此,像这样将客户端id传递给服务器端也是没有意义的。您想在哪里访问该服务器控件?换句话说,事件的名称是什么?@Win不确定我是否理解你的意思,但这是在gridview中,imagebutton使用我指定的onrow命令
。在我的onrow命令中
我可以访问CommandName
和CommandArgument
。但是,如果我无法发送唯一标识按钮(或单元格或行)的CommandArgument,这对我并没有帮助。请发布卡住的代码。
不是有效的语法。它应该是
;即使如此,像这样将客户端id传递给服务器端也是没有意义的。您想在哪里访问该服务器控件?换句话说,事件的名称是什么?@Win不确定我是否理解你的意思,但这是在gridview中,imagebutton使用我指定的onrow命令
。在我的onrow命令中
我可以访问CommandName
和CommandArgument
。但是如果我不能发送唯一标识我的按钮(或单元格或行)的CommandArgument,这并不能真正帮助我,请发布卡住的代码。是的,但由于这是一个Gridview,这只会给我Gridview的ClientID,而不是实际的图像按钮,但由于这是一个Gridview,这只是给我gridview的ClientID,而不是实际的图像按钮