【问题标题】:Adding functionality to an imageButton in a gridview向 gridview 中的 imageButton 添加功能
【发布时间】:2011-08-17 03:23:27
【问题描述】:

我有一个 ImageButton 控件作为 GridView 控件的一部分,该控件显示为 ItemTemplate 并在同一个 GridView 中。我有一个常规的 Button 控件,我在其中添加了一些类似这样的代码

if (e.CommandName == "addToSession")
{
    int index = Convert.ToInt32(e.CommandArgument);

    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

    string ISBN = selectedRow.Cells[0].Text;
    string bookTitle = selectedRow.Cells[1].Text;
    string image = selectedRow.Cells[2].Text;

    //storing title, author, pictureUrl into session variables to 'carry them over' to RateBook.aspx
    Service s = new Service();
    Session["ISBN"] = ISBN;
    Session["bookTitle"] = bookTitle;
    Session["ImageUrl"] = s.returnImageUrl(bookTitle);

    if (Session["userName"] == null)
    {
        Response.Redirect("registerPage.aspx");
    }
    else
    {
        Response.Redirect("RateBook.aspx");
    }
}
else if (e.CommandName == "ratedBooks")
{
    int index = Convert.ToInt32(e.CommandArgument);

    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

    string bookTitle = selectedRow.Cells[1].Text;

    Service s = new Service();

    Session["ImageUrl"] = s.returnImageUrl(bookTitle);

    Response.Redirect("BookRated.aspx");
}

当我运行此代码时,我得到一个格式异常,我又不知道为什么。我稍微改变了图像按钮并将图像嵌套在链接按钮中,这似乎更正确。

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="ratedBooks">
            <asp:Image ID="ImageButton1" ImageUrl='<%#Eval("pictureUrl") %>' runat="server" />   

        </asp:LinkButton>    
    </ItemTemplate>
</asp:TemplateField>

请指教。

问候,

阿里安

【问题讨论】:

  • @marco:在 GridView TemplateField 的 ItemTemplate 上下文中,您绝对可以拥有 Eval() 语句。
  • 嗨...我删除了我的第一条评论...感谢您的建议!

标签: c# asp.net


【解决方案1】:

我相信您可以使用ImageButton 来满足您的需求,因为它支持所有主要的Button 功能,包括CommandName(请参阅http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.commandname.aspx)。

试试这个:

    <asp:ImageButton ID="LinkButton1" runat="server"
                     CommandName="ratedBooks"
                     ImageUrl='<%#Eval("pictureUrl") %>' />

另外,请注意您的格式异常可能来自以下行:

Convert.ToInt32(e.CommandArgument);

原因是这段代码 sn-p 似乎没有为按钮的 CommandArgument 分配值。 Convert.ToInt32 需要传入一个有效的整数值,这意味着ImageButton 需要将一个数字绑定到其CommandArgument 属性。

如果您的解决方案基于此 MSDN 参考 (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx),请注意 &lt;asp:ButtonField&gt; 列类型得到特殊处理; CommandArgument 设置为行索引。当您使用模板字段时,ASP.NET 要求您指定或数据绑定您自己的命令参数。

更新

此问题包含有关将网格视图行索引绑定到自定义按钮的详细信息:

ASP.NET GridView RowIndex As CommandArgument

【讨论】:

    【解决方案2】:

    一种可能的解决方案是添加 ItemDataBound 事件处理程序并搜索图像按钮更改其图像 url。

    MyGrid.RowDataBound += new RepeaterItemEventHandler(MyGrid_RowDataBound);
    
    void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                ImageButton image = e.Row.FindControl("MY_IMAGE_CONTROL") as ImageButton;
    
                image.ImageUrl = "PATH_TO_IMAGE";
            }
        }
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      你需要 CommandArgument

      <asp:TemplateField ShowHeader="False">
      <ItemTemplate>
          <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Delete" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"                                                                             ImageUrl="~/Modelos/Img/deleted.gif" />
      </ItemTemplate>
      </asp:TemplateField>
      

      C# 背后的代码

      public void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
              {
                  string t;
                  if (e.CommandName == "Delete")
                  {
                      int index = Convert.ToInt32(e.CommandArgument);
                      GridViewRow selectedRow = grid1.Rows[index];
                      t = selectedRow.Cells[2].Text;
                  }
              }
      

      在 ASPX 上

      <asp:GridView ID="grid1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" ...
      

      【讨论】:

        猜你喜欢
        • 2020-06-09
        • 1970-01-01
        • 2010-09-07
        • 2017-12-06
        • 1970-01-01
        • 2012-07-22
        • 2015-09-12
        • 1970-01-01
        相关资源
        最近更新 更多