ASP.NET - Problem with haper link control - Asked By mostafa hamdy on 27-Mar-12 10:36 AM

Hello all
I have some web page contains asp hyperlink control created dynamically as the following:

HyperLink lbContent = new HyperLink();


lbContent.ID =

"lnk" + i.ToString();

lbContent.NavigateUrl = URL;

lbContent.Text = Title;
and I set this hyper link in an update panel control , but when I click this link the page is posted back and rloaded again , I just want the hyperlink as navigator to the URL I sent and dont make any post back to the page , please if any body get what I mean and can help me please send me or tell me about some URL may help me in doing that
Regard
Mostafa

dipa ahuja replied to mostafa hamdy on 27-Mar-12 11:03 AM
Make sure you have declare the hyperlink on page_load. And what the url contains ? is it valid url?

or take the linkButton and implement the click event :

protected void Page_Load(object sender, EventArgs e)
{
  LinkButton link = new LinkButton();
  link.Text = "Home";
 
  link.Click += link1_Click;
}
protected void link1_Click(object sender, EventArgs e)
{
  Response.Redirect("home.aspx");
}



[)ia6l0 iii replied to mostafa hamdy on 27-Mar-12 11:22 AM
I don't think it has to do anything with the update panel, as you say you want to just navigate to the url. 

I hope you want to just navigate to it in a new blank window: Use the "Target" property to open it in a new window.
<asp:HyperLink ID="myHyperLink"
runat="server" 
NavigateUrl='someurl'
Target="_blank">

</asp:HyperLink>

Hope this helps.
kalpana aparnathi replied to mostafa hamdy on 27-Mar-12 01:31 PM
hi,

For avoiding postback due to the clicking of the hyperlink . You must return true after the OnClientClick:
OnClientClick="this.disabled='true';return true;"

Any clicks in client-side must return true if the postback is to continue, it's a means of providing client-side validatin on button presses.


Hope will helps!!!!!!!!!!

Regards,
Somesh Yadav replied to mostafa hamdy on 28-Mar-12 01:46 AM
You could do this in the codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
      string pageid = "123";
      MyLink.NavigateUrl = string.Format("../mypage.aspx?id={0}", pageid);
    }
}

UPDATE:

Now that @Marko Ivanovski pointed me in the comments that this hyperlink is inside a GridView which I didn't notice in the beginning the easiest would be to use databinding (<%# syntax):

<asp:TemplateColumn>
    <ItemTemplate>
      <asp:HyperLink
        id="MyLink"
        NavigateUrl='<%# Eval("pageid", "~/mypage.aspx?id={0}")  %>'
        runat="server">
      My Page
      </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateColumn>

In this case pageid is a property of the data source.

UPDATE 2:

Do you really need a server side control? How about:

<a href="<%= this.ResolveUrl("~/mypage.aspx?id=" + pageid) %>">
    My Page
</a>