English 中文(简体)
网页用户控制值
原标题:web user control value set in aspx page
  • 时间:2010-12-09 16:04:50
  •  标签:
  • c#
  • .net
  • ascx

如果可能的话,我有一些C#代码,把网站用户控制上加到px页,如下:

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

我的轴心是:

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}

在将网络用户控制称作上文代码Attributes时,我怎么能将Edit_Mode的数值定为1?

如果不这样做,这种可能性是可能的?

问题回答

您需要在其 as中增加一栏,例如:

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

之后,在您的 as子档案中添加了这样的内容:

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

你们需要检查你们的控制类别名称。

 //.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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签