aspx页面中设置的Web用户控件值

发布于 2024-10-07 00:49:18 字数 552 浏览 0 评论 0原文

这可能吗,我有一些 C# 代码将 Web 用户控件添加到 aspx 页面,请参见下文:

 UserControl myUserControl;
 myUserControl = (UserControl)LoadControl("../TempLayouts/LayoutSize.ascx");
 PlaceHolder1.Controls.Add(myUserControl);

在我的 ascx 上,我有以下代码:

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}

在上面的代码中调用 Web 用户控件属性时,如何将 Edit_Mode 值设置为 1?

这可能不用铸造吗?

Is this 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城花音 2024-10-14 00:49:18

您需要在您的 aspx 文件中添加一个类引用,如下所示:

<%@ Reference Control="../TempLayouts/LayoutSize.ascx" %>

然后,在您的 aspx.cs 文件中添加如下所示的内容:

ASP.LayoutSize_ascx myUserControl;
myUserControl = (ASP.LayoutSize_ascx)LoadControl("../TempLayouts/LayoutSize.ascx");
myUserControl.Edit_Mode = 1;

您需要检查控件的类名。

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.

罗罗贝儿 2024-10-14 00:49:18
 //.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; }
    }
 //.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; }
    }
琉璃梦幻 2024-10-14 00:49:18

将其转换为您的特定控件类型,而不是 (UserControl)。这样,您可以在将用户控件添加到页面之前设置其属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文