0

i use this code in asp.net:

string link = "<a href=\"" + Request.Url.AbsolutePath + "?Id=" + x + ">" + "link" + "</a>";
literalLink.Text = link;

<asp:Literal ID="literalLink" runat="server"></asp:Literal>

but i have postback in this link.

shahin ko
  • 253
  • 2
  • 4
  • 13

2 Answers2

2

If I understand correctly (and I may not), you just want to display the link after a PostBack. If that's the case, you can do it like this:

ASPX Code:

<asp:HyperLink ID="visitAgain" Text="Link" Visible="False" runat="server" />

Code Behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        visitAgain.Visible = true;
        visitAgain.NavigateUrl = Request.Url.AbsolutePath + "?Id" + x;
    }
}

Doing it like this instead of having all the logic in the .aspx file has the added bonus of improved readability and cleaner code.

Meryovi
  • 6,121
  • 5
  • 42
  • 65
  • ohhhhh!! noooooo :-) i want this code for my project: http://stackoverflow.com/questions/2476982/how-to-let-html-link-anchor-do-a-postback-to-be-like-linkbutton – shahin ko Sep 01 '13 at 20:58
1

If you want a PostBack event to the page you're working on using a link, use the <asp:LinkButton> element. It is the same as an ordinary button except that it displays as a common link.

<asp:LinkButton runat="server" Id="lnkButton" Text="I do a postback, yay" OnClick="TheFunctionThatHandlesTheClickIfNecessary" />
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104