GridView HyperLink field in C#

后端 未结 5 587
梦如初夏
梦如初夏 2020-12-17 16:43

Take a look at the following code:



        
相关标签:
5条回答
  • 2020-12-17 17:35

    Use the DataNavigateUrlFields property, comma-delimited value with the fields for parameters in "KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"

    <asp:HyperLinkField DataNavigateUrlFields="Keyword,State,City"
                        DataNavigateUrlFormatString="KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}" 
                        Text="View Details" />
    

    A couple of examples:

    Passing two arguments in DataNavigateUrlFormatString in hyperlink field of .NET 2.0 Grid-View

    Pass Multiple Values from a GridView to Another Page using ASP.NET

    EDIT:

    Set NavigateUrl of HyperLink in RowDataBound event of GridView

    <asp:GridView ID="GridView1" runat="server" 
                  AutoGenerateColumns="False" 
                  DataKeyNames="Keyword"
                  DataSourceID="SqlDataSource1" 
                  onrowdatabound="GridView1_RowDataBound">
       <asp:TemplateField HeaderText="Keyword"  ItemStyle-HorizontalAlign="Center"          FooterStyle-HorizontalAlign="Center">
          <ItemTemplate>
              <asp:HyperLink ID="link" runat="server" Text='<%# Eval("Keyword") %>' />
          </ItemTemplate>
        </asp:TemplateField>
        .......
    </asp:GridView>
    

    Code behind:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
        HyperLink hl = (HyperLink)e.Row.FindControl("link"); 
        if (hl != null) 
        { 
            DataRowView drv = (DataRowView)e.Row.DataItem; 
            string keyword = drv["Keyword"].ToString(); 
            string state = Request.QueryString["State"]; 
            string city = Request.QueryString["City"]; 
            hl.NavigateUrl = "~/KeywordSrchSumDtl.aspx?Keyword=" + keyword + "&State=" + Server.UrlEncode(state) + "&City=" + Server.UrlEncode(city); 
        } 
     } 
    }
    
    0 讨论(0)
  • 2020-12-17 17:39

    You may initialize DataNavigateUrlFields from Code Behind with string[]:

    yourHyperLinkField.DataNavigateUrlFields = new string[] { "Keyword", "State", "City" };
    
    0 讨论(0)
  • 2020-12-17 17:41

    Finally it Navigates by the following code,

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
          HyperLink hl = (HyperLink)e.Row.FindControl("Link");
          if (hl != null)
          {
               DataRowView drv = (DataRowView)e.Row.DataItem;
               string keyword = drv["Keyword"].ToString().Trim();
               string state = strState.ToString().Trim();
               string city = strCity.ToString().Trim();                 
               hl.NavigateUrl = "KeywordSrchSumDtl.aspx?Keyword=" + keyword + "&Geo=" + geo                             + "&Site=" + site;
           }
           }
          }
    

    Thank u guys for the help.

    0 讨论(0)
  • 2020-12-17 17:46
    Some time we need to pass multiple parameters with hyperlink in Gridview, datagrid or any data list control then we can use following code:-</br> 
    
    **CODE:-** 
    <asp:GridView ID="gvFin" runat="server" CellPadding="1" AutoGenerateColumns="false"> 
    
    <Columns>
         <asp:TemplateField ItemStyle-Width="4%" HeaderStyle-Width="4%" SortExpression="CDL"
        HeaderText="CDL#" HeaderStyle-Font-Bold="true">
                                                    <ItemTemplate>
       <asp:HyperLink ID="lnk1" runat="server" 
       Text='<%# DataBinder.Eval(Container.DataItem,"TestValue") %>'
       NavigateUrl='<%# "javascript:ShowACP(\"" + DataBinder.Eval(Container.DataItem, "ID")     + "\",\"" + DataBinder.Eval(Container.DataItem,"ACCOUNTPLAN") + "\");" %>'                                                  ForeColor="Blue" /  </ItemTemplate>
     </asp:TemplateField>
    
    
    **JavaScript Function**
    
    function ShowACP(id, acplabel) 
    {            if (acplabel == "No")
    {
                    window.location = "#";
                }
                else</br> 
                    window.location = "Default.aspx?gid=" + id;
            }
    
    0 讨论(0)
  • 2020-12-17 17:48

    You can try with string.Format method

    NavigateUrl='<%# String.Format("KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}", DataBinder.Eval(Container.DataItem, "Keyword"), Request.QueryString["State"], Request.QueryString["City"]) %>'
    
    0 讨论(0)
提交回复
热议问题