C# UpdatePanel中的Dropdownlist未更新
我创建了一个简单的页面,其中UpdatePanel中有两个DropDownList。当第一个Dropdownlist更改选择时,它会更新第二个Dropdownlist的列表,但它不起作用,我不确定我做错了什么 我得到以下错误: 在UpdatePanel“HeadUpdatePanel”中找不到触发器ID为“ctl00$MainContent$FunctionCombo”的控件 即使如此,当我遍历它时,它也会找到它,并且不是空的。 请看这一行:C# UpdatePanel中的Dropdownlist未更新,c#,asp.net,web,C#,Asp.net,Web,我创建了一个简单的页面,其中UpdatePanel中有两个DropDownList。当第一个Dropdownlist更改选择时,它会更新第二个Dropdownlist的列表,但它不起作用,我不确定我做错了什么 我得到以下错误: 在UpdatePanel“HeadUpdatePanel”中找不到触发器ID为“ctl00$MainContent$FunctionCombo”的控件 即使如此,当我遍历它时,它也会找到它,并且不是空的。 请看这一行: DropDownList dd = (DropDow
DropDownList dd = (DropDownList)cph.FindControl("FunktionCombo");
但当它尝试应用触发器时,会出现以下错误:
trigger.ControlID = cph.FindControl("FunktionCombo").UniqueID;
仅供参考:母版页没有内容,只是调用测试页
以下是我的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:" />
<asp:DropDownList ID="FunktionCombo" OnSelectedIndexChanged="FunktionCombo_SelectionChanged" AutoPostBack="true" runat="server" />
<asp:Label ID="Label2" runat="server" Text="Wähle Tabelle:" />
<asp:DropDownList ID="TabelleCombo" OnSelectedIndexChanged="TabelleCombo_SelectionChanged" AutoPostBack="true" runat="server" />
<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;
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
名称空间测试
{
公共部分类测试页:System.Web.UI.Page
{
私人名单(ospList);;
私人名单(aeList);;
私人名单(icList);;
私有布尔函数更改=false;
私有字符串_currentFunctionDisplay=“”;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
HttpContext.Current.Session[“IsAdmin”]=true;
HttpContext.Current.Session[“IsSubAdmin”]=true;
createComboList();
ContentPlaceHolder cph=(ContentPlaceHolder)this.Master.FindControl(“MainContent”);
UpdatePanelControlTrigger=新的PostBackTrigger();
DropDownList dd=(DropDownList)cph.FindControl(“FunktionCombo”);
trigger.ControlID=cph.FindControl(“FunktionCombo”).UniqueID;
HeadUpdatePanel.Triggers.Add(触发器);
}
}
私有void createComboList()
{
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[“IsubAdmin”]!=null)?(bool)HttpContext.Current.Session[“IsubAdmin”]:false);
_aeList=GetSprocList(“AE”,(HttpContext.Current.Session[“IsAdmin”]!=null)?(bool)HttpContext.Current.Session[“IsAdmin”]:false,(HttpContext.Current.Session[“IsubAdmin”!=null)?(bool)HttpContext.Current.Session[“IsubAdmin”]:false);
_icList=GetSprocList(“IC”,(HttpContext.Current.Session[“IsAdmin”]!=null)?(bool)HttpContext.Current.Session[“IsAdmin”]:false,(HttpContext.Current.Session[“IsubAdmin”!=null)?(bool)HttpContext.Current.Session[“IsubAdmin”]:false);
TabelleCombo.DataSource=\u ospList;
TabelleCombo.DataTextField=“ViewName”;
TabelleCombo.DataValueField=“SprocName”;
TabelleCombo.SelectedIndex=0;
TabelleCombo.DataBind();
}
}
受保护的无效FunktionCombo_SelectionChanged(对象发送方,System.EventArgs e)
{
DropDownList obj=(DropDownList)发送方;
TabelleCombo.DataSource=null;
_functionChanged=true;
如果(FunktionCombo.SelectedValue.ToString()=“OSP”)
{
TabelleCombo.DataSource=\u ospList;
TabelleCombo.DataTextField=“ViewName”;
TabelleCombo.DataValueField=“SprocName”;
_currentFunctionDisplay=“OSP”;
}
else if(FunktionCombo.SelectedValue.ToString()=“AE”)
{
TabelleCombo.DataSource=\aeu列表;
TabelleCombo.DataTextField=“ViewName”;
TabelleCombo.DataValueField=“SprocName”;
_currentFunctionDisplay=“AE”;
}
else if(FunktionCombo.SelectedValue.ToString()=“IC”)
{
TabelleCombo.DataSource=\u icList;
TabelleCombo.DataTextField=“ViewName”;
TabelleCombo.DataValueField=“SprocName”;
_currentFunctionDisplay=“IC”;
}
TabelleCombo.DataBind();
TabelleCombo.SelectedIndex=0;
SetCountText();
}
受保护的无效选项卡组合_选择已更改(对象发送方,System.EventArgs e)
{
DropDownList obj=(DropDownList)发送方;
}
私有void SetCountText(字符串text=”“)
{
CountLbl.Text=TabelleCombo.SelectedValue.ToString();
}
公共静态列表GetSprocList(字符串函数,bool isAdmin,bool isSubAdmin,bool infoPanel=false)
{
List sl=新列表();
开关(功能)
{
“外包服务提供商”案例:
sl.Add(新视图信息(“首次会议集”、“sp_活动\u OSP_首次”);
sl.Add(新视图信息(“联系人”、“sp_活动\ OSP_联系人”);
返回sl;
“AE”案:
sl.Add(新视图信息(“拨号”、“sp_活动_AE_拨号”);
sl.Add(新视图信息(“每天拨号”、“sp_活动_AE_每天拨号”);
返回sl;
案例“IC”:
sl.Add(新视图信息(“每月活动呼叫状态”、“sp_活动_IC_呼叫状态”);
返回sl;
违约:
_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;