Hi,
"Why not just do this:" : because my "code" section is "hard" and i dont
want repeat it three time :
public static bool createLink<T>(T myControl, string message, string action,
string code)
{
switch (myControl.GetType().Name)
{
case "LinkButton":
case "Button":
case "ImageButton":
IAction ia = ((IAction)((myControl as
Control).Parent.NamingContainer));
myControl.GetType().GetProperty("OnClientClick").S etValue(myControl,
"if(confirm('"+message+"')){document.getElementByI d('" + ia._ActionID +
"').value='" + action + "';document.getElementById('" + ia._ValueID +
"').value='" + code + "'; }else return false;", null);
return true;
}
return false;
}
generic, reflection, ..., no importance... i want just no repeat my code...
and i wnt take take the best/good way to do it.
"Dale" <da******@nospam.nospama écrit dans le message de news:
9E**********************************@microsoft.com...
Why use a generic at all? Why not just do this:
public static john(object obj, control c)
{
if (c is LinkButton)
{
((LinkButton)c).OnClientClick = "code";
}
else if (c is Button)
{
((Button)c).OnClientClick = "code";
}
else if (c is ImageButton)
{
((ImageButton)c).OnClientClick = "code";
}
else
{
throw new ArgumentException("Only Button, ImageButton, or
LinkButton objects can be passed to the Control parameter of John(object
obj,
Control c).");
}
}
Alternatively, a more fxCop friendly way of doing the if would be:
LinkButton lb;
if ((lb = c as LinkButton) != null)
{
lb.OnClientClick = "code"
}
..... and so forth.
Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Steph" wrote:
>i code that :
public static bool john<T>(T myControl)
{
switch(myControl.GetType().Name)
{
case "Button":
case "ImageButton":
case "LinkButton":
myControl.getType().GetProperty("OnClientClick"). SetValue(myControl,"code",null);
return true;
}
return false;
}
"Marc Gravell" <ma**********@gmail.coma écrit dans le message de news:
11*********************@n67g2000cwd.googlegroups.c om...
Normally, that wouldn't be a generics question, but rather a base-class
/ interface question.
However, in this case the OnClientClick property isn't exposed in a
base-class / interface; one solution here would be reflection;
*however* since there are only 3 classes (Button, ImageButton and
LinkButton) with this property, I would actually recommend the
overloaded form that you have written. It will be a lot faster.
Marc