【问题标题】:Dynamic parameter passing through hyperlink in a DataGrid in asp.net(C#)在asp.net(C#)中通过DataGrid中的超链接传递动态参数
【发布时间】:2011-06-07 11:27:14
【问题描述】:
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" 
        CellPadding="4">
    <Columns>
    <asp:BoundField DataField="FileID" HeaderText="FileID" />
    <asp:BoundField DataField="FilePath" HeaderText="FilePath" />
    <asp:BoundField DataField="UploadedBy" HeaderText="CreatedBy" />
    <asp:BoundField DataField="CreatedDate" HeaderText="CreatedDate" /> 


        <asp:HyperLinkField HeaderText="LINK" NavigateUrl="show.aspx" Text="SHOW" />


    </Columns>



      conn.Open();
             SqlDataReader rdr = comm.ExecuteReader();
             if (NAME.Equals("admin"))
             {
                 GridView1.DataSource = rdr;
                 GridView1.DataBind();
             }
             else
             {
                 GridView2.DataSource = rdr;
                 GridView2.DataBind();
             }
             rdr.Close();

我想使用 Gridview 中的超链接根据单击的行动态传递值。因为我是新手,所以我不能这样做。请任何人帮助我。

【问题讨论】:

    标签: c# asp.net gridview hyperlink parameter-passing


    【解决方案1】:

    设置hyperlink NavigateUrl 属性如...NavigateUrl='&lt;%# Eval("FileID", "show.aspx?ID={0}" %&gt;'

    NavigateUrl='<%# Eval("FileID", "show.aspx?ID={0}") + "&FilePath=" + Eval("FilePath") %>'
    

    【讨论】:

    • 它给出错误说“数据绑定表达式仅支持具有 DataBinding 事件的对象。System.Web.UI.WebControls.HyperLinkField 没有 DataBinding 事件。”
    • 错误仍然存​​在。什么是 DataBinding 事件?
    • 我不知道,但我测试过上面的解决方案。
    【解决方案2】:

    在网格中添加 onRowCommand 事件

    OnRowCommand="OnRowCommand_GridView1"
    

    然后用 CommandName 和 CommandArgument 定义链接 Button

          <asp:LinkButton ID="lnk1" runat="server" Text="DoClick"  CommandName="Select" CommandArgument='<%#Eval("FileID") %>'></asp:LinkButton>
    

    而不是后面的代码

    protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
    
           if (e.CommandName == "Select")
            {
              int MyFileID = e.CommandArgument;
           //Now Perfrom here ur desired action
           }
    

    希望这会对你有所帮助。

    【讨论】:

      【解决方案3】:

      查看GridView的onRowCommand

      检查以下链接: How to get the current row in GridView Row Command Event?

      有关行命令的信息:GridView.RowCommand Event

      【讨论】:

      • 你能不能详细点。我今天第一次使用DataGrid。
      猜你喜欢
      • 2012-06-03
      • 2021-12-12
      • 2017-04-29
      • 2015-03-01
      • 2016-06-21
      • 2020-02-15
      • 1970-01-01
      • 2011-08-16
      • 2015-04-04
      相关资源
      最近更新 更多