I have following code that adds dynamic LinkButton on the click event of a normal button. The LinkButtons need a EventHandler “LinkButton_Click“
. Since the event handler need to be registered while Page_Load/Page_Init
itself, I am first adding all the possible links within Page_Load
(in postback scenario). (There are totally fours linkbuttons; only two will be displayed in the screen based on business scenario)
The code is working fine and the event handlers are getting invoked on click event.
Note: The business scenario is – if the current time’s second portion is less than 30, the first two LinkButtons need to be displayed; if greater than 30 the last two LinkButtons need to be displayed.
Note: The controls are cleared and added again inside PopulateLinksBasedOnCriteria()
method
QUESTIONS
- In the
PopulateLinksBasedOnCriteria()
method, I am newly creating the link button instances. Still the event handler is firing. This is working because the ID that got added in thePage_Load
is same. Is there any MSDN reference that substantiate this behavior? (I.e. the event handler will be based on the IDs registered whilePage_Load/Page_Init
. This will work even if the controls are cleared and added again) - There is duplicate code in creating LinkButtons – one inside
Page_Load
and other insidePopulateLinksBasedOnCriteria()
method. Is there a better way to achieve this business task without duplicate code?
MarkUp
<form id="form1" runat="server">
<div>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<br />
<asp:PlaceHolder ID="plhDynamicLinks" runat="server"></asp:PlaceHolder>
</div>
</form>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
LinkButton lnk1 = new LinkButton();
lnk1.ID = "D1";
lnk1.Text = "A";
//Event handler must be registered in the Page_Load/Page_Init
lnk1.Click += new EventHandler(LinkButton_Click);
plhDynamicLinks.Controls.Add(lnk1);
LinkButton lnk2 = new LinkButton();
lnk2.ID = "D2";
lnk2.Text = "B";
lnk2.Click += new EventHandler(LinkButton_Click);
plhDynamicLinks.Controls.Add(lnk2);
LinkButton lnk3 = new LinkButton();
lnk3.ID = "D3";
lnk3.Text = "C";
lnk3.Click += new EventHandler(LinkButton_Click);
plhDynamicLinks.Controls.Add(lnk3);
LinkButton lnk4 = new LinkButton();
lnk4.ID = "D4";
lnk4.Text = "D";
lnk4.Click += new EventHandler(LinkButton_Click);
plhDynamicLinks.Controls.Add(lnk4);
}
}
protected void LinkButton_Click(object sender, EventArgs e)
{
PopulateLinksBasedOnCriteria();
LinkButton clickedControl = (LinkButton)sender;
Response.Write(DateTime.Now.ToString()+"___"+ clickedControl.ID + " Link Button Clicked" );
}
protected void btnAdd_Click(object sender, EventArgs e)
{
PopulateLinksBasedOnCriteria();
}
private void PopulateLinksBasedOnCriteria()
{
plhDynamicLinks.Controls.Clear();
if (DateTime.Now.Second < 30)
{
LinkButton linkButton1 = new LinkButton();
linkButton1.ID = "D1";
linkButton1.Text = "1";
plhDynamicLinks.Controls.Add(linkButton1);
LinkButton linkButton2 = new LinkButton();
linkButton2.ID = "D2";
linkButton2.Text = "2";
plhDynamicLinks.Controls.Add(linkButton2);
}
else
{
LinkButton linkButton3 = new LinkButton();
linkButton3.ID = "D3";
linkButton3.Text = "3";
plhDynamicLinks.Controls.Add(linkButton3);
LinkButton linkButton4 = new LinkButton();
linkButton4.ID = "D4";
linkButton4.Text = "4";
plhDynamicLinks.Controls.Add(linkButton4);
}
}