Click here to Skip to main content
15,891,863 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,
I have a textbox control which been created dynamically!
I want to make a function to set textbox's (or any control) event and its delegate from a string variables.

C#
void SetEvent(ref Control control, string EventType, string EventDelegate)
{
     //Set control.event += delegate;   event = EventType and delegate = EventDelegate
}


Function call:
C#
SetEvent("TextChanged", "txtBox_TextChanged"); //The function will set the event dynamically according to the parameters (txtBox.TextChanged += new EventHandler(txtBox_TextChanged);)
OR
SetEvent("DataBinding", "txtBox_DataBinding"); //The function will set the event dynamically according to the parameters (txtBox.DataBinding += new EventHandler(txtBox_DataBinding);)


How can I achieve this procedure??????
Posted
Updated 18-Sep-13 22:31pm
v2

Set onchange event on clientside for your TextBox when you create your control dynamically.

You can do that as the follows:
C#
yourTextBox.Attributes.Add("onchange", "alert(this.value);");


Maybe this can help you:
Parsing C# code (as string) and inserting additional methods[^]

Execute a user-provided string containing c# code[^]
 
Share this answer
 
v3
Comments
Rasool Ahmed 19-Sep-13 4:27am    
That's not the solution I want.
I will update my question to understand it.
norbitrial 19-Sep-13 4:34am    
I have updated my solution.
Rasool Ahmed 19-Sep-13 7:05am    
norbitrial, these links tell that string containing code executed at runtime independent (outside the original code).
My code string must executed withing the original code.
void SetEvent(object control, string eventTypes, string EventDelegate)
{
EventInfo ei = control.GetType().GetEvent(eventTypes);

MethodInfo mi = this.GetType().GetMethod(EventDelegate, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, this, mi);

ei.AddEventHandler(control, del);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900