似乎无法捕获 asp.net 中 gridview 中的按钮单击
我将一些图像按钮放入网格视图中,但无法捕获单击事件。创建单击事件或在 gridview 中创建 OnRowCommand 处理程序都不起作用。
单击按钮只需回发到当前页面。
我像这样添加按钮:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string status = DataBinder.Eval(e.Row.DataItem, "visitstatusuid").ToString();
string visitUID = DataBinder.Eval(e.Row.DataItem, "visituid").ToString();
Color backColor = Color.White;
Color foreColor = Color.Black;
ImageButton b;
switch (status)
{
case "U": // Unallocated
backColor = ColorTranslator.FromHtml("#B2A1C7");
b = new ImageButton();
b.Width = Unit.Pixel(25);
b.Height = Unit.Pixel(30);
b.AlternateText = "Book";
b.ImageUrl = "../../Images/New/booking.gif";
b.ToolTip = "Booking";
b.CommandName = "Booking";
b.CommandArgument = visitUID;
b.CausesValidation = false;
e.Row.Cells[(e.Row.Cells.Count - 3)].Controls.Add(b);
等等。
I put some Image Buttons into my gridview, but I cannot capture the click event. Neither creating a click event, nor creating an OnRowCommand handler in the gridview works.
Clicking the buttons simply postbacks to the current page.
I add my buttons like this:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string status = DataBinder.Eval(e.Row.DataItem, "visitstatusuid").ToString();
string visitUID = DataBinder.Eval(e.Row.DataItem, "visituid").ToString();
Color backColor = Color.White;
Color foreColor = Color.Black;
ImageButton b;
switch (status)
{
case "U": // Unallocated
backColor = ColorTranslator.FromHtml("#B2A1C7");
b = new ImageButton();
b.Width = Unit.Pixel(25);
b.Height = Unit.Pixel(30);
b.AlternateText = "Book";
b.ImageUrl = "../../Images/New/booking.gif";
b.ToolTip = "Booking";
b.CommandName = "Booking";
b.CommandArgument = visitUID;
b.CausesValidation = false;
e.Row.Cells[(e.Row.Cells.Count - 3)].Controls.Add(b);
etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
创建按钮时,您需要附加处理程序:
编辑:
不要在 OnRowDataBound 处理程序中创建按钮,而是使用 OnRowCreated。
这可确保在回发时重新创建按钮。
示例:
You'll need to attach the handler when the button is created:
Edit:
Instead of creating the button in the OnRowDataBound handler, use OnRowCreated.
This ensures the button is recreated on postbacks.
Example:
除非您在其他地方添加事件处理程序,否则您需要在 aspx 文件的页面指令中设置
AutoEventWireup="true"
。话虽如此,我更喜欢显式连接事件,而不是使用
AutoEventWireup
将此行添加到您的OnInit
方法中:Unless you are adding an event handler elsewhere you would need to set
AutoEventWireup="true"
in the page directive of your aspx file.That being said I prefer explicitly wiring events so rather than use
AutoEventWireup
add this line to yourOnInit
method:为了使使用 RowDataBound 的方法发挥作用,您需要在每次页面加载时重新绑定网格,并确保在生命周期中的 OnLoad 之前执行此操作,以便及时注册单击事件。
我成功使用的另一种方法是创建一个新方法来执行 DataGrid 按钮设置,例如,
时以及每次回发(即在 OnLoad 处理程序中)时调用该方法:
然后每次执行手动数据绑定 这种方法是您不必在每次回发时进行数据绑定,从而节省资源。
For your approach using RowDataBound to work, you need to rebind the grid on every page load, and ensure you do it no later than OnLoad in the lifecycle, in order for the click event to be registered in time.
An alternative approach I have had success with is to create a new method for doing the DataGrid button setup, e.g.
Then you call the method every time you perform a manual databind, and also on every postback i.e. in your OnLoad handler do:
The advantage of this approach is that you don't have to databind on every postback, which saves resources.
为 gridview 创建一个 RowCommand 事件处理程序并检查命令名称以查看是否是您的按钮触发了它
的效果
create a RowCommand event handler for the gridview and check the command name to see if it's your button triggering it
something to the effect of
把grid的绑定事件放进去不回发。
Put the binding event of grid in to not post back.