I am trying to generate a method on a custom control that will take an object I have created with metadata pertaining to the control and have the control created in this one method and added to the page control collection. Below is the object that I created to contain the control metadata.
public class ListControlSetup
{
public ListControl ListControl { get; set; }
public String ControlId { get; set; }
public String ControlCssClass { get; set; }
public bool CausesValidation { get; set; }
public IuIDropDownDictionary DropDownDictionary { get; set; }
public String DropDownMethod { get; set; }
public bool AutoPostback { get; set; }
}
I then have a method that takes the above object as a parameter and creates the control. Lets say a dropdownlist in this instance. Lets say I wanted to pass an Event for the dropdownlist OnSelectedIndexchange event. How would I do this in a generic fashion so that I could have the event wired up in the called procedure. Below is the called procedure I currently have.
private void ControlSetUp(ListControlSetup control)
{
control.ListControl = this.CreateDropDownListControl(开发者_C百科control.ControlId, control.ControlCssClass, control.CausesValidation);
_myManager.DropDownMethod = control.DropDownMethod;
_myManager.FillDropDown(control.DropDownDictionary, control.ListControl);
this.Controls.Add(control.ListControl);
}
below is the call and object instantiation.
ListControlSetup lcs = new ListControlSetup{ ListControl = ddlControl, DropDownMethod = SPConsts.GetData,
DropDownDictionary = new DataDictionary(), ControlId="ddlControlId", ControlCssClass="inputfield", CausesValidation=false, AutoPostback=false};
ControlSetUp(lcs);
Any advice would be great as to how I would add events to the ControlSetUp method.
Thank You
The solution to your problem is with delegates. The DropDownList.OnSelectedIndexChanged
event is declared using a delegate of type EventHandler
(which itself is declared in System.dll). So add a method of the same EventHandler
type to your ListControlSetup
class, like this:
public class ListControlSetup
{
...
EventHandler OnSelectedIndexChanged;
// Don't let the stackoverflow coloring fool you,
// 'OnSelectedIndexChanged' is a member variable name.
}
Then, in your ControlSetUp()
method, do this:
if ( control.OnSelectedIndexChanged != null )
ddlControl.SelectedIndexChanged += control.OnSelectedIndexChanged;
Finally, to assign the ListControlSetup's OnSelectedIndexChanged list member, do this:
ListControlSetup lcs = new ListControlSetup() { ... };
lcs.OnSelectedIndexChanged += new EventHandler( ddl_SelectedIndexChanged );
That assumes your event handler method is ddl_SelectedIndexChanged, of course.
Hope this helps!
精彩评论