locked
Drop Down List Reset After Post Back in Asp.net web forms RRS feed

  • Question

  • User-1714974833 posted

    i am working on online Shop.I have a problem regarding to dropdownlist basically i have 2 dropdownlist getting data from database.when i select dropdownlist1 that is category then surely dropdownlist 2 shows brands of this category but unfortunately not.it shows 1st category value after post back and also when i select something else instead of 1st value then after post back the dropdownlist1 backs me to 1st value.I have also used script manager and update panel but after using this the dropdownlist 1 remains constant on 1st value. am new in this field and have no idea what to do.I have made lot of searches but can't get right answer.. help its my final year project and am stuck to this dropdown list issue.
    here is my Code
    <tr>
    <td>Category</td><td>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Category_Name" DataValueField="CATID">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Elite SHopConnectionString %>" SelectCommand="SELECT [CATID], [Category_Name] FROM [Categories]"></asp:SqlDataSource>
    </td>
    </tr>
    <tr>
    <td>Brand</td><td>
    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Brand_Name" DataValueField="BID">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Elite SHopConnectionString %>" SelectCommand="SELECT [BID], [Brand_Name] FROM [Brand] WHERE ([CATID] = @CATID)">
    <SelectParameters>
    <asp:ControlParameter ControlID="DropDownList1" Name="CATID" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>
    </td>

    Sunday, November 18, 2018 5:09 AM

Answers

  • User-893317190 posted

    Hi sherazkhan,

    You don't need to use updatePanel to solve your problem .I have found that in your master page ,you nest some form markups in the root form,which causes your problem.

    Please use  ctrl +f to find all the form in your master page and remove all the nested form and ensure there is only one root form in your master page.

      <%-- <form action="form">--%>
                  <select class="country" name="country">
                    <option value="USD">USD</option>
                    <option value="JPN">JPN</option>
                    <option value="BAN">BAN</option>
                  </select>
      <%--          </form>--%>
     <%--   <form action="#">--%>
                <input class="search-box" id="search" type="search" placeholder="Enter Keywords...">
                <button class="search-icon" type="submit"><i class="ti-search"></i></button>
             <%-- </form>--%>
      <%--    <form action="#">--%>
            <select class="form-control w-100 mb-2" name="color">
              <option value="brown">Brown Color</option>
              <option value="gray">Gray Color</option>
              <option value="black">Black Color</option>
            </select>
            <select class="form-control w-100 mb-3" name="size">
              <option value="small">Small Size</option>
              <option value="medium">Medium Size</option>
              <option value="large">Large Size</option>
            </select>
            <button class="btn btn-primary w-100 mb-lg-4 mb-3">add to cart</button>
    <%--      </form>--%>

    There may be other forms in your master page, please remove all the other forms except the root form (with runat="server")in your master page.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 23, 2018 1:30 AM

All replies

  • User-893317190 posted

    Hi sherazkhan,

    It's strange, I have tried similar  code to yours and I find it works well.

    Below is my code. Product has foreign key CategoryID and One category has many Products.

    <form id="form1" runat="server">
            <asp:DropDownList ID="Category" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="CategoryName" DataValueField="CategoryID"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WingtipToysConnectionString %>" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:SqlDataSource>

    <asp:DropDownList ID="Product" runat="server" DataSourceID="SqlDataSource2" DataTextField="ProductName" DataValueField="ProductName"></asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:WingtipToysConnectionString %>" SelectCommand="SELECT [ProductName], [CategoryID], [ProductID] FROM [Products] WHERE ([CategoryID] = @CategoryID)"> <SelectParameters> <asp:ControlParameter ControlID="Category" DefaultValue="1" Name="CategoryID" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> </form>

    The result.

    Where do you put your dropdownlist , do they in a datacontrol such as gridview, listview?

    If they are in a datacontrol,please post your code of the whole datacontrol and the related code behind.

    I also suggest you could make a test page, in the test page there are only your two dropdown list  and have no code behind  and then you could see whether it goes wrong.

    Best regards,

    Ackerly Xu

    Monday, November 19, 2018 5:17 AM
  • User-1714974833 posted

    Same code and same data base run on other Simple web template and works fine..There is no issue regarding to dropdown list.It Select all items and also changes their coressponding categories.

    but this Code Does not works on template which i want to work on.This Template have Plugins and well designed.

    Monday, November 19, 2018 7:25 AM
  • User-893317190 posted

    Hi sherazkhan,

    Could you show your complete code of aspx and code behind so that we could reproduce your problem?

    Best regards,

    Ackerly Xu

    Monday, November 19, 2018 7:29 AM
  • User-1714974833 posted

    Here is My Source Code

    <%@ Page Title="" Language="C#" MasterPageFile="~/Admin_Site.Master" AutoEventWireup="true" CodeBehind="Add_Products.aspx.cs" Inherits="Elite_Shop.Add_Products" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>


    <center>
    <table>
    <tr>
    <td>Product Name</td><td>
    <asp:TextBox ID="TextBox1" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Product Type</td><td>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Product_Type" DataValueField="PTID" CssClass="form-control">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdidasConnectionString %>" SelectCommand="SELECT [PTID], [Product_Type] FROM [Product_Type]"></asp:SqlDataSource>
    </td>
    </tr>
    <tr>
    <td>Category</td><td>
    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="CATName" DataValueField="CATID" CssClass="form-control" AutoPostBack="True">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdidasConnectionString %>" SelectCommand="SELECT [CATID], [CATName] FROM [Categories] WHERE ([PTID] = @PTID)">
    <SelectParameters>
    <asp:ControlParameter ControlID="DropDownList1" Name="PTID" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>
    </td>
    </tr>
    <tr>
    <td>Brand</td><td>
    <asp:DropDownList ID="DropDownList3" runat="server" CssClass="form-control" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="BName" DataValueField="BID">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:AdidasConnectionString %>" SelectCommand="SELECT [BID], [BName] FROM [Brand] WHERE ([PTID] = @PTID)">
    <SelectParameters>
    <asp:ControlParameter ControlID="DropDownList1" Name="PTID" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>
    </td>
    </tr>
    <tr>
    <td>Price</td><td>
    <asp:TextBox ID="TextBox2" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Colour</td><td>
    <asp:TextBox ID="TextBox3" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Weight</td><td>
    <asp:TextBox ID="TextBox4" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Description</td><td>
    <asp:TextBox ID="TextBox5" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Image</td><td>
    <asp:FileUpload ID="FileUpload1" runat="server" CssClass="form-control" />
    </td>
    </tr>
    <tr>
    <td></td><td>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" CssClass="btn btn-primary" />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </td>
    </tr>

    </table>
    </center>

    </ContentTemplate></asp:UpdatePanel>

    </asp:Content>

    Monday, November 19, 2018 7:31 AM
  • User-1714974833 posted

    Here is My Code Behind Submit Button

    protected void Button1_Click(object sender, EventArgs e)
    {

    Product obj = new Product();
    obj.PName = TextBox1.Text;
    obj.PTID = Convert.ToInt32(DropDownList1.SelectedValue);
    obj.CATID = Convert.ToInt32(DropDownList2.SelectedValue);
    obj.BID = Convert.ToInt32(DropDownList3.SelectedValue);
    obj.Price = Convert.ToDouble(TextBox2.Text);
    obj.Weight = TextBox4.Text;
    obj.Colour = TextBox3.Text;
    obj.Description = TextBox5.Text;
    Random num = new Random();
    Double code = num.Next(1000, 100000000);
    obj.image = code + FileUpload1.FileName;
    FileUpload1.SaveAs(Server.MapPath(@"Images/" + obj.image));
    AdidasEntities dbobj = new AdidasEntities();
    dbobj.Products.Add(obj);
    dbobj.SaveChanges();
    Label1.Text = "Product Added";
    clear();

    Monday, November 19, 2018 7:32 AM
  • User-1714974833 posted

    Front end of formForm

    Monday, November 19, 2018 7:35 AM
  • User-893317190 posted

    Hi sherazkhan,

    I'm sorry , even use the code you have posted, I still couldn't reproduce your problem.In my case, it still works well.

    I found you are using a master page, maybe it is your master page that causes your problem, you could move your code to an aspx without master page and have a try to see whether it is caused by master page.

    If it is true, please post your code in your master page.

    I'm also not clear about your method "clear()" , what do you do the method?

    Below is my result. I even put a button outside the updatepanel , still couldn't reproduce your problem.

    I also found you want to update a file in updatepanel, you should pay attention  fileuploading may not work using updatepanel,if you meet file upload problem, please remove your updatepanel.

    Best regards,

    Ackerly Xu

    Tuesday, November 20, 2018 1:58 AM
  • User-1714974833 posted
    Thanks to you..
    You have give your 100 percent but unfortunately i couldn't not find a solution to overcome this problem.I have made lot of changes but found samw result..
    I think there is an issue regarding to template which i am using.What you think??
    Because the Same Code,same datebase and even same code behind submitbutton when puts onto simple template it works fine..
    The method that is clear method which i used to clear results of textbox when i clicked on submit button..
    As far as your suggestion to put code in aspx form i will try..
    My all focus goes to template.I Think there is an issue in template which prevents drop-down list from working.What you think can template cause this issue??
    Tuesday, November 20, 2018 4:22 AM
  • User-1714974833 posted
    I Know when update panel is implemented image doesn't works..
    Actually i do this may be post back issue solved and also it prevents from page reloading
    Tuesday, November 20, 2018 4:24 AM
  • User-1714974833 posted
    Do you have any solution using update panel still image works??
    Tuesday, November 20, 2018 4:26 AM
  • User-1714974833 posted
    I have also run my code on aspx form without inherited master page but still found the samw results.
    So i think it is confirmed there is an template issue..
    Tuesday, November 20, 2018 4:38 AM
  • User-893317190 posted

    Hi sherazkhan,

    My code is as follows. And it works well, have you posted all your code?I'm sorry,still couldn't reproduce your problem.

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
    
    <center>
    <table>
    <tr>
    <td>Product Name</td><td>
    <asp:TextBox ID="TextBox1" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Product Type</td><td>
    
    
    
    </td>
    </tr>
    <tr>
    <td>Category</td><td>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:WingtipToysConnectionString %>" SelectCommand="SELECT [ProductName], [Description], [UnitPrice] FROM [Products]"></asp:SqlDataSource>
    
            <asp:DropDownList ID="Category" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="CategoryName" DataValueField="CategoryID"></asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td>Brand</td><td>
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WingtipToysConnectionString %>" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:SqlDataSource>
            <asp:DropDownList ID="Product" AutoPostBack="True" runat="server" DataSourceID="SqlDataSource2" DataTextField="ProductName" DataValueField="ProductName"></asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:WingtipToysConnectionString %>" SelectCommand="SELECT [ProductName], [CategoryID], [ProductID] FROM [Products] WHERE ([CategoryID] = @CategoryID)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="Category" DefaultValue="1" Name="CategoryID" PropertyName="SelectedValue" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>
    </td>
    </tr>
    <tr>
    <td>Price</td><td>
    <asp:TextBox ID="TextBox2" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Colour</td><td>
    <asp:TextBox ID="TextBox3" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Weight</td><td>
    <asp:TextBox ID="TextBox4" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Description</td><td>
    <asp:TextBox ID="TextBox5" runat="server" CssClass="form-control"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>Image</td><td>
    <asp:FileUpload ID="FileUpload1" runat="server" CssClass="form-control" />
    </td>
    </tr>
    <tr>
    <td></td><td>
    <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" CssClass="btn btn-primary" />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </td>
    </tr>
    </table>
    </center>
    </ContentTemplate></asp:UpdatePanel>
    
      <div style="padding-left:250px"><asp:Button ID="Button1" runat="server" Text="ButtonOutSideUpdaePanel"  /></div> 
               
        </form>

    Best regards,

    Ackerly Xu

    Tuesday, November 20, 2018 4:44 AM
  • User-1714974833 posted
    Hi😊
    I Have find the issue..When i disabled java script using Chrome browser the drop down list start working so issue is in the java script
    But by turning off java script many of template features doesn't works like slider changing images..
    Now let me help what to do??
    Actually What should i do??
    is there any solution??
    Wednesday, November 21, 2018 3:48 PM
  • User-893317190 posted

    Hi sherazkhan,

    The problem seems to be caused by js.

    But you had  better not disable all the javascript.

    Please try to remove one or related javascript in your aspx  once to find out which javascript causes your problem.

    If you find the javascript , then remove it.

    Best regards,

    Ackerly Xu

    Thursday, November 22, 2018 1:08 AM
  • User-893317190 posted

    Hi sherazkhan,

    You don't need to use updatePanel to solve your problem .I have found that in your master page ,you nest some form markups in the root form,which causes your problem.

    Please use  ctrl +f to find all the form in your master page and remove all the nested form and ensure there is only one root form in your master page.

      <%-- <form action="form">--%>
                  <select class="country" name="country">
                    <option value="USD">USD</option>
                    <option value="JPN">JPN</option>
                    <option value="BAN">BAN</option>
                  </select>
      <%--          </form>--%>
     <%--   <form action="#">--%>
                <input class="search-box" id="search" type="search" placeholder="Enter Keywords...">
                <button class="search-icon" type="submit"><i class="ti-search"></i></button>
             <%-- </form>--%>
      <%--    <form action="#">--%>
            <select class="form-control w-100 mb-2" name="color">
              <option value="brown">Brown Color</option>
              <option value="gray">Gray Color</option>
              <option value="black">Black Color</option>
            </select>
            <select class="form-control w-100 mb-3" name="size">
              <option value="small">Small Size</option>
              <option value="medium">Medium Size</option>
              <option value="large">Large Size</option>
            </select>
            <button class="btn btn-primary w-100 mb-lg-4 mb-3">add to cart</button>
    <%--      </form>--%>

    There may be other forms in your master page, please remove all the other forms except the root form (with runat="server")in your master page.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 23, 2018 1:30 AM
  • User-1714974833 posted
    Waoo Drop Down List Starts Working..
    Thanks Ackerly.
    Friday, November 23, 2018 3:26 AM