4

Im trying to set navigation url to a hyperlink which is inside a gridview.

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

The code now look like inside GridviewRowDataBound(object sender, GridViewRowEventArgs e)

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

I want to set the navidation inside this code

If anyone have an idea it will be helpful

Thanks

1
  • This will just render the text "<asp:HyperLink..." on your page, not actually create a control and add it to viewstate. Bummer, but this approach will never work. Commented Sep 12, 2011 at 18:02

4 Answers 4

5

You should just create a HyperLink control, instead of trying to add one 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 could 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 a Literal control though, I would do something like this instead:

Literal.Text += String.Format("<a href=\"{0}\">Contact</a>", navigationUrl); 
Sign up to request clarification or add additional context in comments.

2 Comments

Perhaps I'm misunderstanding the question or context, but won't that simply render "<asp:Hyperlink..." to the response being sent back to the browser? At what point will the aspx page "re-process" the literal's text so that "<asp:Hyperlink..." is rendered as "<a href=..."?
@mikemanne: That was my thought as well, hence my first suggestion to add a HyperLink control to a parent container of some sort.
4

If you are just trying to simply databind a HyperLink field in a GridView with a bound field, you can use a TemplateField. Here is an example to do it up front and not the hassle of adding it in 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>

Comments

1

When we writing html content to the literal, it won't picking up the asp hyperlink correctly. But when I used the normal “a” tag, it taking the redirection path correctly.

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

Comments

0

for make a menu and submenu code c#. with variables and navigateurl is of form next

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

1 Comment

Seems you went on a different direction! This is not about creating a menu with navigation. This is a link + some other information inside a grid-view column where data been appended to a literal. Seems you joined the site today. Appreciate your contribution. But have a look at the questions where there aren’t any accepted answers. If answer is accepted you will see a check mark on left to the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.