GridView HyperLink field in C#

2020-02-11 03:36发布

问题:

Take a look at the following code:

<asp:HyperLinkField 
    DataNavigateUrlFields="NameID" 
    DataNavigateUrlFormatString="names.aspx?nameid={0}"
    DataTextField="name" 
    HeaderText="Name" 
    ItemStyle-Width="100px"
    ItemStyle-Wrap="true" />

It takes only the name id to navigate to the next page. How will I include the two other parameters which are not in the gridview. The navigate URL I'm using has to take the keyword which is already present in the gridview and the other two parameters from the database table. I tried using all these codes. Nothing did work for me.

<asp:HyperLinkField DataTextField="Keyword" DataNavigateUrlFields="Keyword"
    DataNavigateUrlFormatString="KeywordSrchSumDtl.aspx?Keyword={0}&State={1}&City={2}"
    HeaderStyle-VerticalAlign="Bottom" ItemStyle-HorizontalAlign="center" />

I cant use the above the code because the state and city are not in the GridView but available in my data table.

I tried using the following code too, but it doesn't work:

 <asp:TemplateField HeaderText="Keyword"  ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:HyperLink ID="link" runat="server" NavigateUrl='<% # "KeywordSrchSumDtl.aspx?Keyword="Eval("Keyword")+"&State="+Request.QueryString["State"]%>' Text='<%# Eval("Keyword") %>'>
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

I also tried this:

        <asp:HyperLink ID="Link1" runat="Server" NavigateUrl='<%#redirectURL()+Server.UrlEncode((Eval("Keyword")).ToString())%>' Text='<%# DataBinder.Eval(Container.DataItem,"Keyword") %>'>
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

.aspx.cs

return "KeywordSrchSumDtl.aspx?Keyword=" + 
    //I DONNO HOW TO CALL THE KEYWORD HERE//
    + "&State=" + System.Web.HttpContext.Current.Request.QueryString["State"]
    + "&City=" + System.Web.HttpContext.Current.Request.QueryString["City"];

I don't know how to solve this.

回答1:

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); 
    } 
 } 
}


回答2:

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"]) %>'


回答3:

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.



回答4:

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;
        }


回答5:

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

yourHyperLinkField.DataNavigateUrlFields = new string[] { "Keyword", "State", "City" };