First Add a Webuser control to your project and name it as “Alphabetic_Control.ascx”. Then add 26 link buttons. Each button has “Command Name” and “onclick” events.
“Command Name” contains the text of the link button like A, B, etc. and all “onclick” events of 26 link button calls “SelectList_Alpha” function.
The final user control code is:
- <asp:LinkButton ID="lnkA" runat="server" CommandName="A" Text="A" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkB" runat="server" CommandName="B" Text="B" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkC" runat="server" CommandName="C" Text="C" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkD" runat="server" CommandName="D" Text="D" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkE" runat="server" CommandName="E" Text="E" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkF" runat="server" CommandName="F" Text="F" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkG" runat="server" CommandName="G" Text="G" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkH" runat="server" CommandName="H" Text="H" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkI" runat="server" CommandName="I" Text="I" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkJ" runat="server" CommandName="J" Text="J" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkK" runat="server" CommandName="K" Text="K" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkL" runat="server" CommandName="L" Text="L" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkM" runat="server" CommandName="M" Text="M" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkN" runat="server" CommandName="N" Text="N" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkO" runat="server" CommandName="O" Text="O" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkP" runat="server" CommandName="P" Text="P" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkQ" runat="server" CommandName="Q" Text="Q" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkR" runat="server" CommandName="R" Text="R" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkS" runat="server" CommandName="S" Text="S" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkT" runat="server" CommandName="T" Text="T" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkU" runat="server" CommandName="U" Text="U" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkV" runat="server" CommandName="V" Text="V" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkW" runat="server" CommandName="W" Text="W" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkX" runat="server" CommandName="X" Text="X" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkY" runat="server" CommandName="Y" Text="Y" OnClick="SelectList_Alpha"></asp:LinkButton> -
- <asp:LinkButton ID="lnkZ" runat="server" CommandName="Z" Text="Z" OnClick="SelectList_Alpha"></asp:LinkButton>
Write the “SelectList_Alpha” function in the code behind. The complete code is..
The User control designing is completed, Now we have to generate a “Click” events to this User Control. The reason to create “Click” event is when we take this user control to other page it will not fire the click event. We have to create a “Click” event explicitly. For this we need to create a “Delegate” and “Event Handler”. It can shown as..
- public delegate void ClickEventHandler(object sender, EventArgs e);
- public event ClickEventHandler Click;
- protected void SelectList_Alpha(object sender, EventArgs e)
- {
- LinkButton lnk = (LinkButton)sender;
- Click(sender, e);
- }
Now create new page as “AlphaTest.aspx” in this page go to “design” view and drag the user control which we created.
Now go the code behind of “AlphaText.aspx”. In the page_load event create a delegate. The delegate can be created as.
The above code can be achieved as, First the ID of the User control, i.e, AplhaBeticcontrol1. when we put a “.” Inside that it will show all the methods and events. In that one select “Click”. Then put a “+”and “=” automatically designer will tell you to press the “Tab” button. Press 2 times it will generate the “Click” event.
In the click event write the fallowing code..
- protected void Alphabetic_Control1_Click(object sender, EventArgs e)
- {
- LinkButton lnkbtn = (LinkButton)sender;
- Response.Write("You Selected : " + lnkbtn.CommandName.ToString());
- }
Now run the "AlphaTest.aspx" page, when ever you click on the user control a event will be fired.
Download Sample Code
3 comments:
The event 'Alphabetic_Control1_Click' is not firing. I'm not getting any output. Can you please check?
Thanks.
Good Work
Shyam
www.shyamsrinivas.com
Hi,
This code does not work It's giving an exception
are you missing using the directive or assembly reference
Not sure what's missing.
Post a Comment