DropDownList Autopostback clears fields, reverts to default

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amvigill
    New Member
    • Aug 2012
    • 5

    DropDownList Autopostback clears fields, reverts to default

    Hi,

    I have two drop down lists inside of two different fields in a detailsview, the first one determines the values of the second one.

    When i select a value from the first ddl autopostback erases all data already in the detailsview and reverts the first dropdownlist back to the first selection.

    Also when i select from the list (change the ddl selection more than once) it will throw a :

    Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidO perationExcepti on: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    Source Error:

    [No relevant source lines]

    Code:
    -------First DDL
    
         <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SQLLEAVECODE"
                 DataTextField="LEAVETYPE" DataValueField="LEAVECODE"
                 onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                 AppendDataBoundItems="True" ViewStateMode="Enabled" AutoPostBack="True"
                 SelectedValue='<%# BIND("REQLEAVECODE") %>' Width="80px">
                 <asp:ListItem> </asp:ListItem>
          </asp:DropDownList>
    
    -------Second DDL
    
        <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1"
              DataTextField="REASON" DataValueField="REASON" 
              SelectedValue='<%# Bind("leavetype") %>'>
        </asp:DropDownList>
    
    
    
    ---------page load
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DetailsView1.DataBind();
                }
            }
    
    
    --------DDL 1 selectedIndexChange
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList lvtype = (DropDownList)(DetailsView1.FindControl("DropDownList6"));
                DropDownList lvreason = (DropDownList)(DetailsView1.FindControl("DropDownList5"));
                Label27.Text = lvtype.SelectedValue.ToString();
      
                DetailsView1.Fields[4].Visible = true;
            }
  • ariful alam
    New Member
    • Jan 2011
    • 185

    #2
    What do you need actually? Do you need to change the value in DropDownList_2 those linked to value of DropDownList_1 in every selection of DropDownList_1?

    Comment

    • amvigill
      New Member
      • Aug 2012
      • 5

      #3
      Yes, DropDownList_1 needs to change the values in list 2 ... but some of the values of DropDownList_1 need to be the exact value of DropDownList_2 if there is no other values that correspond to DropDownList_1 in DropDownList_2.

      For example, Sick Leave will be in DropDownList_1 and it will have 6 different options in DropDownList_2. But Personal Holiday in DropDownList_1 will only have one option in DropDownList_2 which will be itself, personal holiday

      Comment

      • ariful alam
        New Member
        • Jan 2011
        • 185

        #4
        to change automatically the values of list_2 because of selecting an item from list_1, you can use Microsoft Ajax Toolkit. This is the best to do this. you can see an example here(http://www.asp.net/ajaxLibrary/AjaxC...gDropDown.aspx). to download Microsoft Ajax Toolkit visit http://ajaxcontroltoolkit.codeplex.com/

        Now the second requirement. Basically, the list values come from database tables. like for first list i may use Category Table and for second list i may use Sub_category Table. If any record in Category Table has no linked record in Sub_category Table, i used to store the same record of Category Table in Sub_category Table as linked data. So, the selection of an item in list_1 also shows in list_2.

        Comment

        • amvigill
          New Member
          • Aug 2012
          • 5

          #5
          Thank you!

          That solution did partially work, but the only problem with it is i have to select the first DropDownList1 value twice for it to stay loaded, autopostback still clears all fields the first time, but the dropdownlists seem to be responding properly other than that.

          Comment

          • amvigill
            New Member
            • Aug 2012
            • 5

            #6
            Sorry, Actually it worked for a few times but now it is immediatly throwing this error


            Code:
            Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
            
            Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
            
            Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
            
            Source Error: 
            
            
            [No relevant source lines]
            
            Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\9b1781c5\212dfe37\App_Web_0td2wxdy.1.cs    Line: 0 
            
            Stack Trace: 
            
            
            [InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.]
               System.Web.UI.Page.GetDataItem() +2802118
               ASP.authorized_leaverequest_newrequestform_aspx.__DataBinding__control69(Object sender, EventArgs e) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\9b1781c5\212dfe37\App_Web_0td2wxdy.1.cs:0
               System.Web.UI.Control.OnDataBinding(EventArgs e) +91
               System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +14
               System.Web.UI.WebControls.ListControl.PerformSelect() +34
               System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
               System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
               System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +29
               System.Web.UI.Control.PreRenderRecursiveInternal() +103
               System.Web.UI.Control.PreRenderRecursiveInternal() +175
               System.Web.UI.Control.PreRenderRecursiveInternal() +175
               System.Web.UI.Control.PreRenderRecursiveInternal() +175
               System.Web.UI.Control.PreRenderRecursiveInternal() +175
               System.Web.UI.Control.PreRenderRecursiveInternal() +175
               System.Web.UI.Control.PreRenderRecursiveInternal() +175
               System.Web.UI.Control.PreRenderRecursiveInternal() +175
               System.Web.UI.Control.PreRenderRecursiveInternal() +175
               System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496

            Comment

            • ariful alam
              New Member
              • Jan 2011
              • 185

              #7
              Is that error comes because of DropDownLists?

              Do you tested the cascading dropdownlist of Microsoft Ajax Toolkit?

              Whats are in your page? can you attach your .aspx and .cs file here with the database table files that require for this?

              Comment

              • amvigill
                New Member
                • Aug 2012
                • 5

                #8
                Sorry I cannot post the tables required for this. The cascading dropdownlist isnt working well because it is in a detailsview and the ddls are in seperate fields.

                Thank you for all your help though.

                Comment

                Working...