Hi Shiva_J,
Check with the below code. I took literal control where linkbutton is to be generated.
If you have any control whose Autopostback propery = true then you need not to add following code in aspx, Only add literal control. Because it is automatically generated by .NET when Autopostback property of any control is true.
HTML
<div>
<asp:TextBox runat="server" ID="txtValue" />
<br />
<asp:Literal ID="ltr" runat="server"></asp:Literal>
<%-- Don't add below html if autopostback is true.---%>
<%-----------Start-------------------%>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<script type="text/javascript">
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
</script>
<%-----------End-------------------%>
</div>
Here __doPostBack method is used to fire postback and passed unique information as argument. When user clicks on button then __doPostBack method will be called, which sets two hidden control "__EVENTTARGET" and "EVENTARGUMENT" and submit the form. Now I check __EVENTTARGET on Page_Load if it is dynamic button then lbDynamic_Click is called.
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GenerateButton();
}
if (Request.Form["__EVENTTARGET"] != null && Request.Form["__EVENTTARGET"] == "lbDynamic")
{
lbDynamic_Click(null, null);
}
}
private void GenerateButton()
{
StringBuilder sb = new StringBuilder();
//sb.Append("<input type='button' id='btn' value='Button:' onclick='javascript:__doPostBack(\"lbDynamic\");' >");
sb.Append("<asp:LinkButton ID=\"Link\" OnClick='javascript:__doPostBack(\"lbDynamic\");' runat=\"server\">ProjectNumber</asp:LinkButton>");
sb.Append(" ");
sb.Append("- Hours Approved by");
sb.Append(" ");
sb.Append("ApprovedBy");
ltr.Text = sb.ToString();
}
private void lbDynamic_Click(object sender, System.EventArgs e)
{
Response.Write(txtValue.Text);
}