Setting the navigation URL for a hyperlink dynamically

I am trying to set a navigation url to a hyperlink that is inside a gridview.

Im creating a table inside gridview using a literal in c # backend code.

Now the code looks like inside a GridviewRowDataBound (object sender, GridViewRowEventArgs e)

Literal.Text += "<asp:HyperLink ID='hlContact' runat='server' NavigateUrl='#'>Contact </asp:HyperLink>"; 

I want to set navigation inside this code

If anyone has an idea, this will be helpful.

thanks

+4
source share
4 answers

When we write html content in literal , it will not correctly select asp hyperlink . But when I used the regular "a" tag, it correctly interprets the redirect path.

 literal.Text += "a ID='linkcontact' runat='server' href='" + "www.website./pagename.aspx?ID=" + id + "'>contact</a>"; 
+1
source

You should simply create a HyperLink control instead of trying to add it to a literal:

 HyperLink lnk = new HyperLink(); lnk.Text = "Hello World!"; lnk.NavigateUrl = "~/somefolder/somepage.aspx"; e.Row.Cells[0].Controls.Add(lnk); 

If your approach can work, you can try something like this:

 Literal.Text += String.Format("<asp:HyperLink ID=\"hlContact\" runat=\"server\" NavigateUrl=\"{0}\">Contact</asp:HyperLink>", navigationUrl); 

If you want to use Literal control, I would do something like this:

 Literal.Text += String.Format("<a href=\"{0}\">Contact</a>", navigationUrl); 
+5
source

If you are just trying to just bind data to a HyperLink field in a GridView with an associated field, you can use TemplateField. Here is an example to do it ahead, not the complexity of adding it to the code behind.

 <asp:TemplateField HeaderText="Contact" SortExpression="LastName, FirstName"> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# String.Format("~/Page.aspx?ID={0}", Eval("CustID").ToString()) %>'>Contact</asp:HyperLink>) </ItemTemplate> </asp:TemplateField> 
+2
source

to create a C # menu and submenu. with variables and navigateurl has the following form

  <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"> <Items> <asp:MenuItem> <asp:MenuItem></asp:MenuItem> <asp:MenuItem></asp:MenuItem> </asp:MenuItem> </Items> </asp:Menu> NavigationMenu.Items[0].Text = "xxxxxx"; name of menu MenuItem menu = NavigationMenu.Items[0]; MenuItem submenu = new MenuItem("xxxxxx"); //name of submenu submenu.NavigateUrl = "~/Main/xxxxx.aspx?id=" + id + ""; MenuItem submenu1 = new MenuItem("xxxxxxx");//name of sumbenu1 submenu1.NavigateUrl = "~/Main/xxxxxxx.aspx?id=" + id + ""; menu.ChildItems.Add(submenu); menu.ChildItems.Add(submenu1); 
0
source

All Articles