使用参数调用 WebuserControl 错误 -

发布于 2024-11-02 22:56:47 字数 2479 浏览 0 评论 0原文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Text;
using System.IO;
using System.Reflection;

namespace e_compro
{
    public partial class fetchrepeater : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string Result(string controlName)
        {
           // return RenderControl(controlName);
            Control toreturn = LoadControl(controlName, "hello");
            return toreturn;
        }

        //public static string RenderControl(string controlName)
        //{
        //    Page page = new Page();
        //    UserControl userControl = (UserControl)page.LoadControl(controlName);
        //    userControl.EnableViewState = false;
        //    HtmlForm form = new HtmlForm();
        //    form.Controls.Add(userControl);
        //    page.Controls.Add(form);

        //    StringWriter textWriter = new StringWriter();
        //    HttpContext.Current.Server.Execute(page, textWriter, false);
        //    return textWriter.ToString();
        //}

        public static UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
        {
            List<Type> constParamTypes = new List<Type>();
            foreach (object constParam in constructorParameters)
            {
                constParamTypes.Add(constParam.GetType());
            }

            UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;

            // Find the relevant constructor
            ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());

            //And then call the relevant constructor
            if (constructor == null)
            {
                throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
            }
            else
            {
                constructor.Invoke(ctl, constructorParameters);
            }

            // Finally return the fully initialized UC
            return ctl;
        }


    }
}

我已将 LoadControl 方法从受保护的静态方法更改为公共静态方法。我收到第一个参数的错误,该参数是 Webuser 控件 .ascx 文件的位置。

错误 76 非静态字段、方法或属性“System.Web.UI.TemplateControl.LoadControl(string)”需要对象引用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Text;
using System.IO;
using System.Reflection;

namespace e_compro
{
    public partial class fetchrepeater : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string Result(string controlName)
        {
           // return RenderControl(controlName);
            Control toreturn = LoadControl(controlName, "hello");
            return toreturn;
        }

        //public static string RenderControl(string controlName)
        //{
        //    Page page = new Page();
        //    UserControl userControl = (UserControl)page.LoadControl(controlName);
        //    userControl.EnableViewState = false;
        //    HtmlForm form = new HtmlForm();
        //    form.Controls.Add(userControl);
        //    page.Controls.Add(form);

        //    StringWriter textWriter = new StringWriter();
        //    HttpContext.Current.Server.Execute(page, textWriter, false);
        //    return textWriter.ToString();
        //}

        public static UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
        {
            List<Type> constParamTypes = new List<Type>();
            foreach (object constParam in constructorParameters)
            {
                constParamTypes.Add(constParam.GetType());
            }

            UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;

            // Find the relevant constructor
            ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());

            //And then call the relevant constructor
            if (constructor == null)
            {
                throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
            }
            else
            {
                constructor.Invoke(ctl, constructorParameters);
            }

            // Finally return the fully initialized UC
            return ctl;
        }


    }
}

I have change from protected to public static method for LoadControl method. Im getting this error for the first parameter which is the location of the Webuser control .ascx file.

Error 76 An object reference is required for the non-static field, method, or property 'System.Web.UI.TemplateControl.LoadControl(string)'

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

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

发布评论

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

评论(1

匿名的好友 2024-11-09 22:56:47

您提供给 LoadControl 的第一个参数是 Type,其值 controlName.GetType().BaseType 等于 System.Object ( controlName是一个stringstring的基类型是object) ->错误类型“System.Object”不继承自“System.Web.UI.Control”,因为LoadControl需要一个System.Web.UI.Control 类型。

您想要实例化一个控件,并给出其名称和名称。一些参数。不幸的是,没有任何版本的 LoadControl 接受这些参数。幸运的是,实现这一目标非常简单。看一下这篇文章:Asp.net 的一个很好的例子具有多个参数的LoadControl

The 1st parameter you're giving to LoadControl is a Type and it's value controlName.GetType().BaseType is equal to System.Object (controlName is a string and basetype of string is object) -> error Type 'System.Object' does not inherit from 'System.Web.UI.Control' because LoadControl expects a System.Web.UI.Control type.

You want to instanciate a control giving it's name & some parameters. Unfortunatly, there is no version of LoadControl that accepts those parameters. Fortunatly, it's quite simple to achieve that. Take a look at this article: A Good Example of Asp.net LoadControl with multiple parameters.

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