简体   繁体   中英

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

so here's what's going on. I have a table that is created in code-behind like so:

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

Now, I understand that using if(!IsPostBack) is a valid solution to this problem. Unfortunately if I try this the table is not created. I believe this is because I am using the result of my searchbox to create the table, but I am uncertain. (To be honest, I think this is where the problem lies, but I'll tell you the rest anyway)

So I want to add a row to this table using the footer of the gridview to show three Dropdownlists, where the selection of one list item updates the next list. So far I have been able to get the first Dropdownlist working fine, however when I select an option nothing happens. Here is my Dropdownlist code:

<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>

Here is my c# method:

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

Any help for this problem would be greatly appreciated! I know it's something to do with the Page_Load, but I am not sure how to do it any other way!

Thanks heaps :)

Here is the GridView code:

<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>

And the getData method:

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();
}

If i understood you correctly, you're creating a table dynamically. This have to be done on every postback in Page_load at the latest.

\n

So you simply need to bind the GridView if(!IsPostBack) but the table on every postback, hence separate both mentods from each other.

After you've edited your question i know what you mean. So you need to create the DataSource of the GridView according to user input.

You need to DataBind your GridView from Page_Load only if(!IsPostBack) (with default data). Then you could handle the SearchBox TextChanged event(or a button that applies the search). From there you need to DataBind the GridView accordingly with the search-parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM