Showing posts with label public. Show all posts
Showing posts with label public. Show all posts

Thursday, March 29, 2012

Passing values between two usercontrols?

Hi

How do I pass values between two UserControls?

Can I just register the one usercontrol inside the other usercontrol and then use a public property?

ThanksThis is a very common question, but there is a good and a bad way to answer it.
The short, bad way is to expose a public property, then have one of the controls find the other using FindControl and then set the property after having cast to the right type.
The problem with this is that it introduces a strong coupling between your two controls. What if one of the controls is not on the page? What if it's been replaced with another control with the same ID? What if one control evolves and not the other?

Which leads to the good way to do it: not to do it. The page or a higher container control should act as a global controller here and manage the relationships between the controls it contains. The controls themselves should be self-contained and not depend on any external control to function. The page should handle the events from the controls and manage the communication between controls from there.

Does this make sense?
Hi

Making sense.

Thanks

Saturday, March 24, 2012

passing variable value to method in .cs

I would like to set
errormessage and retrieve it in my method QWE()
It doesnt work. What am I doing wrong?

public class AppLogic
{
string errormessage ="We are sorry, DB Error try again later";

public static void QWE()
{
HttpContext.Current.Response.Write("<script type=text/javascript language=javascript>alert('" + errormessage +"');</script>");
}
}

Mate, How can we help you if you dont tell us what error message you get?

Try this though...

public string errormessage = errormessage ="We are sorry, DB Error try again later";


Embarrassed

CS0120: An object reference is required for the nonstatic field, method, or property 'AppLogic.errormessage'

public class AppLogic
{
publicstaticstring errormessage ="We are sorry, DB Error try again later";

public static void QWE()
{
HttpContext.Current.Response.Write("<script type=text/javascript language=javascript>alert('" +
AppLogic.errormessage +"');</script>");
}
}

static method can only manipulate static field.


lovely it works :)
thank you

Best to use getters and setters when passing information in vairable about your system.

//Delcare the variable as private.

#region Declerations

private

staticstring fromAddress =string.Empty;

#endregion

//Delcare the getters and setters as public

#region Properties

publicstaticstring FromAddress {get {return fromAddress; }set { fromAddress =value; } }

#endregion

passing variable value to method in .cs

I would like to set
errormessage and retrieve it in my method QWE()
It doesnt work. What am I doing wrong?

public class AppLogic{string errormessage ="We are sorry, DB Error try again later";public static void QWE() { HttpContext.Current.Response.Write("<script type=text/javascript language=javascript>alert('" + errormessage +"');</script>"); }}

Can you give an example of the return? What is the result/output of running this?


I would like to set
errormessage

string errormessage ="We are sorry, DB Error try again later";
and retrieve it in my method QWE()

('" + errormessage +"')
It doesnt work. What am I doing wrong?

public class AppLogic
{
string errormessage ="We are sorry, DB Error try again later";

public static void QWE()
{
HttpContext.Current.Response.Write("<script type=text/javascript language=javascript>alert('" + errormessage +"');</script>");
}