简体   繁体   English

自定义用户控制数据源

[英]Custom user control data source

I have a custom control that has a DropDownList inside and it's created by CreateUserControl. 我有一个自定义控件,里面有一个DropDownList,它是由CreateUserControl创建的。 I'm saving data source directly to dropdown control. 我将数据源直接保存到下拉控件。 After page postbacked my binded data disappear. 页面回发后,我的绑定数据消失了。 Should i save/restore my binded data myself in some tricky way? 我是否应该以某种棘手的方式保存/恢复绑定数据?

public class EnumDropDownList : UserControl
{
    private readonly DropDownList _ddlSelector;
    private Dictionary<long, string> _dataSource;

    public EnumDropDownList()
    {
        _ddlSelector = new DropDownList();
        _dataSource = new Dictionary<long, string>();
    }


    public object DataSource
    {
        set
        {
            // datasource logic
        }
    }

    public long SelectedValue
    {
        get { return Convert.ToInt64(_ddlSelector.SelectedValue); }
        set { _ddlSelector.SelectedValue = value.ToString(); }
    }

    public override void DataBind()
    {
        _ddlSelector.DataSource = _dataSource;
        _ddlSelector.DataTextField = "Value";
        _ddlSelector.DataValueField = "Key";
        _ddlSelector.DataBind();

        base.DataBind();
    }

    [PermissionSet(SecurityAction.Demand, Name = "Execution")]
    protected override void CreateChildControls()
    {
        Controls.Add(_ddlSelector);
    }
}

In some cases I have saved the dataset in a session variable. 在某些情况下,我已将数据集保存在会话变量中。 Whicn can then be referenced after postbacks. 然后可以在回发之后引用Whicn。 Something like this: 像这样:

Session.Add("dataSource", _dataSource);//create the session variable

Then you can reference it, depending on the type the data source is (in example I used a datable) 然后,您可以引用它,具体取决于数据源的类型(例如,我使用了datable)

_ddlSelector.DataSource = (DataTable)Session["dataSource"];

You code is combination of UserControl and CustomServerControl . 您的代码是UserControlCustomServerControl组合。

It could have be a lot easier if you implement one instead of combination. 如果实施一个而不是组合,则可能会容易得多。

I created two controls - UserControl and CustomServerControl . 我创建了两个控件UserControlCustomServerControl

UserControl 用户控件

Place the dropdownlist to ASPX instead of loading dymaiclally. 将下拉列表放置到ASPX,而不是动态加载。 If you load dynamically, you'll have to take care of persistence of control. 如果动态加载,则必须注意控制的持久性。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EnumDropDownList.ascx.cs"
    Inherits="WebApplication2010.EnumDropDownList" %>
<asp:DropDownList runat="server" ID="DropDownList1" />

public partial class EnumDropDownList : System.Web.UI.UserControl
{
    private Dictionary<long, string> _dataSource;

    public EnumDropDownList()
    {
        _dataSource = new Dictionary<long, string>();
    }

    public Dictionary<long, string> DataSource
    {
        set { _dataSource = value; }
    }

    public long SelectedValue
    {
        get { return Convert.ToInt64(DropDownList1.SelectedValue); }
        set { DropDownList1.SelectedValue = value.ToString(); }
    }

    public override void DataBind()
    {
        DropDownList1.DataSource = _dataSource;
        DropDownList1.DataTextField = "Value";
        DropDownList1.DataValueField = "Key";
        DropDownList1.DataBind();

        base.DataBind();
    }
}

Custom Server Control (it is a lot easier to implement for your case) 自定义服务器控件(根据您的情况更容易实现)

It basically inherits DropDownList web control. 它基本上继承了DropDownList Web控件。

public class MyDropDownList : DropDownList
{
    public long SelectedInt64Value
    {
        get { return Convert.ToInt64(SelectedValue); }
        set { SelectedValue = value.ToString(); }
    }

    public Dictionary<long, string> DataSource
    {
        get { return (Dictionary<long, string>)ViewState["DataSource"]; }
        set { ViewState["DataSource"] = value; }
    }

    public override void DataBind()
    {
        foreach (var item in DataSource)
            Items.Add(new ListItem(item.Value, item.Key.ToString()));

        base.DataBind();
    }
}

Usage 用法

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="WebApplication2010.WebForm5" %>

<%@ Register Src="EnumDropDownList.ascx" TagName="EnumDropDownList" TagPrefix="uc1" %>
<%@ Register TagPrefix="asp" Namespace="WebApplication2010" Assembly="WebApplication2010" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <uc1:EnumDropDownList ID="EnumDropDownList1" runat="server" />
    <asp:Button runat="server" ID="Button1" Text="Submit" OnClick="Button1_Click" />
    <asp:MyDropDownList id="MyDropDownList1" runat="server" />
    </form>
</body>
</html>

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        Dictionary<long, string> dataSource  = new Dictionary<long, string>();
        dataSource.Add(1, "One");
        dataSource.Add(2, "Two");

        EnumDropDownList1.DataSource = dataSource;
        EnumDropDownList1.DataBind();

        MyDropDownList1.DataSource = dataSource;
        MyDropDownList1.DataBind();
    }
}

THis Is aspx file : 这是aspx文件:

                <telerik:RadComboBox ID="cmbCurrency" runat="server" Width="200px" MaxHeight="200px"
                    EmptyMessage="Select a currency" AutoPostBack="true" Filter="Contains" EnableLoadOnDemand="true">
                </telerik:RadComboBox>

This is code Behind : 这是背后的代码:

 if (!IsPostBack)
            {
                popCurrencyName();              
            }    

    public void popCurrencyName()
                {
                    DataTable dtCurrency = objCurrencyBAL.getCurrency(long.MinValue);

                    if (dtCurrency.Rows.Count > 0)
                    {
                        cmbCurrency.DataSource = dtCurrency;
                        cmbCurrency.DataTextField = "Name";
                        cmbCurrency.DataValueField = "CurrencyId";
                        cmbCurrency.DataBind();
                    }
                }

Try this code: 试试这个代码:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM