簡體   English   中英

DDL的SelectedIndexchanged沒有觸發(AutoPostBack =“ true”)

[英]Selectedindexchanged for DDL is not firing (AutoPostBack = “true”)

所以這是怎么回事。 我有一個在代碼隱藏中創建的表,如下所示:

 protected void Page_Load(object sender, EventArgs e)
    {
            RetrievedValue = SearchBox.Text;
            GetData(RetrievedValue); //Creates a Datatable, populates it and binds it to a GridView
    }

現在,我知道使用if(!IsPostBack)是解決此問題的有效方法。 不幸的是,如果我嘗試此操作,則不會創建表。 我相信這是因為我正在使用搜索框的結果來創建表,但是我不確定。 (說實話,我認為這就是問題所在,但是我會其余部分告訴您)

因此,我想使用gridview的頁腳在此表中添加一行以顯示三個Dropdownlists,其中一個列表項的選擇將更新下一個列表。 到目前為止,我已經能夠使第一個Dropdownlist工作正常,但是,當我選擇一個選項時,什么也沒發生。 這是我的下拉列表代碼:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems='True' 
       DataSourceID="SqlDataSource1" AutoPostBack = "true" EnableViewState = "true"
       OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
       DataTextField="field1" DataValueField="field1">
               <asp:ListItem Value="-2" Text="--choose--" Selected="True" />
               <asp:ListItem Value="-1" Text="ALL" />
</asp:DropDownList>

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
       <Triggers> 
               <asp:AsyncPostbackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" /> 
       </Triggers> 
</asp:UpdatePanel> 


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" 
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
    OldValuesParameterFormatString="original_{0}" 
    SelectCommand="SELECT DISTINCT field1 FROM table">
</asp:SqlDataSource>

這是我的C#方法:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("SelectedIndexChanged"); //not being called
    }

對於這個問題的任何幫助將不勝感激! 我知道這與Page_Load有關,但是我不確定如何以其他方式進行!

謝謝堆:)

這是GridView代碼:

<asp:GridView ID="GridView8" runat="server" AllowPaging="True" AutoGenerateColumns="false" 
     AllowSorting="True" CellPadding="4" OnRowDataBound="GridView8_RowDataBound" 
     ForeColor="#333333" GridLines="None" PageSize="20" Width="100%" ShowFooter="false" >
     <Columns>
       <asp:TemplateField HeaderText=" ">
         <FooterTemplate>
            <asp:LinkButton id="Insert" runat="server" CausesValidation="True" Text="Insert"  OnClick="Insert_Click" /><br />
            <asp:LinkButton id="Cancel" runat="server" CausesValidation="True" Text="Cancel" OnClick="Cancel_Click"  />
         </FooterTemplate>
       </asp:TemplateField>

       <asp:TemplateField HeaderText="FieldName">
         <ItemTemplate>
            <%# Eval("FieldName")%>
         </ItemTemplate>
         <FooterTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems='True' DataSourceID="SqlDataSource1" AutoPostBack = "true"
                  EnableViewState = "true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                  DataTextField="field1" DataValueField="field1">
                  <asp:ListItem Value="-2" Text="--choose--" Selected="True" />
                  <asp:ListItem Value="-1" Text="ALL" />
            </asp:DropDownList>

            <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
                  <Triggers> 
                      <asp:AsyncPostbackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" /> 
                  </Triggers> 
            </asp:UpdatePanel> 

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                 ConflictDetection="CompareAllValues" 
                 ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
                 OldValuesParameterFormatString="original_{0}" 
                 SelectCommand="SELECT DISTINCT field1 FROM table">
            </asp:SqlDataSource>
         </FooterTemplate>
       </asp:TemplateField>

                    <asp:TemplateField HeaderText="FieldName2">
                        <ItemTemplate>
                            <%# Eval("FieldName2")%>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems='True' Enabled="false">
                                              <asp:ListItem Value="-2" Text="--choose--" />
                                              <asp:ListItem Value="-1" Text="ALL" />
                            </asp:DropDownList>
                        </FooterTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="FieldName3">
                        <ItemTemplate>
                            <%# Eval("FieldName3") %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems='True' Enabled="false">
                                              <asp:ListItem Value="-2" Text="--choose--" />
                                              <asp:ListItem Value="-1" Text="ALL" />
                            </asp:DropDownList>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>  
            </asp:GridView>

和getData方法:

protected void GetData(String str)
{
    DataTable table = new DataTable();
    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
    {
        string sql = "SELECT FieldName3, FieldName2, FieldName1 FROM table t WHERE field5 LIKE @field5";
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
            cmd.Parameters.Add(new SqlParameter("@field5", str));

            using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
            {
                ad.Fill(table);
            }
        }
    }

    /*Populate table with adjusted data*/
    DataTable table2 = new DataTable();
    if (str.Length != 0 && table.Rows.Count != 0)
    {
        String NameCodes1 = table.Rows[0]["FieldName3"].ToString();
        String NameCodes2 = table.Rows[0]["FieldName2"].ToString();
        String NameCodes3 = table.Rows[0]["FieldName1"].ToString();

        if (String.CompareOrdinal(NameCodes1, "%") == 0) //if ALL
        {
            DataColumn dcol = new DataColumn("FieldName3", typeof(System.String));
            table2.Columns.Add(dcol);
            DataColumn dcol2 = new DataColumn("FieldName2", typeof(System.String));
            table2.Columns.Add(dcol2);
            DataColumn dcol3 = new DataColumn("FieldName1", typeof(System.String));
            table2.Columns.Add(dcol3);
            CheckPHO(NameCodes2, NameCodes3, table2, 0); //adds rows depending on whether there are multiple codes in the field
        }
        else //not ALL
        {
            pracCodes = Regex.Replace(NameCodes1, ",", "','");
            pracCodes = "'" + NameCodes1 + "'";
            using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
            {
                string sql = "SELECT name AS FieldName3, t2.[PHO_Name] AS FieldName2, t1.field1 AS FieldName1 FROM table3 t3 " +
                                "LEFT JOIN table2 t2 ON t3.PHO = t2.PHO_ID " +
                                "LEFT JOIN table1 t1 ON t2.DHB_ID = t1.DHB_ID " +
                                "WHERE IDPractice IN (" + NameCodes1 + ")";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    {
                        ad.Fill(table2);
                    }
                }
            }
        }
    }

    GridView8.DataSource = table2;
    GridView8.DataBind();
}

如果我正確地理解了您,則說明您正在動態創建表。 最遲必須在Page_load中的每個回發上執行此操作。

\n

因此,您只需要綁定GridView if(!IsPostBack)但綁定每個回發中的表,從而將兩個方法分開。

在您編輯完問題后,我知道您的意思了。 因此,您需要根據用戶輸入創建GridView的數據源。

if(!IsPostBack) (帶有默認數據)時,才需要從Page_Load DataBind GridView。 然后,您可以處理SearchBox TextChanged事件(或應用搜索的按鈕)。 從那里,您需要使用搜索參數對GridView進行數據DataBind

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM