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
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
}