【问题标题】:Send parameters into dynamically loaded user controls将参数发送到动态加载的用户控件
【发布时间】:2010-09-19 17:43:13
【问题描述】:

我使用 jQuery 将一些用户控件的内容加载到我的页面中。所以我有这个从我的用户控件中提取内容的功能,它就像一个魅力。

    public string GetObjectHtml(string pathToControl)
    {
        // Create instance of the page control
        Page page = new Page();

        // Create instance of the user control
        UserControl userControl = (UserControl)page.LoadControl(pathToControl);

        //Disabled ViewState- If required
        userControl.EnableViewState = false;

        //Form control is mandatory on page control to process User Controls
        HtmlForm form = new HtmlForm();

        //Add user control to the form
        form.Controls.Add(userControl);

        //Add form to the page
        page.Controls.Add(form);

        //Write the control Html to text writer
        StringWriter textWriter = new StringWriter();

        //execute page on server
        HttpContext.Current.Server.Execute(page, textWriter, false);

        // Clean up code and return html
        string html = CleanHtml(textWriter.ToString());

        return html;
    }

但我真的很想在创建用户控件时将一些参数发送到我的用户控件中。这可能吗,我该怎么做?

我可以看到LoadControl() 可以使用object[] parameters 获取一些参数,但我真的不知道如何使用它,非常感谢!

【问题讨论】:

    标签: c# asp.net user-controls dynamic-usercontrols


    【解决方案1】:

    您可以在您的用户控件上为适当的参数实现一个接口。

    public interface ICustomParams
    {
        string UserName { get; set; }
        DateTime SelectedDate { get; set; }
    }
    

    在用户控件中实现接口,像这样

    public partial class WebUserControl : System.Web.UI.UserControl , ICustomParams
    {
        public string UserName { get; set; }
        public DateTime SelectedDate  { get; set; }
    }
    

    然后加载你的控件:

      UserControl userControl = (UserControl)page.LoadControl(pathToControl);
    

    通过接口访问控件

      ICustomParams ucontrol = userControl as ICustomParams;
      if(ucontrol!=null)
      {
           ucontrol.UserName = "henry";
           ucontrol.SelectedDate = DateTime.Now;
      }
    

    完成,

    您可以在那里添加多个接口用于多种用途。 如果用户控件没有实现接口,if语句将避免使用它

    但是,如果您确实无法访问用户控件,并且您知道“一点”要设置的属性以及它们的类型,请尝试使用反射更动态的方式:

    加载用户控件:

        UserControl userControl = (UserControl)Page.LoadControl(@"~/WebUserControl.ascx");
    

    获取加载的用户控件的属性:

        PropertyInfo[] info = userControl.GetType().GetProperties();
    

    循环遍历它:

        foreach (PropertyInfo item in info)
        {
            if (item.CanWrite)
            {
                 switch (item.Name)
                 {
                     case "ClientName"
                         // A property exists inside the control": //
                         item.SetValue(userControl, "john", null); 
                         // john is the new value here
                     break;
                 }
            }
        }
    

    我只会鼓励你这样做,如果你不能访问用户控件,并且每个用户控件有几十个具有大量可变属性的用户控件。 (它会变得非常丑陋、缓慢且不安全)

    【讨论】:

    • 完美!正是我需要的东西,它有效,非常感谢!
    • 添加了一些额外的东西,但现在看起来已经过时了;)请不要使用反射的东西......
    【解决方案2】:

    我不确定一般情况下如何完成,但看起来您无论如何都在加载自己的用户控件。例如,尝试将 UserControl 转换为您正在使用的控件的类型。

     // Create instance of the user control 
        MyUserControl userControl = (MyUserControl)page.LoadControl("/MyUserControl.ascx"); 
    
      userControl.Param1="my param";
    

    【讨论】:

    • 谢谢,但我的问题是我使用一个函数来获取多个用户控件的内容。所以我并不总是知道用户控件的名称,只知道它的路径。我已经更新了我的问题以反映这一点。
    猜你喜欢
    • 2011-12-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多