事件触发了方法,但没有';t更新页面

本文关键字:更新 方法 事件 | 更新日期: 2023-09-27 18:20:15

所以我在更新面板中有很多列表框。第一个的OnselectedIndexChanged事件触发一个方法来更新下一个,依此类推。现在只有第一个列表框正确地更新第二个。第二个没有像应该的那样更新第三个。

以及在调试时。我点击第二个列表框(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的内容是相关的。我很乐意发布它。

事件触发了方法,但没有';t更新页面

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

您在YearList上缺少OnSelectedIndexChanged

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