1

所以我在更新面板中有很多列表框。第一个的 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# 代码隐藏。

  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 工作,但实际上只有 UpdateModels 会影响网页。我不知道为什么。

如果它有帮助,我首先在 Visual Studio Express for Web 2010 中制作了这个项目,然后切换到 Visual Studio 2012 Express for Web。我的项目有一个更新/转换过程。如果这与它有关,请告诉我。

此外,我愿意应要求发布任何其他源代码。例如,如果您认为 web.config 的内容是相关的。我很乐意发布它。

4

3 回答 3

0

你失踪OnSelectedIndexChangedYearList

于 2013-06-28T21:59:04.660 回答
0

你的 UpdatePanel1.Update() 在你的 UpdateYear 函数的底部被注释掉了——你试过取消注释吗?

于 2013-06-28T19:40:59.873 回答
0

不确定,但UpdatePanel1.Update();应该是UpdateYear函数的最后一行。你知道为什么,不是吗?

于 2013-06-30T13:49:06.707 回答