0

我将数据绑定到 GridView 控件。最后一列之一具有图像 URL 链接。

     <asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False" Width="100%" OnRowCommand="gvccase_RowCommand" RowStyle-CssClass="rowHover">……..


<asp:TemplateField>
   <ItemTemplate>
   <asp:ImageButton ID="lnbEdit" AlternateText="edit" ImageUrl="~/images/edit_icon.gif"
                                                runat="server" CommandName="cmdEdit" />
                                        </ItemTemplate>
                                        <HeaderTemplate>
                                            Edit
                                        </HeaderTemplate>
                                    </asp:TemplateField>

当用户单击最后一列时,将调用 OnRowCommnd 事件(protected void gvccase_RowCommand(object sender, System.Web.UI.WebControls.CommandEventArgs e))。

如何读取用户点击了哪一行?第一列有案例#。我想阅读“OnRowCommand”中的案例#。怎么做?

或者我想要完成的只是....当用户单击特定行时,我想将他们带到详细信息页面。

谢谢你,史密斯

4

3 回答 3

0

为什么不做类似的事情:

<asp:GridView runat="server" ID="gvMajor" EmptyDataText="Your search terms did not match any records">
    <Columns>
        <asp:BoundField DataField="MajorCode" HeaderText="Major Code" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="lbModify" runat="server" CommandArgument='<%# Eval("MajorId") %>' OnClick="lbModify_Click" ImageUrl="~/Modify.png"   />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

然后在代码隐藏中:

protected void lbModify_Click(object sender, EventArgs e)
{
    var editLink = ((ImageButton)sender);
    //editLink.CommandArgument == MajorId
}

只需确保将 gridview 绑定到正确的数据源,如下所示:

    DataTable dt = new DataTable();
    dt.Columns.Add("MajorId");
    dt.Columns.Add("MajorCode");

    //start loop here if needed
    DataRow dr = dt.NewRow();

    dr["MajorId"] = 1;
    dr["MajorCode"] = "MAJOR CODE 1";
    dt.Rows.Add(dr);
    //end loop

    gvMajor.DataSource = dt;
    gvMajor.DataBind();
于 2012-11-28T00:14:10.703 回答
0

来自http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewcommandeventargs.aspx

  void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);

  }
于 2012-11-28T00:00:37.317 回答
0

你可以试试这个。

HTML

  <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowCommand="GridView1_RowCommand">
      <Columns>
          <asp:TemplateField>
              <ItemTemplate>
                  <asp:Label ID="lblDatos" Text='<%#Eval("nombre")%>' runat="server" >
                  </asp:Label>
              </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField>
              <ItemTemplate>
                  <asp:ImageButton ID="lnbEdit" AlternateText="edit" CommandArgument='<%#Eval("link")%>'
                                            runat="server" CommandName="cmdEdit" />
              </ItemTemplate>
          </asp:TemplateField>
      </Columns>
  </asp:GridView>    

代码背后

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
            this.llenar();

    }

    void llenar()
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("nombre");
        dt.Columns.Add("link");
       DataRow dr= dt.NewRow();
       DataRow d1 = dt.NewRow();

       dr["nombre"] = "google";
       d1["nombre"] = "amazon";
       dr["link"]  = "http://google.com";
       d1["link"] = "http://amazon.com";
       dt.Rows.Add(dr);
       dt.Rows.Add(d1);

        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();


    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "cmdEdit")
        {
            Response.Redirect(e.CommandArgument.ToString());

        }
    }
}
于 2012-11-28T00:58:31.183 回答