似乎无法捕获 asp.net 中 gridview 中的按钮单击

发布于 2024-08-05 19:49:01 字数 1174 浏览 2 评论 0原文

我将一些图像按钮放入网格视图中,但无法捕获单击事件。创建单击事件或在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲欢浪云 2024-08-12 19:49:01

创建按钮时,您需要附加处理程序:

b.Click += MyButtonClickEventHandler;

编辑
不要在 OnRowDataBound 处理程序中创建按钮,而是使用 OnRowCreated。
这可确保在回发时重新创建按钮。

示例:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack) {
    BindData();
  }
}

protected void BindData()
{
  // Do your databinding here.
}

protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
  var b = new ImageButton();
  b.AlternateText = "Click Me!";
  // Etc.

  b.Click += MyButton_Click;
  // Add the button to the column you want.
}

protected void MyButton_Click(object sender, ImageClickEventArgs e)
{
  // Do your thing...
}

You'll need to attach the handler when the button is created:

b.Click += MyButtonClickEventHandler;

Edit:
Instead of creating the button in the OnRowDataBound handler, use OnRowCreated.
This ensures the button is recreated on postbacks.

Example:

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack) {
    BindData();
  }
}

protected void BindData()
{
  // Do your databinding here.
}

protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
  var b = new ImageButton();
  b.AlternateText = "Click Me!";
  // Etc.

  b.Click += MyButton_Click;
  // Add the button to the column you want.
}

protected void MyButton_Click(object sender, ImageClickEventArgs e)
{
  // Do your thing...
}
辞旧 2024-08-12 19:49:01

除非您在其他地方添加事件处理程序,否则您需要在 aspx 文件的页面指令中设置 AutoEventWireup="true"

话虽如此,我更喜欢显式连接事件,而不是使用 AutoEventWireup 将此行添加到您的 OnInit 方法中:

gridview1.RowDataBound += this.gridview1_RowDataBound;

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 your OnInit method:

gridview1.RowDataBound += this.gridview1_RowDataBound;
幻梦 2024-08-12 19:49:01

为了使使用 RowDataBound 的方法发挥作用,您需要在每次页面加载时重新绑定网格,并确保在生命周期中的 OnLoad 之前执行此操作,以便及时注册单击事件。

我成功使用的另一种方法是创建一个新方法来执行 DataGrid 按钮设置,例如,

void PerformConditionalGridFormatting()
{
    foreach (GridViewRow row in gvCaseList.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
             ... Add your buttons to the cells here
        }
    }
}

时以及每次回发(即在 OnLoad 处理程序中)时调用该方法:

if (Page.IsPostBack) PerformConditionalGridFormatting();

然后每次执行手动数据绑定 这种方法是您不必在每次回发时进行数据绑定,从而节省资源。

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.

void PerformConditionalGridFormatting()
{
    foreach (GridViewRow row in gvCaseList.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
             ... Add your buttons to the cells here
        }
    }
}

Then you call the method every time you perform a manual databind, and also on every postback i.e. in your OnLoad handler do:

if (Page.IsPostBack) PerformConditionalGridFormatting();

The advantage of this approach is that you don't have to databind on every postback, which saves resources.

樱娆 2024-08-12 19:49:01

为 gridview 创建一个 RowCommand 事件处理程序并检查命令名称以查看是否是您的按钮触发了它

的效果

void gridview1_RowCommand(object sender, args e)
{

if (e.CommandName == "Booking")
{
// call your desired method here
}

}

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

void gridview1_RowCommand(object sender, args e)
{

if (e.CommandName == "Booking")
{
// call your desired method here
}

}
说不完的你爱 2024-08-12 19:49:01

把grid的绑定事件放进去不回发。

Put the binding event of grid in to not post back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文