Figer's Technology Consulting | C# eventhandler passing variables from Usercontrol to parent page

C# eventhandler passing variables from Usercontrol to parent page

In this example I'm passing a telerik radgrid collection to the parent page from the child usercontrol when the user selects a row.


-----------------------------

User Controls (.ascx)

-----------------------------


public partial class NPISearch : System.Web.UI.UserControl 

//This is just the standard class section when a user control is created</em>


{

<strong>public delegate void ReturnNPISearchEvent (Telerik.Web.UI.GridDataItemCollection e); //You need to add this line</strong>


Page_Load() { 

//Standard Page_load event

protected void grdNPI_SelectedIndexChanged(object sender, EventArgs e) 

//Here is the function that raises the event to the parent page

{

//Null check makes sure the main page is attached to the event

if (this.ReturnNPISearch != null)

this.ReturnNPISearch(grdNPI.MasterTableView.Items);

}


}


-----------------------------------

Parent page (.aspx)

---------------------------------


protected void Page_Load(object sender, EventArgs e) //Add the line below in Page_Load()

{

NPISearch1.ReturnNPISearch += new      MCRASunshine.Account.NPISearch.ReturnNPISearchEvent(MyEventHandlerFunction_ReturnNPISearch);

}


//This is the function that is called when the usercontrol event is fired

public void MyEventHandlerFunction_ReturnNPISearch(Telerik.Web.UI.GridDataItemCollection e)


{

this.divSearch.Visible = false;

this.divAddInfo.Visible = true;

txtInfoNPI.Enabled = false;


//set values in textboxes

foreach (Telerik.Web.UI.GridDataItem dataItem in e)

{

if (dataItem.Selected == true)

{

this.txtInfoFirstName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(dataItem.Cells[4].Text.ToLower().Trim('"'));

this.txtInfoLastName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(dataItem.Cells[3].Text.ToLower().Trim('"'));

this.txtInfoNPI.Text = dataItem.Cells[5].Text.Trim('"');

Comments are closed