I’m writing item template for repeater in separate control, and then I’m using following code:
HospitalRepeater.DataSource = LocationsList;
HospitalRepeater.ItemTemplate = Page.LoadTemplate("~/UserControls/HospitalDetails.ascx");
HospitalRepeater.DataBind();
This code worked fine, however now we want to add custom events to HospitalDetails control.
We created following event with custom event args:
public class HospitalItemEventArgs : EventArgs
{
public Int32 HospitalID { get; set; }
public HospitalItemEventArgs() { }
public HospitalItemEventArgs(Int32 hID)
{
this.HospitalID = hID;
}
}
public event EventHandler<HospitalItemEventArgs> HospitalAction;
protected virtual void OnHospitalAction(HospitalItemEventArgs e)
{
if (HospitalAction!= null)
this.HospitalAction(this, e);
}
Now here is a problem – I can’t access this custom event from my code after loading this control as template, because it returns object of System.Web.UI.ITemplate.
I assumed that this is wrapper above my exact control, but this assumtion is wrong.
Cast to my control type fails with following error message:
Unable to cast object of type 'SimpleTemplate' to type 'UserControls.HospitalDetails'.
I’ve tried reverse action – load control using
Page.LoadControl("~/UserControls/HospitalDetails.ascx");
It returns object of correct HospitalDetails type, but It does not implement ITemplate interface.
Whan I tried to do that I’ve received error message:
Unable to cast object of type 'ASP.usercontrols_hospitaldetails_ascx' to type 'System.Web.UI.ITemplate'.
Can anyone help me to deal with this cast, or find another solution which matches following requrements:
- Repeater should be bound to list of HospitalItems to display details.
- Template for displaying details should hide it’s UI interactions and expose few simple events like HospitalAction with hospital ID.
- Separate object must have ability to subscribe to this events.
Your best bet is probably to create some
<asp:Button />controls specifying theCommandNameandCommandArgumentattributes. You can then handle the bubbled even on the repeater itself, interrogate the arguments forCommandNameandCommandArgument.I’m not using a separate control as a template but the same thing can be accomplished like this.
Update
The purpose for the button’s
CommandArgumentandCommandNameattributes is to allow you to provide the user with individual actions to take on databound ui content. TheCommandArgumentattribute is to allow you a point of entry back into the data to retrieve the relevant information for the event. I have never tried to put multiple values in this attribute, but I do not see a reason why it would not work.Another option is to create a separate list that contains HospitalID and DoctorID associations, create a unique ID for each association and store that list in a database (if you need to persist it), cache (if used by multiple users), session (if used by multiple pages by same user) or Viewstate (if used by a single page and the list is relatively short).