web user control value set in aspx page
Is 开发者_如何学Cthis possible, I have some C# code that adds web user control onto aspx page see below:
UserControl myUserControl;
myUserControl = (UserControl)LoadControl("../TempLayouts/LayoutSize.ascx");
PlaceHolder1.Controls.Add(myUserControl);
On my ascx i have the following code:
private int Edit_Mode = 0;
public int Get_EditMode
{
get { return Edit_Mode; }
set { Edit_Mode = value; }
}
protected void Page_Load(object sender, EventArgs e)
{ if(Edit_Mode == 1)//do something}
How can I set Edit_Mode value to 1 when calling the web user control in code above, Attributes?
This this possible without casting ?
You need add a class reference in your aspx file, something like this:
<%@ Reference Control="../TempLayouts/LayoutSize.ascx" %>
Then, in your aspx.cs file add something like this:
ASP.LayoutSize_ascx myUserControl;
myUserControl = (ASP.LayoutSize_ascx)LoadControl("../TempLayouts/LayoutSize.ascx");
myUserControl.Edit_Mode = 1;
You need check the class name of your control.
//.aspx
Control c = Page.LoadControl("LayoutSize.ascx");
c.GetType().GetProperty("Get_Editor_Mode").SetValue(c, True, null);
//.ascx
private bool Editor_Mode = false;
public bool Get_Editor_Mode
{
get { return Editor_Mode; }
set { Editor_Mode = value; }
}
Cast it to your specific control type instead of (UserControl)
. That way you can set the properties of your user control before you add it to the page.
精彩评论