Button does not stay checked inside itemcommand

6 posts, 0 answers
  1. Paulo
    Paulo avatar
    30 posts
    Member since:
    Mar 2013

    Posted 23 May 2014 Link to this post

    I have buttons on ItemCommand that fuction correctly. However when pressed it should be marked as checked and keep like that until user clear all filters or press another button (see image 1).
    The aspx code is in image 2.
    Is there any trick to solve this problem. I tried to access the button from code behind inside the ItemCommand but the checked didn't work.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 May 2014 in reply to Paulo Link to this post

    Hi Paulo,

    In order to achieve your scenario please try to change the ButtonType of the RadButton to the ToggleButton as follows.

    ASPX:
    ...
    <
    CommandItemTemplate>
        <telerik:RadButton ID="RadButton1" runat="server" ToggleType="Radio" ButtonType="ToggleButton"  GroupName="Radios" Text="Clear All Filters">
        </telerik:RadButton>
        <telerik:RadButton ID="RadButton2" runat="server" ToggleType="Radio" ButtonType="ToggleButton" GroupName="Radios" Text="Last 2 weeks">
        </telerik:RadButton>
        <telerik:RadButton ID="RadButton3" runat="server" ToggleType="Radio" ButtonType="ToggleButton" GroupName="Radios" Text="Last 30 days">
        </telerik:RadButton>
        <telerik:RadButton ID="RadButton4" runat="server" ToggleType="Radio" ButtonType="ToggleButton" GroupName="Radios" Text="Last 90 days">
        </telerik:RadButton>
    </CommandItemTemplate>
    ...

    Let me know if you have any concern.
    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Paulo
    Paulo avatar
    30 posts
    Member since:
    Mar 2013

    Posted 26 May 2014 Link to this post

    Shinu

    thanks for your message but I have tried all types of buttons. When the grid rebinds the buttons go to original states. Also I thought that keeping it checked it would fire the item command in case of any post back but it doesn't. I might need to change the page design. The description of the Item Command functionality should be more clear about what kind of operations it can handle
     to avoid people to spend lot of time writing code that won't work. That is just my opinion. 
    Thank for your help Shinu I really appreciate. If you have any thoughts please let me know. I still have couple of days to have this done.
  5. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1494 posts

    Posted 27 May 2014 Link to this post

    Hello Paulo,

    I have already replied to the same ticket that was opened by you on the matter, so that I paste my answer here, in order for the rest of the community to benefit of it.

    The behavior you are experiencing is expected because after executing particular command in the grid, the controls inside the command item template are recreated, so that their ViewState is not persisted. In order to simulate such a persistence you can execute JavaScript code from the server that checks the clicked button. For example:
    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand">
        <MasterTableView CommandItemDisplay="Top">
            <CommandItemTemplate>
                <telerik:RadButton ID="RadButton1" runat="server" ToggleType="Radio" ButtonType="LinkButton"
                    GroupName="Radios" AutoPostBack="true" Text="Clear All Filters" CommandName="Clear_All" />
                <telerik:RadButton ID="RadButton2" runat="server" ToggleType="Radio" ButtonType="LinkButton"
                    GroupName="Radios" AutoPostBack="true" Text="Last 2 weeks" CommandName="Last_2_weeks" />
                <telerik:RadButton ID="RadButton3" runat="server" ToggleType="Radio" ButtonType="LinkButton"
                    GroupName="Radios" AutoPostBack="true" Text="Last 30 Days" CommandName="Last_30_Days" />
                <telerik:RadButton ID="RadButton4" runat="server" ToggleType="Radio" ButtonType="LinkButton"
                    GroupName="Radios" AutoPostBack="true" Text="Last 90 Days" CommandName="Last_90_Days" />
                <telerik:RadButton ID="RadButton5" runat="server" ToggleType="Radio" ButtonType="LinkButton"
                    GroupName="Radios" AutoPostBack="true" Text="Last 12 Months" CommandName="Last_12_Months" />
                <asp:TextBox ID="Textbox1" runat="server" />
            </CommandItemTemplate>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid1.DataSource = new int[] { 1, 2, 3 };
        RadGrid1.DataBind();
    }
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        RadButton targetRadButton = new RadButton();
        GridItem cmdItem = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
        switch (e.CommandName)
        {
            case "Clear_All":
                targetRadButton = cmdItem.FindControl("RadButton1") as RadButton;
                break;
            case "Last_2_weeks":
                targetRadButton = cmdItem.FindControl("RadButton2") as RadButton;
                break;
            case "Last_30_Days":
                targetRadButton = cmdItem.FindControl("RadButton3") as RadButton;
                break;
            case "Last_90_Days":
                targetRadButton = cmdItem.FindControl("RadButton4") as RadButton;
                break;
            case "Last_12_Months":
                targetRadButton = cmdItem.FindControl("RadButton5") as RadButton;
                break;
        }
      
        string script = "function f(){$find(\"" + targetRadButton.ClientID + "\").set_checked(true); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "key", script, true);
    }



    Regards,
    Danail Vasilev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Paulo
    Paulo avatar
    30 posts
    Member since:
    Mar 2013

    Posted 27 May 2014 in reply to Danail Vasilev Link to this post

    Danail

    thank you for your response, but for some reason it didn't work in my project. However I used a different approche. I active what I wanted wich was having the button pressed and calling the item command event handler I added a onclientselecteditemchanged function which fires the item command and when the grid_prerender happen I identify the button and set it to checked. It is a lot of code but it works. Your solution is simple which I like but for some reason didn't work when I added to my project neither when I just reproduced your code in a new project. Let me know if there is anything missing in your solution.
    Thank you very much.
  7. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1494 posts

    Posted 28 May 2014 Link to this post

    Hello Paulo,

    I have tried to reproduce the mentioned issue but to no avail - the RadButton inside the RadGrid is properly checked after clicking it. You can watch the short video test in the attached archive and then tell me what I am missing.

    If, however, the issue is only reproducible when the used logic is placed in your project it would be better to isolate the issue in a small runnable VS example and then send it back to us, so that we can make an investigation locally.

    Thank you for your cooperation.

    Regards,
    Danail Vasilev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017