动态创建级联RadCombo用户控件

时间:2012-03-21 10:48:32

标签: c# user-controls dynamic-controls radcombobox

我有一个用户控件,它是2个RadCombos级联。我正试图在运行时将这些添加到我的页面。问题是用户控件的Page_Load JS函数永远不会触发。此外,每当aspx.cs的Page_Load触发IsPostback时总是如此。有人可以帮忙吗?

背后的控制代码:

public partial class ctrlCascadedProcedureDropDown : System.Web.UI.UserControl
{
    public int ProcedureTypeSelectedValue { get; set; }
    public int ProcedureSelectedValue { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadProcedureTypes();

            if (ProcedureTypeSelectedValue > 0)
                LoadProcedures(ProcedureTypeSelectedValue.ToString());

            rcbProcedureType.SelectedValue = ProcedureTypeSelectedValue.ToString();
            rcbProcedure.SelectedValue = ProcedureSelectedValue.ToString();
        }
        else if (!Page.IsCallback)
        {
            if (rcbProcedureType != null)
                LoadProcedures(rcbProcedureType.SelectedValue);
        }
    }

    protected void rcbProcedureType_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        LoadProcedureTypes();
    }

    protected void rcbProcedure_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        LoadProcedures(e.Text);
    }

    protected void LoadProcedureTypes()
    {
        TARNDB.OpsProcedureType opsProcedureType = new TARNDB.OpsProcedureType();
        rcbProcedureType.DataSource = opsProcedureType.GetProcedureTypes().OrderBy(x => x.ProcedureType);;
        rcbProcedureType.DataValueField = "ID";
        rcbProcedureType.DataTextField = "ProcedureType";
        rcbProcedureType.DataBind();

        rcbProcedureType.Items.Insert(0, new RadComboBoxItem(""));  
    }

    protected void LoadProcedures(string proceduretypeid)
    {
        int _proceduretypeid;
        if (int.TryParse(proceduretypeid, out _proceduretypeid))
        {
            TARNDB.OpsProcedure opsProcedure = new TARNDB.OpsProcedure();
            rcbProcedure.DataSource = opsProcedure.GetProcedures(_proceduretypeid).OrderBy(x => x.ProcedureName); ;
            rcbProcedure.DataValueField = "ID";
            rcbProcedure.DataTextField = "ProcedureName";
            rcbProcedure.DataBind();
        }
    }
}

控制加价:

<div>
<telerik:RadComboBox ID="rcbProcedureType" runat="server" 
    OnClientSelectedIndexChanging="LoadProcedures" 
    OnItemsRequested="rcbProcedureType_ItemsRequested" />
<telerik:RadComboBox ID="rcbProcedure" runat="server" Width="250px" 
    OnClientItemsRequested="ItemsLoaded"
    OnItemsRequested="rcbProcedure_ItemsRequested" />
</div>

<script type="text/javascript">

var rcbProcedure;
var rcbProcedureType;

function pageLoad() {
    rcbProcedure = $find("<%= rcbProcedure.ClientID %>");
    rcbProcedureType = $find("<%= rcbProcedureType.ClientID %>");

    alert("in");
}

function LoadProcedures(sender, eventArgs) {
    var item = eventArgs.get_item();
    rcbProcedure.set_text("Loading...");

    // if a procedure type is selected
    if (item.get_index() > 0) {
        // this will fire the ItemsRequested event of the 
        // procedures combobox passing the procedureTypeID as a parameter 
        rcbProcedure.requestItems(item.get_value(), false);
    }
    else {
        // the -Select a continent- item was chosen
        rcbProcedure.set_text(" ");
        rcbProcedure.clearItems();
    }
}

function ItemsLoaded(sender, eventArgs) {
    if (sender.get_items().get_count() > 0) {            
        // pre-select the first item
        sender.set_text(sender.get_items().getItem(0).get_text());
        sender.get_items().getItem(0).highlight();
    }
    //sender.showDropDown();
}

</script>

致电代码:

public partial class Operations : BasePage
{
    public List<DynamicControl> DynamicControls
    {
        get
        {
            return (List<DynamicControl>)Session["_DynamicControls"];
        }
        set
        {
            Session["_DynamicControls"] = value;
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DynamicControls = null;
        }
        else
        {
            if (DynamicControls != null && DynamicControls.Count > 0)
            {
                foreach (DynamicControl control in DynamicControls)
                {
                    ctrlCascadedProcedureDropDown cpd = new ctrlCascadedProcedureDropDown();
                    cpd.ID = control.ID;

                    switch (control.PlaceHolder.ID)
                    {
                        case "phProcedure":
                            phProcedure.Controls.Add(cpd);
                            phProcedure.Controls.Add(new Literal() { Text = "<BR />" });
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }

    protected void btnAddProcedure_Click(object sender, EventArgs e)
    {
        AddDynamicProcedureControl("Procedure", phProcedure);
    }

    private void AddDynamicProcedureControl(string name, PlaceHolder placeHolder, int procedureTypeSelectedValue = 0, 
        int procedureSelectedValue = 0)
    {
        Control control = LoadControl("~/Controls/ctrlCascadedProcedureDropDown.ascx");
        ctrlCascadedProcedureDropDown cpd = (ctrlCascadedProcedureDropDown)control;

        int controlCount = (DynamicControls == null ? 0 : DynamicControls.Count);
        string ID = string.Concat(name, controlCount.ToString());
        cpd.ID = ID;
        cpd.ProcedureTypeSelectedValue = procedureTypeSelectedValue;
        cpd.ProcedureSelectedValue = procedureSelectedValue;

        DynamicControl dc = new DynamicControl() { ID = ID, PlaceHolder = placeHolder };
        if (DynamicControls == null)
            DynamicControls = new List<DynamicControl>();
        DynamicControls.Add(dc);

        placeHolder.Controls.Add(cpd);
        placeHolder.Controls.Add(new Literal() { Text = "<BR />" });
    }

    private List<int> GetDynamicList(PlaceHolder placeHolder)
    {
        List<int> list = null;
        list = new List<int>();
        foreach (Control control in placeHolder.Controls)
        {
            if (control is GenericDropDown)
            {
                int id;
                if (int.TryParse(((GenericDropDown)control).SelectedValue, out id) && id > 0)
                    list.Add(id);
            }
        }

        return list;
    }
}

非常感谢, 保罗

1 个答案:

答案 0 :(得分:0)

我没有在PageInIt中使用LoadControl。