GridView 内的 ImageButton 与常规 ImageButton 的 OnClick 差异
我正在开发一个 ASP.NET/C# 项目,并且有一个 GridView 控件。其中一列是一个 TemplateField/ItemTemplate,里面有一个 ImageButton。在 OnClick 事件期间,我使面板可见。当使用常规 ImageButton 执行此操作时,页面将重新加载并且面板可见。我的 GridView 内的 ImageButton 不会发生这种情况。有谁知道我怎样才能做到这一点,或者为什么会出现这种情况?
感谢您抽出宝贵的时间,如果您有任何疑问,请告诉我。
这是相关的 asp 定义
<asp:TemplateField ItemStyle-Width="90px" ItemStyle-VerticalAlign ="Bottom" ItemStyle-HorizontalAlign ="Center" HeaderText="Add Alias">
<HeaderStyle Font-Bold="True" Font-Size="11px" VerticalAlign="Bottom"/>
<ItemTemplate>
<asp:ImageButton ID ="btnAddAliasHeader" runat="server" ImageUrl="~/img/AddAlias.gif" onclick="btnAddAlias_Click" />
</ItemTemplate>
</asp:TemplateField>
这是 OnClick 函数的 C# 代码。
protected void btnAddAlias_Click(object sender, EventArgs e)
{
ImageButton btnAddAlias = (ImageButton)sender;
GridViewRow row = (GridViewRow)btnAddAlias.Parent.Parent;
int rowIndex = row.RowIndex;
lblSelectedCity.Text = gvResults.Rows[rowIndex].Cells[0].Text;
lblSelectedCountry.Text = ddlCountry.Text;
pnlAddAlias.Visible = true;
}
我添加了 OnRowCommand 事件并使面板在那里可见,但它仍然没有显示。我认为这可能与回发有关,因为如果我然后单击 gridview 外部的按钮,则在重新加载时, gridview 内部按钮的面板会显示正常。
编辑:我是个白痴,忽略了所有这一切都发生在更新面板中。
I'm working on a ASP.NET/C# project, and have a GridView control. One of the columns is a TemplateField/ItemTemplate with an ImageButton inside. During the OnClick event, I make a panel visible. When doing this with a regular ImageButton, the page reloads and the panel is visible. This doesn't happen with my ImageButton inside of the GridView. Does anyone know how I can make it do so, or why this is the case?
Thanks for your time, please let me know if you have any questions.
Here's the relevant asp definition
<asp:TemplateField ItemStyle-Width="90px" ItemStyle-VerticalAlign ="Bottom" ItemStyle-HorizontalAlign ="Center" HeaderText="Add Alias">
<HeaderStyle Font-Bold="True" Font-Size="11px" VerticalAlign="Bottom"/>
<ItemTemplate>
<asp:ImageButton ID ="btnAddAliasHeader" runat="server" ImageUrl="~/img/AddAlias.gif" onclick="btnAddAlias_Click" />
</ItemTemplate>
</asp:TemplateField>
And here's the C# for the OnClick function.
protected void btnAddAlias_Click(object sender, EventArgs e)
{
ImageButton btnAddAlias = (ImageButton)sender;
GridViewRow row = (GridViewRow)btnAddAlias.Parent.Parent;
int rowIndex = row.RowIndex;
lblSelectedCity.Text = gvResults.Rows[rowIndex].Cells[0].Text;
lblSelectedCountry.Text = ddlCountry.Text;
pnlAddAlias.Visible = true;
}
I added the OnRowCommand event and made the panel visible there, however it still doesn't show up. I think it may have something to do with postback, because if I then click the button outside of the gridview, on the reload then the panel from the button inside the gridview shows up fine.
Edit: I'm an idiot and left out that all of this is happening in an updatepanel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当
Button
、ImageButton
和LinkButton
位于像您这样的控件模板中时,它们不会触发Click
事件描述。 (编辑:除非与控件的OnClick
属性一起使用)相反,包含的控件会触发一个事件,在GridView
的情况下,它是一个RowCommand< /代码> 事件。使用按钮的
CommandName
和CommandArgument
属性来确定在事件处理程序中采取的正确操作,如下所示:标记:
代码:
Button
s,ImageButton
s andLinkButton
s do not fireClick
events when they are in a control template like you describe. (Edit: unless used with theOnClick
property of the control) Instead, the containing control fires an event, in the case of theGridView
it is aRowCommand
event. Use theCommandName
andCommandArgument
properties of your button to determine the proper action to take in the event handler as shown below:Markup:
Code:
容器内的按钮/图像按钮(例如网格视图(列表视图、转发器等))的单击事件会冒泡到容器本身。要使面板可见,需要使用按钮上的命令名称和命令参数属性,然后使用gridview的OnRowCommand事件来显示面板
HTH
The buttons/imagebuttons inside a container such as a gridview (listview, repeater, etc) have their click event bubbled up to the container itself. To make the panel visible, you need to use the command name and command argument properties on the button, and then use the OnRowCommand event of the gridview to dispay the panel
HTH
我假设您的事件没有触发 - 您应该使用 imagebutton 上的 commandname 属性 - 并在 gridview 的 onrowcommand 上选择它 - 或者在 onrowcreated 事件中注册您的 imagebutton 单击事件。
I'm supposing your event isn't firing - you should use the commandname attribute on the imagebutton - and pick it up on the onrowcommand of the gridview - or register your imagebutton click event in the onrowcreated event.
如果不需要回发。抱歉,如果我误解了这个问题。您可以不使用图像按钮(服务器控件),而使用包含在 a 标签和 jQuery 中的链接或常规图像。这将阻止回发到服务器。希望这有帮助。
希望这有帮助。
If the postback is not needed. Sorry if I misunderstood the question. You could instead of using an image button (server control), use a link or regular image wrapped in an a tag and jQuery. This will prevent the postback to the server. Hope this helps.
Hope this helps.
您可以使用 gridview 的 Oncommand 事件,而不是在特定控件上使用 Onclick,只需从控件传递命令名并通过 e.commandname 检查它,然后执行您想要执行的操作
You can user Oncommand Event for gridview rather than using Onclick on the perticular control only you have to pass commandname from the control and check it by e.commandname and do what ever you want to perform