开发者

Calling WebuserControl with parameters error -

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)
            {
                constParamTy开发者_如何学运维pes.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)'


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜