Delegates and events:
Handle or Subscribe to ASP.NET Controls events from UserControl in ASPX Page.
Step 1:
Create a usercontrol having a textbox and button control
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
<input type="text" name="Text1" size="4" maxlength="6" ID="Text1" runat="server"/>
<asp:Button ID="btnSubmit" runat="server" Text="Go"
OnClientClick="javascript:return clickherego(this.form);"
style="color:White;background-color:#75bde9;border-style:None;width:25px;height:20px;font-size:10px;font-family:verdana"
onclick="btnSubmit_Click"/> </asp:Panel>
Explanation:
DefaultButton="btnSubmit" in panel control will trigger btnSubmit button click when user presses enter key or return key in Mac.
Step 2:
Inherit EventArgs Class to Pass values to Event Handler. This will enable us to get the user control textbox values in ASPX page.
public class NavEventArgs : EventArgs
{
private int _text;
public int SearchText
{
get { return _text; }
}
public NavEventArgs(int searchtext)
{
this._text = searchtext;
}
}
Step 3:
Create an event in UserControl1.ascx.cs file
private bool _goClicked;
public bool GoClicked
{
get { return _goClicked; }
set { _goClicked = value; }
}
private int _text1value;
public int Text1Value
{
get { return _text1value; }
set { _text1value = value; }
}
public delegate void NavEventHandler(object sender, NavEventArgs e );
public event NavEventHandler GoButtonClicked;
Step 4:
Fire the GoButtonClicked event in submit button clicked event.
protected void btnSubmit_Click(object sender, EventArgs e)
{
GoClicked = true;
Text1Value = Convert.ToInt32(Text1.Value);
Text1.Value = string.Empty;
if (GoButtonClicked != null)
{
//this value can be accessed using e.SearchText in EventHandler
NavEventArgs e1 = new NavEventArgs(Text1Value);
//Fire the event
GoButtonClicked(this, e1);
}
}
Entire UserControl1.ascx.cs file
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class dinning_navigation : System.Web.UI.UserControl
{
private bool _goClicked;
public bool GoClicked
{
get { return _goClicked; }
set { _goClicked = value; }
}
private int _text1value;
public int Text1Value
{
get { return _text1value; }
set { _text1value = value; }
}
public delegate void NavEventHandler(object sender, NavEventArgs e );
public event NavEventHandler GoButtonClicked;
protected void Page_Load(object sender, EventArgs e)
{
GoClicked = false;
Text1.Attributes.Add("onkeydown", "if(event.keyCode== 13){document.getElementById('" + btnSubmit.UniqueID + "').click();}");
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Response.Redirect("search_result.aspx?text=" + Text1.Value);
GoClicked = true;
Text1Value = Convert.ToInt32(Text1.Value);
Text1.Value = string.Empty;
if (GoButtonClicked != null)
{
NavEventArgs e1 = new NavEventArgs(Text1Value);
GoButtonClicked(this, e1);
}
}
}
public class NavEventArgs : EventArgs
{
private int _text;
public int SearchText
{
get { return _text; }
}
public NavEventArgs(int searchtext)
{
this._text = searchtext;
}
}
Step 5:
Handle GoButtonClicked event in ASPX
Step 5.1:
Add the user control to EventsTest.aspx page
<uc1:dinning_navigation ID="Dinning_navigation1" runat="server" OnGoButtonClicked="dinning_navigation_GoButtonClicked" />
Event Handler for GoButtonClicked. Now OnGoButtonClicked event will automatically showed by Intellisense in visual studio. dinning_navigation_GoButtonClicked is the EventHandler name I have given.
OnGoButtonClicked="dinning_navigation_GoButtonClicked"
Step 5.3:
EventHandler for GoButtonClicked event in EventsTest.aspx.cs file.
protected void dinning_navigation_GoButtonClicked(object sender, NavEventArgs e)
{
FillGrid4GoButtonClicked(e.SearchText);
GridView1.PageIndex = 0;
GridView1.DataBind();
}
No comments:
Post a Comment