如何设置用户控件的DataSource属性in.aspx文件? [英] How to set DataSource property in.aspx file of a user control?

查看:133
本文介绍了如何设置用户控件的DataSource属性in.aspx文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件(不希望创建自定义的控制)在此控制我有一个列表框。 现在我想它可以用作数据源此列表框的用户数据源属性。

I have a user control (don't want to create custom control) in this control i have a listbox. Now i want to user Datasource property which can be use as a datasource for this listbox.

<UC:CustomList ID="list" CustomDataSource="objectDataSource1" runat="server" />

在控制:

 public object CustomDataSource
    {
        get
        {
            return this.checkComboBox.DataSourceID;
        }

        set
        {
            this.checkComboBox.DataSource = value;
        }            
    }

这给运行时错误无法从它的字符串重新presentation'objectDataSource1'为'CustomDataSource'属性创建类型的对象'System.Object的'。

It give runtime error "Cannot create an object of type 'System.Object' from its string representation 'objectDataSource1' for the 'CustomDataSource' property."

推荐答案

在重新examening你的问题,我想我明白你的要求。

After re-examening your question I think I understand what you ask for.

由于您使用的是ObjectDataSource控件的ID,你需要找到控制并得到SelectMethod属性。然后,你需要调用该方法得到的数据是presented。这是你真的不想这样做,因为它导致了很多问题,如不能够在CustomDataSource财产在code前设置因为不填充在该阶段Page.Controls东西,所以你不能发现ObjectDataSource控件。您可以在Page_Load中却设置在code中的属性后面。

Since you are using the ID of the ObjectDataSource you need to find that control and get the SelectMethod property. Then you need to invoke that method to get the data to be presented. This is something you really don't want to do because it leads to a lot more problems, like not being able to set the CustomDataSource property in the code front since Page.Controls isn't populated at that stage so you cannot find the ObjectDataSource control. You can however set the property in code behind in Page_Load.

下面是你如何能做到这一点的例子:

Here is an example of how you could do it:

code为用户控件的背后:

Code Behind for the Usercontrol:

public partial class MyUserControl : System.Web.UI.UserControl
{
    private string customDataSource;
    public string CustomDataSource
    {
        get { return customDataSource; }
        set
        {
            customDataSource = value;
            var ctrl = (ObjectDataSource) this.Page.FindControlRecursive(customDataSource);
            if (ctrl == null) return;

            var m = this.Page.GetType().GetMethod(ctrl.SelectMethod);
            if (m == null) return;

            var data = m.Invoke(this.Page, BindingFlags.InvokeMethod | BindingFlags.Public, null, null, null);

            checkComboBox.DataSource = data;
            checkComboBox.DataBind();
        }
    }
}

$ C $下使用的用户控件的页面背后:

Code Behind for the page using the Usercontrol:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            myCtrl.CustomDataSource = "objectDataSource1";
    }

    public object GetData()
    {
        return new List<string> { "1", "2", "3" };
    }
}

在某处你喜欢的东西插入你的用户控件的网页上:

Somewhere on the page you are inserting your Usercontrol with something like:

<uc:MyUserControl runat="server" id="myCtrl"></uc:MyUserControl>

和ObjectDataSource控件的过程:

And of course the ObjectDataSource:

<asp:ObjectDataSource runat="server" ID="objectDataSource1" SelectMethod="GetData"></asp:ObjectDataSource>

FindControlRecursive是一个简单的扩展方法:

FindControlRecursive is a simple Extension Method:

public static class ExtensionMethods
{
    public static Control FindControlRecursive(this Control control, string id)
    {
        if (control == null) return null;
        if (control.ID == id) return control;

        foreach (Control c in control.Controls)
        {
            var found = FindControlRecursive(c, id);
            if (found != null) return found;
        }

        return null;
    }
}

这篇关于如何设置用户控件的DataSource属性in.aspx文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆