RadComboBox postback issue with OnTextChanged

4 posts, 0 answers
  1. Bob
    Bob avatar
    3 posts
    Member since:
    May 2012

    Posted 22 Mar 2014 Link to this post


    I'm trying to implement a cascading type behvaior between RadComboBoxes so that a second ComboBox gets populated when a user checks items in the first ComboBox. I've wired up a handler on the OnTextChanged event because I wanted my logic to execute after a users has finished checking the desired items not after each item is checked. I'm setting the initial checked items in the first ComboBox during Page Load based on an existing record in the Database. What I'm noticing is that the OnTextChanged event is firing during the first Post Back to the page regardless of which control initiated the Post Back. I've mocked up a sample page to recreate the behavior.

    aspx:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test2.aspx.cs" Inherits="Test2" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body style="background-color: #c3cce5">
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadComboBox ID="RadDropDownList" runat="server"
            MaxHeight="300px" Width="200px" DropDownAutoWidth="Enabled" OnTextChanged="RadDropDownList_TextChanged"
            EmptyMessage="--Choose--" CheckBoxes="true" AutoPostBack="true" >
            <Items>
                <telerik:RadComboBoxItem Value="test1" Text="Test1" />
                <telerik:RadComboBoxItem Value="test2" Text="Test2" />
                <telerik:RadComboBoxItem Value="test3" Text="Test3" />
                <telerik:RadComboBoxItem Value="test4" Text="Test4" />
                <telerik:RadComboBoxItem Value="test5" Text="Test5" />
            </Items>
        </telerik:RadComboBox>
        <asp:Button ID="UpdateButton" runat="server"
            Text="Update" OnClick="UpdateButton_Click" />
        </form>
    </body>
    </html>

    aspc.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
     
    public partial class Test2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                System.Diagnostics.Debug.WriteLine("Page Load PostBack");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Page Load No PostBack");
                //Select a value
                if (RadDropDownList.FindItemByText("Test3") != null)
                {
                    RadDropDownList.FindItemByText("Test3").Checked = true;
                }
            }
        }
     
        protected void RadDropDownList_TextChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("RadDropDownList_TextChanged");
        }
     
        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("UpdateRuleButton_Click");
        }
    }


    When I run the page, I see the following output:
        Page Load No PostBack

    When I click on the Update Button (i.e. I haven't changed anything in the ComboBox), I see the following output:
        Page Load PostBack
        RadDropDownList_TextChanged
        UpdateRuleButton_Click

    Notice that the RadDropDownList_TextChanged method is getting called even though the postback was initated from the Update Button. Why/how is the RadDropDownList_TextChanged getting executed? Is it due to the fact of me setting the value during Initial Page Load?

    If I click the Button again, I get the expected behavior:
        Page Load PostBack
        UpdateRuleButton_Click

    If I comment out the checking of the Item during Page Load of the ComboBox, then I don't experience the weird Behavior. Wondering if this is a bug or working as designed.



  2. Aneliya Petkova
    Admin
    Aneliya Petkova avatar
    207 posts

    Posted 24 Mar 2014 Link to this post

    Hello Bob,

    Thank you for contacting us.

    The TextChanged event occurs when the text in the input area of RadComboBox changes. In your case, you check an item on the server and on the first PostBack the event is fired - this is the standard behavior. Could you please explain in more details the exact functionality you want to achieve or perhaps you can give us an example?
    Please check our online demo on how you can create related RadComboBox controls:
    http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multiplecomboboxes/defaultcs.aspx

    Hope you will find this helpful.


    Regards,
    Aneliya Petkova
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bob
    Bob avatar
    3 posts
    Member since:
    May 2012

    Posted 24 Mar 2014 in reply to Aneliya Petkova Link to this post

    I sort of figured that was happening, but it was still a surprise to find that checking items in Page Load would still cause it to fire on the first post back.

    I've got a form where I can let users create rules. These rules get stored in a database so that users can edit the rule after its been created. One particular field in the rule has a dependency on another field. For example if the user picked an apple, then the second ComboBox would display the available attributes for an apple (i.e. grows on tree, red, etc.). If a user picked both apple and orange, then the 2nd drop down would only display attributes that are common between the apple and orange (i.e. grows on a tree.) Note: These aren't the actual rules but just an example to explain the behavior. So, the second ComboBox needs to be dynamically populated based on values the users has selected in the first. When the rule is loaded from the database, I need to select the existing items from ComboBox 1 and 2 that were previously saved which is causing the control to be in the state of "The Text has Changed".

    I've already implemented a work around using a HiddenField and javascript on the OnClientTextChange event to drive my expected behavior and prevent the firing of the event on first postback (i.e. don't reload ComboBox 2 if nothing in ComboBox 1 has been changed.)
      
  5. Aneliya Petkova
    Admin
    Aneliya Petkova avatar
    207 posts

    Posted 24 Mar 2014 Link to this post

    Hello Bob,

    I had performed some tests, but in all scenarios TextChanged event is fired on the first postback. I'm afraid that the current event propagation is by design and I would suggest you to apply the workaround that you had found. 

    If you have additional questions I will be glad to assist you any further.


    Regards,
    Aneliya Petkova
    Telerik
     

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

     
Back to Top