所以我在更新面板中有很多列表框。第一个OnselectedIndexChanged事件触发一个更新下一个的方法,依此类推。现在只有第一个列表框正确更新第二个列表框。第二个不会像它应该更新第三个。
在调试时。我点击第二个ListBox(ModelList),它根据调试器正确触发事件。 C#代码隐藏中的值正在更新,但更改不会显示在网页上,就像它们在第一个中一样。
有一个标签,例如我可以在调试中看到它的文本属性更改。但它没有改变网站。 另一件有趣的事情是,一旦第二个列表框被点击,第一个列表框就会停止更新,但代码隐藏方法仍然会毫无例外地触发: - /
好的谈话。这是我的代码。 (相关部分)。
ASP.NET代码
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<%--register triggers for Partial postback --%>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="showbutton" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="viewapps" EventName="Click" />
</Triggers>
<ContentTemplate>
<%--- the controls in their rows and columns --%>
<%--column 1 --%>
<asp:Panel runat="server" CssClass="column1">
<asp:Panel ID="Panel1" runat="server" CssClass="row1">
<%-- Make Panel --%>
<asp:Label runat="server" ID="TESTER">Text</asp:Label>
<span runat="server" style="padding:8px; position:relative;" >
<asp:Label ID="label1" runat="server" Text="Make" Font-Size="Large" ></asp:Label>
<asp:Listbox AutoPostback="true" ID="MakeList" runat="server" Width="166px" SelectionMode ="Multiple" DataTextField="MakeName" DataValueField="MakeID" OnSelectedIndexChanged="UpdateModels" DataSourceID="MakeSource">
</asp:Listbox>
<asp:SqlDataSource runat="server" ID="MakeSource" ConnectionString="<%$ ConnectionStrings:VCDBConnectionString %>"></asp:SqlDataSource>
</span>
</asp:Panel>
<%--Model Panel --%>
<asp:Panel ID="Panel2" CssClass="row2" runat="server">
<span runat="server" style="padding:8px; position:relative;">
<asp:Label ID="label8" runat="server" Text="Model" Font-Size="Large" ></asp:Label>
<asp:Listbox ID="ModelList" runat="server" Width="166px" SelectionMode="Multiple" DataSourceID="ModelSource" OnSelectedIndexChanged="UpdateYear" AutoPostBack="true">
</asp:Listbox>
<asp:SqlDataSource ID="ModelSource" runat="server" ConnectionString="<%$ ConnectionStrings:VCDBConnectionString %>" > </asp:SqlDataSource>
</span>
</asp:Panel>
</asp:Panel> <%--End of Column1 --%>
<%-- column 3--%>
<asp:Panel CssClass="column3" runat="server">
<%--Year Panel --%>
<asp:Panel ID="Panel7" CssClass="row1" runat="server">
<span runat="server" style="padding:8px; position:relative;">
<asp:Label ID="label13" runat="server" Text="Year" Font-Size="Large" ></asp:Label>
<asp:Listbox AutoPostback="true" ID="YearList" runat="server" Width="166px" SelectionMode="Multiple" DataSourceID="YearSource">
</asp:Listbox>
<asp:SqlDataSource ID="YearSource" runat="server" ConnectionString="<%$ ConnectionStrings:VCDBConnectionString %>"></asp:SqlDataSource>
</span>
</asp:Panel>
<%--End of Content Template! --%>
<%--Don't put any Dynmaic content past here! --%>
</ContentTemplate>
</asp:UpdatePanel>
C#codebehind。
public partial class MassUpdate : System.Web.UI.Page
{
//setup connection strings
// string VCDBconnect = ConfigurationManager.ConnectionStrings["VCDBConnectionString"].ConnectionString;
// string ACESconnect = ConfigurationManager.ConnectionStrings["ACESConnectionString"].ConnectionString;
//utility lists for building complex queries
List<string> selectedMakes= new List<string>();
List<string> selectedModels = new List<string>();
List<string> selectedYears = new List<string>();
List<string> selectedSubmodels = new List<string>();
List<string> selectedEngines = new List<string>();
List<string> selectedLocations = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false) {
MakeSource.SelectCommand = "SELECT [MakeID], [MakeName] FROM [Make] ORDER BY [MakeName]";
//setup the EVENTS
// MakeList.SelectedIndexChanged += UpdateModels;
}//end of postback==false
}//end of Page Load
// called by selected index changed on Make
public void UpdateModels(object sender, EventArgs e)
{
//build a string for a SQL query for the Models
string baseQuery = "SELECT DISTINCT M.[ModelID], M.[ModelName] FROM Model M INNER JOIN BaseVehicle BV ON BV.ModelID = M.ModelID Where BV.MakeID= '";
string newQuery = "";
selectedMakes.Clear();
//build a query into a list which will be compiled later into a single string
List<string> queryBuilder = new List<string>();
//add the base query
queryBuilder.Add(baseQuery);
//add the seleted items to items in the list
foreach (ListItem li in MakeList.Items)
{
if (li.Selected)
{
queryBuilder.Add(li.Value);
queryBuilder.Add("' OR BV.MakeID = '");
//build the list of selected makes for later use
selectedMakes.Add(li.Value);
}
}
try
{
//remove the last ' AND BV.MakeID= '
queryBuilder.RemoveAt(queryBuilder.Count-1);
//add back the ' and the orderby
queryBuilder.Add("'");
queryBuilder.Add(" ORDER BY [ModelName]");
//build the string
foreach(string s in queryBuilder){
newQuery+= s;
}
//debug for visibilty
TESTER.Text =newQuery;
}
catch (ArgumentNullException) { TESTER.Text = "Argument Null"; }
catch (IndexOutOfRangeException) { TESTER.Text = "Index out of range"; }
catch (UpdateException) { TESTER.Text = "Update Problems"; }
catch (Exception) { TESTER.Text = "Other Problems"; }
ModelSource.SelectCommand = newQuery;
ModelList.DataTextField="ModelName";
ModelList.DataValueField = "ModelID";
// GroupList.Enabled = false;
// YearList.Enabled = false;
}
//called by onSelectedIndexchanged event from Model
public void UpdateYear(object sender, EventArgs e)
{
TESTER.Visible = false;
UpdatePanel1.Update();
try
{
//empty it so it doesn't reuse old selections in future queries.
selectedModels.Clear();
//build a string for a SQL query for the Models
//basic idea = SELECT [YearID] FROM [BaseVehicle] Where [YearID] >='1950' AND ([MakeID] = '' ) AND ([ModelID] = '') ORDER BY [YearID]
string baseQuery = "SELECT [YearID] FROM [BaseVehicle] Where [YearID] >='1950' AND (";
string addOn = " ORDER BY [YearID]";
string newQuery = "";
//build a query into a list which will be compiled later into a single string
List<string> queryBuilder = new List<string>();
//add the base query
queryBuilder.Add(baseQuery);
//will need a for each loop for each where clause group
//will need one for each loop for buiilding the selected list for model
//will need a final foreach loop to build the query
//add the seleted items from the make list
queryBuilder.Add("[MakeID] ='");
foreach (string li in selectedMakes)
{
queryBuilder.Add(li);
queryBuilder.Add("' OR [MakeID] = '");
}
//<---- cleanup area ---->
//remove the last ' OR MakeID= '
queryBuilder.RemoveAt(queryBuilder.Count - 1);
//add the ' to close the last ID value
queryBuilder.Add("'");
//close the where clause group with a )
queryBuilder.Add(") ");
//<---- END cleanup area ---->
//start the new where clause group
queryBuilder.Add("AND ([ModelID ='");
foreach (ListItem li in ModelList.Items)
{
if (li.Selected == true)
{
//add the selected item's ID to the queryBuilder as a string
queryBuilder.Add(li.Value);
queryBuilder.Add("' OR [ModelID] = '");
//build selected model list for later use
selectedModels.Add(li.Value);
}
}
//<---- cleanup area ---->
//remove the last ' OR ModelID = '
queryBuilder.RemoveAt(queryBuilder.Count - 1);
//add the ' to close the last ID value
queryBuilder.Add("'");
//close the where clause group with a )
queryBuilder.Add(") ");
//<---- END cleanup area ---->
//add the addons(ending clauses such as ORDER BY)
queryBuilder.Add(addOn);
//Build the query!!!
//build the string
foreach (string s in queryBuilder)
{
/*somehow there are 2 select statements that are built right here */
newQuery += s;
}
try
{
//debug for visibilty
TESTER.Text = newQuery;
YearSource.SelectCommand = newQuery;
YearList.DataTextField = "YearID";
YearList.DataValueField = "YearID";
}
catch(Exception k)
{
TESTER.Text = "SQL small block fail " +k.ToString();
}
}
catch (Exception j) {
TESTER.Text = j.ToString();
}
//UpdatePanel1.Update();
}//end of method
同样,两种方法都按照Debug工作,但只有UpdateModel实际上影响了网页。我不明白为什么。
如果我帮助我首先在Visual Studio Express for Web 2010中创建此项目,则切换到Visual Studio 2012 Express for Web。我的项目有更新/转换过程。如果这与它有关,请告诉我。
此外,我愿意根据要求发布任何其他源代码。如果您认为web.config的内容是相关的,例如。我很乐意发布它。
答案 0 :(得分:0)
您的UpdatePanel1.Update()在UpdateYear函数的底部被注释掉了 - 您是否尝试取消注释?
答案 1 :(得分:0)
OnSelectedIndexChanged
上您遗失了YearList
。
答案 2 :(得分:0)
不确定,但UpdatePanel1.Update();
应该是UpdateYear
功能的最后一行。你知道为什么,不是吗?