ViewState issue with RadButton as CheckBox

4 posts, 0 answers
  1. Aljoscha
    Aljoscha avatar
    5 posts
    Member since:
    Mar 2013

    Posted 08 Jun 2015 Link to this post

    Hello,

    I'm just sticking with a problem regarding the viewstate of a RadButton.

    I want to create a RadButton dynamically and use it then further on. This given, I have two szenarios:

    1. I create a RadButton and check it with the mouse. On the following PostBack, everything is OK and the checked- property has the expeted value.

    2. I create a RadButton and check it during creation. If I don't change the state in the frontend, then the checked- property is alsways false, not matter, what I set it to.

    The shown value of the RadButton is always correct, until the mysterious PostBack. If I try to produce the behaviour with the ASP:CheckBox, everything works fine.

    Has somebody a hint, what my mistake is?

    Yours Aljoscha

     

    Here is my example code to reproduce:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>
     
    <!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>
        <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
    </head>
    <body>
        <form id="form1" runat="server">
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
                <Scripts>
                    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
                    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
                    <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
                </Scripts>
            </telerik:RadScriptManager>
            <div id="myDiv" runat="server">
                <telerik:RadButton runat="server" OnClick="Unnamed_Click"></telerik:RadButton>
                <asp:Label runat="server" ID="lbText"></asp:Label>
            </div>
     
        </form>
    </body>
    </html>

    protected void Page_Load(object sender, EventArgs e)
       {
     
           RadButton rb = (RadButton)LoadControl(typeof(RadButton), new object[0]);
           // RadButton rb = new RadButton();
           rb.ID = "Test1234";
           rb.ButtonType = RadButtonType.ToggleButton;
           rb.ToggleType = ButtonToggleType.CheckBox;
           rb.Text = "Testcheck RadButton";
           rb.AutoPostBack = false;
     
           CheckBox cb = new CheckBox();
           cb.ID = "Test3456";
           cb.Text = "Testcheck CheckBox";
           cb.AutoPostBack = false;
     
           if (!IsPostBack)
           {
               rb.Checked = true;
               cb.Checked = true;
           }
     
           myDiv.Controls.Add(rb);
           myDiv.Controls.Add(cb);
       }
     
       protected void Unnamed_Click(object sender, EventArgs e)
       {
           foreach (Control ctrl in myDiv.Controls)
           {
               if (ctrl is RadButton)
               {
                   RadButton rb = ctrl as RadButton;
                   lbText.Text += "RadButton " + rb.ID + " : " + rb.Checked.ToString() + "<br/>\r\n";
               }
     
               if (ctrl is CheckBox)
               {
                   CheckBox cb = ctrl as CheckBox;
                   lbText.Text += "CheckBox " + cb.ID + " : " + cb.Checked.ToString() + "<br/>\r\n";
               }
           }
       }

     

  2. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1490 posts

    Posted 11 Jun 2015 Link to this post

    Hello Aljoscha,

    This is an issue with the control. I have logged it here (http://feedback.telerik.com/Project/108/Feedback/Details/160929), so that you can monitor, comment and vote on it.

    For the time being you can use the following JavaScript workaround:
    <script>
        $T = Telerik.Web.UI;
        $T.RadButton.prototype.initialize = function () {
            var that = this;
            $T.RadButton.callBaseMethod(that, "initialize");
     
            //if the button is in RTL parent apply rbRtl CSS class to the holding element
            that._setRightToLeft();
     
            that._createToggleStates();
     
            that._requestIconsAndImages();
     
            if (that.get_checked()) {
                that.clearCheckedRadios(that.get_uniqueGroupName());
            }
            $T.registerRadButton(that);
     
            that._initModules();
     
            that._initHTML();
            that._attachClickEvent(true);
            if (that.get_enabled() && !that.get_readOnly()) {
                that._attachEvents(true);
            }
            that.updateClientState();
            that.raiseEvent('load', Sys.EventArgs.Empty);
        };
    </script>

    As a small token of gratitude for reporting this issue to us I have updated your Telerik points.

    Regards,
    Danail Vasilev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Aljoscha
    Aljoscha avatar
    5 posts
    Member since:
    Mar 2013

    Posted 11 Jun 2015 in reply to Danail Vasilev Link to this post

    Thanks for your help.

    I'll try this approach shortly.

    But as I try to debug my dialog, where this first happened, I found out, that the same problem seems to be there with the RadDropDownList, too. Can you please proof this, or do I have a mistake on my side here?

  5. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1490 posts

    Posted 16 Jun 2015 Link to this post

    Hello Aljosch,

    I am not able to reproduce the mentioned issue with the code below. Can you try to reproduce the unexpected behavior with this snippet and then tell me what changes you have made, so that I can proceed further with the investigation?

    ASPX:
    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <telerik:RadButton ID="RadButton1" runat="server" Text="Postback" />
        <telerik:RadDropDownList ID="RadDropDownList1" runat="server">
            <Items>
                <telerik:DropDownListItem Text="text 1" Value="1" />
                <telerik:DropDownListItem Text="text 2" Value="2" />
            </Items>
        </telerik:RadDropDownList>
    </form>

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RadDropDownList1.Items[1].Selected = true;
        }
    }



    Regards,
    Danail Vasilev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top