"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Thursday, April 21, 2011

Multiple Control having One event OR selecting alphabet

Hi *.*,
Ha ha both sounds different isit? Actually i'm had a list alphabet (linkbutton) Everyone having same click Event to make may code less and sexy. here its go >>In some situations we may have a requirement to search the records Alphabetic wise. For these type of requirement we have to take 26 alphabets and when we have to generate click events to know which alphabet clicked. Its not a good way to generate 26 button click events. Instead of that we can take Command Name and we will call the same function. And if we make a user control, this control can used many times in our project.


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.

On user control Design part :
 <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="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> 
 
Code on UserControl
 
 Add this Name Space
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
  public delegate void ClickEventHandler(object sender, EventArgs e); 
  public event ClickEventHandler Click;    
 protected void Page_Load(object sender, EventArgs e)   
 {    
 }   
  protected void SelectList_Alpha(object sender, EventArgs e) 
   {      
  LinkButton lnk = (LinkButton)sender; 
  Click(sender, e);  
  } 
On Aspx 
 
Register that user control
<%@ Register Src="Alphabetic_Control.ascx" TagName="Alphabetic_Control" TagPrefix="uc1" %> 
 
Add control this way
  <uc1:Alphabetic_Control ID="Alphabetic_Control1" runat="server" />

 On Aspx.cs

Add these name space 
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

Adding an event to that control
    protected void Page_Load(object sender, EventArgs e)
    {
      Alphabetic_Control1.Click += new 
 Alphabetic_Control.ClickEventHandler(Alphabetic_Control1_Click);       
    }
 
    protected void Alphabetic_Control1_Click(object sender, EventArgs e)
    {
        LinkButton lnkbtn = (LinkButton)sender;
        Response.Write("You Selected : " + lnkbtn.CommandName.ToString());
    }

Download Sample from here

Hope u got it.
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: