I need to pass a value from the onload event of a user control to the onload event of the parent page. I have found this code on stackoverflow but I can't seem to get it to work/adapt it:
Public Property SomeValue() As String
Get
Return textbox开发者_高级运维1.Text
End Get
End Property
The value I need to pass is a single int from a database.
conn.Open();
reader = comm.ExecuteReader();
if (reader.Read())
{
shoppingCartHeadID = Convert.ToInt32(reader["shoppingCartHeadID"]);
hfShoppingCartHeadID.Value = shoppingCartHeadID.ToString();
}
reader.Close();
You don't really need to pass anything. Just create a property on your user control and access it from the page:
protected void Page_Load(object sender, EventArgs e)
{
string userControlValue = UserControl.UserControlValue;
}
its not the fine way, but if you realy now what you are doing there, you can try it with an property
mayby something like this:
//child uc class
...
public string MyValue { get; private set; }
//... loaded (...)
{
...
this.MyValue = "Value";
}
// Parent uc class
//... loaded (...)
{
...
string childValue = this.ucChildControl.MyValue;
}
This code:
Public Property SomeValue() As String
Get
Return textbox1.Text
End Get
End Property
Looks like this in C#:
public String SomeValue {
get{ return textbox1.text}
}
I think by the way that the child user control should set a property, what the parent user control should read.
//child
public string DatatoShare{get;set;}
protected override Onload()
{
DatatoShare = "Whatever you want"
base.Onload()
}
//parent
private ChildUserControl child;
public string NeedChildsData()
{
return child.DatatoShare
}
In the end I opted to create a separate public class to call and control the cart. This turned out to be much more efficient and cleaner code!
Thank you for your suggestions.
精彩评论