|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionSometimes it is required to access a user control from another user control that resides on the same page in ASP.NET. This purpose can be achieved in several ways. Here I have two controls. One is an editor and the other one is a list. My target is to create a communication between these two controls. This article describes the way to achieve this goal. Possible WaysThe possible ways to access a user control from another user control are:
This article highlights the second one. Basic MethodologyThe communication between the controls takes place via the container page. One control triggers an event which is caught by the page and then the page accesses the other control using
So there are two events and respective event arguments here: public class PersonEventArgs:EventArgs
{
private bool isSuccess = false;
public bool IsSuccess
{
get { return isSuccess; }
set { isSuccess = value; }
}
public PersonEventArgs(bool success)
{
isSuccess = success;
}
}
public class EditPersonEventArgs:EventArgs
{
private int personId = 0;
public int PersonId
{
get { return personId; }
set { personId = value; }
}
public EditPersonEventArgs(int Id)
{
personId = Id;
}
}
Editor ControlPersonEditor.ascx has the public partial class PersonEditor : System.Web.UI.UserControl
{
public delegate void SavePerson(object sender, PersonEventArgs arg);
public event SavePerson SavedPerson;
protected void btnSave_Click(object sender, EventArgs e)
{
Person newPerson = new Person();
newPerson.Age = Convert.ToInt32(txtAge.Text);
newPerson.ContactNo = txtContactNo.Text;
newPerson.Email = txtEmail.Text;
newPerson.FatherName = txtFatherName.Text;
newPerson.Id = Convert.ToInt32(hfPersonId.Value);
newPerson.Name = txtName.Text;
PersonDataAccess newDA = new PersonDataAccess();
bool status=newDA.Save(newPerson);
if (SavedPerson != null)
{
PersonEventArgs newArg = new PersonEventArgs(status);
SavedPerson(this, newArg);
}
if (status)
{
ClearForm();
}
}
public void PrepareEditView(int personId)
{
PersonDataAccess newDA = new PersonDataAccess();
Person currentPerson = newDA.GetPersonById(personId);
if (currentPerson != null)
{
txtAge.Text = currentPerson.Age.ToString();
txtContactNo.Text = currentPerson.ContactNo;
txtEmail.Text = currentPerson.Email;
txtFatherName.Text = currentPerson.FatherName;
txtName.Text = currentPerson.Name;
hfPersonId.Value = personId.ToString();
}
}
private void ClearForm()
{
txtAge.Text = string.Empty;
txtContactNo.Text = string.Empty;
txtEmail.Text = string.Empty;
txtFatherName.Text = string.Empty;
txtName.Text = string.Empty;
hfPersonId.Value = "0";
}
}
List ControlPersonList.ascx control has the public partial class PersonList : System.Web.UI.UserControl
{
public delegate void EditPerson(object sender, EditPersonEventArgs arg);
public event EditPerson EditedPerson;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadPersonList();
}
}
public void LoadPersonList()
{
PersonDataAccess newDA = new PersonDataAccess();
grdViewPersonList.DataSource = newDA.GetPersonList();
grdViewPersonList.DataBind();
}
protected void grdViewPersonList_RowEditing(object sender, GridViewEditEventArgs e)
{
if (EditedPerson != null)
{
int personId = Convert.ToInt32
(grdViewPersonList.DataKeys[e.NewEditIndex].Value);
EditPersonEventArgs newArg = new EditPersonEventArgs(personId);
EditedPerson(this, newArg);
}
}
protected void grdViewPersonList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int personId = Convert.ToInt32(grdViewPersonList.DataKeys[e.RowIndex].Value);
PersonDataAccess newDA = new PersonDataAccess();
bool status = newDA.Delete(personId);
if (status)
{
LoadPersonList();
}
}
}
Control ContainerAt last, put the controls on the Default.aspx page and set the event handlers <body>
<form id="form1" runat="server">
<div>
<strong>User Control 1</strong><br />
<div style="border-style:solid; border-width:1px;
border-color:Black; clear:both; width:510px;">
<uc1:PersonEditor runat="server" ID="ucntrlPersonEditor"
OnSavedPerson="ucntrlPersonEditor_SavedPerson" />
</div>
<br />
<strong>User Control 2</strong><br />
<div style="border-style:solid; border-width:1px;
border-color:Black; clear:both; width:510px;">
<uc1:PersonList runat="server"
ID="ucntrlPersonList" OnEditedPerson="ucntrlPersonList_EditedPerson"/>
</div>
</div>
</form>
</body>
And the event handlers code looks like this: public partial class _Default : System.Web.UI.Page
{
protected void ucntrlPersonEditor_SavedPerson(object sender, PersonEventArgs e)
{
if (e.IsSuccess)
{
ucntrlPersonList.LoadPersonList();
}
}
protected void ucntrlPersonList_EditedPerson(object sender, EditPersonEventArgs e)
{
if (e.PersonId>0)
{
ucntrlPersonEditor.PrepareEditView(e.PersonId);
}
}
}
Points of InterestThe functionality includes:
History
|
||||||||||||||||||||||||||||||||||||||||