Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:55:55+00:00 2026-06-01T08:55:55+00:00

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

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:55:56+00:00Added an answer on June 1, 2026 at 8:55 am

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code (Say we have a single button on the page that
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here's the setup - I have a view that lists products. On that same
Here is a scenario: User installs .NET application that you have made. After some
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my problem...I have a page that loads a list of clients and
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.