Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView HyperLink field in C#

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.

like image 527
Sam Avatar asked Oct 10 '12 15:10

Sam


People also ask

How to make a column in GridView as hyperlink in c#?

protected void Page_Load(object sender, EventArgs e) {DataSet dt = GetExistingRecords(); GridView1. DataSource = dt; GridView1.

What is a hyperlink field?

The Hyperlink field shows the title or explanatory text for a hyperlink associated with a task, resource, or assignment. This text appears when you rest the pointer on the hyperlink indicator in the Indicators field. There are several categories of Hyperlink fields. Data Type Text.

How to create hyperlink column in GridView in asp net?

Hover your mouse over the GridView and click the arrow that appears in the top right. Go to "Choose Data Source" and select "new data source..." Create the connection string to your database and choose the NewsHeadline table.


1 Answers

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); 
    } 
 } 
}
like image 131
chridam Avatar answered Oct 11 '22 03:10

chridam