Pass a Single Int from One User Control to the Other Using C#
I have two user controls inside an ASPX page. All I want to do is pass one single int from one control to the other during page load.
I have tried several of the examples on here but none of them work - sorry I mean I can't get them to work! Here's what I've got:
Default.ASPX
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ucBottom.varBottomID = ucTop.varBottomID;
}
}
UCTOP
public partial class ucTop : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
int varBottomID = 100;
}
}
UCBOTTOM
public partial class ucBottom : System.Web.UI.UserControl
{
public int varBottomID { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
BottomText.Text = Convert.ToUInt32(varBottomID).ToString();
}
}
There are no errors except for BottomText.Text returning 0 instead of 100. Also int varBottomID = 100; is stating that it is assigned but never used (ucTop).
From one of the answers here i开发者_开发知识库t's good practice for one UC not to be aware of the other and to use the parent to pass data. But alas not for me.
As always, any help would be appreciated (but in a below dummies guide format!).
EDIT - Sorry A typo crept in the code in Default...
In your main page, it's not really changing anything -- it's setting ucBottom.varBottomID
to itself. (Edit: appears to have been fixed)
In ucTop
, int varBottomID = 100;
does nothing -- it sets a variable inside the function, which won't be visible outside.
The result of those two issues is that you never end up having anything communicated from ucTop
to ucBottom
.
(BTW, convention is to start class names with an uppercase letter, and instance names with a lowercase letter. It's not exactly obvious from looking at the code whether you're attempting to access static or instance members of your classes. From here on i'm assuming you have a ucBottom
control, which is an instance of the ucBottom
class, and the same for ucTop
. It's the only case i see where your code, as pasted, would compile without errors.)
You may need something similar to:
public partial class _Default : Page
{
protected void Page_Load(sender obj, EventArgs e)
{
ucBottom.varBottomID = ucTop.varBottomID; // fix the first issue
}
}
public partial class ucTop : UserControl
{
public int varBottomID { get; set; } // fix the second issue
protected void Page_Load(object src, EventArgs e)
{
varBottomID = 100;
}
}
You will also need to pick different events to trigger on, if you go this route. If everything triggers on Load, you have a problem -- Load fires on the page first, then on the child controls. So the property won't get set correctly. (The setting goes in 3 steps: ucTop
deciding the correct value to set, _Default
setting it from top to bottom, and ucBottom
using it. You need that sequence to go decide, set, use in order for it to work correctly, but if everything triggers on Load you'll end up with set, decide, use or set, use, decide.) You might consider letting _Default
do its thing on LoadComplete, and ucBottom
on PreRender.
Modify your code as needed.
public partial class ucTop : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
ucBottom.varBottomID = 100;
}
}
public partial class ucBottom : System.Web.UI.UserControl
{
public int varBottomID { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
BottomText.Text = Convert.ToUInt32(varBottomID).ToString();
}
}
Defining a class which both UserControl
s could access a static
variable would work:
public static class RequiredVars
{
public static int BottomID { get; set;}
}
精彩评论