繁体   English   中英

Dropdownlist在UpdatePanel中未更新

[英]Dropdownlist not updating in UpdatePanel

我创建了一个简单的页面,其中在UpdatePanel中有两个下拉列表。 当第一个Dropdownlist更改选择时,应该更新第二个dropdownlist的列表,但是它不起作用,并且我不确定自己在做什么错。

我收到以下错误:在UpdatePanel“ HeadUpdatePanel”中找不到触发器的ID为“ ctl00 $ MainContent $ FunctionCombo”的控件。

即使如此,当我单步执行时也会找到它并且它不为null。 看到这一行:

DropDownList dd = (DropDownList)cph.FindControl("FunktionCombo");

但是,当它尝试应用触发器时,就会出现以下错误:

trigger.ControlID = cph.FindControl("FunktionCombo").UniqueID;

仅供参考:母版页没有任何内容,仅调用TestPage!

这是我的TestPage代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="Test.TestPage" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager" runat="server" />

    <div class="container">
        <div>
            <asp:UpdatePanel ID="HeadUpdatePanel" UpdateMode="Always" runat="server">
                <ContentTemplate>
                    <asp:Label ID="Label1" runat="server" Text="Wähle Funktion:" />&nbsp;
                    <asp:DropDownList ID="FunktionCombo" OnSelectedIndexChanged="FunktionCombo_SelectionChanged" AutoPostBack="true" runat="server" />&nbsp;&nbsp;&nbsp;
                    <asp:Label ID="Label2" runat="server" Text="Wähle Tabelle:" />&nbsp;
                    <asp:DropDownList ID="TabelleCombo" OnSelectedIndexChanged="TabelleCombo_SelectionChanged" AutoPostBack="true" runat="server" />&nbsp;&nbsp;&nbsp;
                    <asp:Label ID="CountLbl" runat="server" Text="Wähle Funktion:" />
                    <asp:Button ID="StartBn" runat="server" Text="Start" onclick="StartBtn_Click" />
                </ContentTemplate>               
            </asp:UpdatePanel>            
        </div>        
    </div>
</asp:Content>


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test
{
    public partial class TestPage : System.Web.UI.Page
    {

        private List<ViewInfo> _ospList;
        private List<ViewInfo> _aeList;
        private List<ViewInfo> _icList;

        private bool _functionChanged = false;
        private string _currentFunctionDisplay = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HttpContext.Current.Session["IsAdmin"] = true;
                HttpContext.Current.Session["IsSubAdmin"] = true;

                CreateComboLists();

                ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("MainContent");
                UpdatePanelControlTrigger trigger = new PostBackTrigger();
                DropDownList dd = (DropDownList)cph.FindControl("FunktionCombo");
                trigger.ControlID = cph.FindControl("FunktionCombo").UniqueID;

                HeadUpdatePanel.Triggers.Add(trigger);
            }
        }

        private void CreateComboLists()
        {
            if ((bool)HttpContext.Current.Session["IsAdmin"] == true)
            {
                FunktionCombo.Items.Add("OSP");
                FunktionCombo.Items.Add("AE");
                FunktionCombo.Items.Add("IC");
                FunktionCombo.DataBind();
                FunktionCombo.SelectedIndex = 0;

                _ospList = GetSprocList("OSP", (HttpContext.Current.Session["IsAdmin"] != null) ? (bool)HttpContext.Current.Session["IsAdmin"] : false, (HttpContext.Current.Session["IsSubAdmin"] != null) ? (bool)HttpContext.Current.Session["IsSubAdmin"] : false);
                _aeList = GetSprocList("AE", (HttpContext.Current.Session["IsAdmin"] != null) ? (bool)HttpContext.Current.Session["IsAdmin"] : false, (HttpContext.Current.Session["IsSubAdmin"] != null) ? (bool)HttpContext.Current.Session["IsSubAdmin"] : false);
                _icList = GetSprocList("IC", (HttpContext.Current.Session["IsAdmin"] != null) ? (bool)HttpContext.Current.Session["IsAdmin"] : false, (HttpContext.Current.Session["IsSubAdmin"] != null) ? (bool)HttpContext.Current.Session["IsSubAdmin"] : false);

                TabelleCombo.DataSource = _ospList;
                TabelleCombo.DataTextField = "ViewName";
                TabelleCombo.DataValueField = "SprocName";
                TabelleCombo.SelectedIndex = 0;
                TabelleCombo.DataBind();
            }            
        }

        protected void FunktionCombo_SelectionChanged(object sender, System.EventArgs e)
        {
            DropDownList obj = (DropDownList)sender;

            TabelleCombo.DataSource = null;
            _functionChanged = true;

            if (FunktionCombo.SelectedValue.ToString() == "OSP")
            {
                TabelleCombo.DataSource = _ospList;
                TabelleCombo.DataTextField = "ViewName";
                TabelleCombo.DataValueField = "SprocName";
                _currentFunctionDisplay = "OSP";
            }
            else if (FunktionCombo.SelectedValue.ToString() == "AE")
            {
                TabelleCombo.DataSource = _aeList;
                TabelleCombo.DataTextField = "ViewName";
                TabelleCombo.DataValueField = "SprocName";
                _currentFunctionDisplay = "AE";
            }
            else if (FunktionCombo.SelectedValue.ToString() == "IC")
            {
                TabelleCombo.DataSource = _icList;
                TabelleCombo.DataTextField = "ViewName";
                TabelleCombo.DataValueField = "SprocName";
                _currentFunctionDisplay = "IC";
            }

            TabelleCombo.DataBind();
            TabelleCombo.SelectedIndex = 0;
            SetCountText();
        }

        protected void TabelleCombo_SelectionChanged(object sender, System.EventArgs e)
        {
            DropDownList obj = (DropDownList)sender;
        }

        private void SetCountText(string text = "")
        {
            CountLbl.Text = TabelleCombo.SelectedValue.ToString();
        }

        public static List<ViewInfo> GetSprocList(string function, bool isAdmin, bool isSubAdmin, bool infoPanel = false)
        {
            List<ViewInfo> sl = new List<ViewInfo>();

            switch (function)
            {
                case "OSP":
                    sl.Add(new ViewInfo("First Meetings Set", "sp_Activity_OSP_First"));
                    sl.Add(new ViewInfo("Contacts", "sp_Activity_OSP_Contact"));                   
                    return sl;

                case "AE":                    
                    sl.Add(new ViewInfo("Dials", "sp_Activity_AE_Dials"));
                    sl.Add(new ViewInfo("Dials Daily", "sp_Activity_AE_Dials_Daily"));
                    return sl;

                case "IC":
                    sl.Add(new ViewInfo("Monthly Activity Callstatus", "sp_Activity_IC_CallStatus"));
                    return sl;                

                default:
                    sl.Add(new ViewInfo("First Meetings Set", "sp_Activity_OSP_First"));
                    sl.Add(new ViewInfo("Contacts", "sp_Activity_OSP_Contact"));
                    return sl;
            }
        }
    }

    public class ViewInfo
    {
        public string ViewName { get; set; }
        public string SprocName { get; set; }
        public string ExtraName { get; set; }
        public List<ViewInfo> ViewList { get; set; }

        public ViewInfo()
        {
            this.ViewName = "";
            this.SprocName = "";
        }

        public ViewInfo(string name, string sproc, string extraName = "")
        {
            this.ViewName = name;
            this.SprocName = sproc;
            this.ExtraName = extraName;
        }
    }
}

我对此表示感谢。

好的,我找到了问题,很抱歉,因为它是如此愚蠢。 第一个问题:我删除了不需要的触发器,因为触发它的DDL位于Updatepanel内部,从而解决了该问题。

第二个问题:当我将数据源应用于FunktionCombo_SelectionChanged内部的DDL时,该列表为空,因为我忘记了它将通过回发删除。

所以我只是在运行时创建列表,现在它可以工作了。

_ospList = SprocEvaluator.GetSprocList("OSP", (HttpContext.Current.Session["IsAdmin"] != null) ? (bool)HttpContext.Current.Session["IsAdmin"] : false, (HttpContext.Current.Session["IsSubAdmin"] != null) ? (bool)HttpContext.Current.Session["IsSubAdmin"] : false);
TabelleCombo.DataSource = _ospList;

再次,对此表示抱歉,但感谢您的所有帮助!

暂无
暂无

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

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