Question

I have some code behind that I want to call using a LinkButton as it is easier than trying to use a HyperLink.

Something Like this

StringBuilder sb = new StringBuilder();
sb.Append("<p>Please check that you have entered the correct user name If you have, <asp:LinkButton ID='lnkSendEmail' runat='server' onclick='lnkSendEmail_Click'>Click Here</asp:LinkButton> to complete registration</p><br/>if you have not registerd, <a href='Registration.aspx'>Click here</a>");
labTester.Text = sb.ToString();

When the page however reneders, the LinkButton is not clickable.

Is it possible to do what I am attempting?

Regards

Was it helpful?

Solution

Asp.net will not render that linkbutton tag written as string. If you want to create linkbutton dynamically, then you have to create it as dynamic control and add it to any parent control like PlaceHolder or Panel. You can use following code for reference:

Add a placeholder on aspx page.( suppose it's id is PlaceHolder1)

.cs file code:

LinkButton lnkSendEmail = new LinkButton();
lnkSendEmail.ID = "lnkSendEmail"; 
lnkSendEmail.Text = "Click Here";
lnkSendEmail.Click += new EventHandler(lnkSendEmail_Click);
PlaceHolder1.Controls.Add(new LiteralControl("Please check that you have entered the correct user name If you have, "));
PlaceHolder1.Controls.Add(lnkSendEmail);
PlaceHolder1.Controls.Add(new LiteralControl("to complete registration</p><br/>if you have not registerd, <a href='Registration.aspx'>Click here</a>"));

This will add text and linkbutton, also you can use that onclick event of link button.

protected void lnkSendEmail_Click(object sender, EventArgs e)
{
  //write your code to handle link button click event
}

OTHER TIPS

<p> within an <a> element is illegal, links would go within the paragraphs instead. That is to say, if you want a <p> wrapper, then put it around your link and insert the plain-text into the links Text property.

Oh, and only now seeing that later on you try to write out other stuff, specifically unencoded content that won't be rendered in the way you expect. .NET won't process your embedded LinkButton because the Text property is treated just as that, text.

Does it even render as LinkButton??, you can have anchor tag with defined event to handle this scenarion, but adding LinkButton like this wont work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top