Get selected Tab server-side

5 posts, 0 answers
  1. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 01 Apr 2011 Link to this post

    Hi

    I was quite surprised when using the RadTabStrip1.SelectedTab in a button click event in server side code to find that the value was null. I guess it makes sense as whatever tab a user clicks is all handled client side.
    I decided to use the following code to find the selected tab server side. I've set the first tab selected by default.

    Please can you tell me is this the correct way - or is there something easier?


    <telerik:RadTabStrip ID="RadTabStrip1" runat="server" MultiPageID="RadMultiPage1" OnTabClick="RadTabStrip1_TabClick">
       <Tabs>
          <telerik:RadTab Text="Tab1" Selected="true"/>
          <telerik:RadTab Text="Tab2" />
          <telerik:RadTab Text="Tab2" />
       </Tabs>
    </telerik:RadTabStrip>
     
    <telerik:RadMultiPage ID="RadMultiPage1" runat="server">
       <telerik:RadPageView ID="RadPageView1" runat="server" Width="100%" Selected="true">xxx</telerik:RadPageView>
       <telerik:RadPageView ID="RadPageView2" runat="server" Width="100%">yyy</telerik:RadPageView>
       <telerik:RadPageView ID="RadPageView3" runat="server" Width="100%">zzz</telerik:RadPageView>
    </telerik:RadMultiPage>

    <asp:Button ID="btnOk" runat="server" Text="OK" onclick="btnOk_Click" />


    The server-side code:
    public string CurrentPageViewID
    {
      get
      {
        if (ViewState["CurrentPageViewID"] != null)
          return (string)ViewState["CurrentPageViewID"];
        else
          return "RadPageView1";
      }
      set
      {
        ViewState["CurrentPageViewID"] = value;
      }
    }
     
    protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
    {
      //update current tab
      CurrentPageViewID = e.Tab.PageView.ID;
    }
     
    protected void btnOk_Click(object sender, EventArgs e)
    {
      Response.Write("Current Tab PageView ID is " + CurrentPageViewID);
    }
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 01 Apr 2011 Link to this post

    Hello Jeremy,

    Try the following code to access the selected Tab in the Button click event.
    C#:
    protected void btnOk_Click(object sender, EventArgs e)
      {
          RadTab rdTab =new RadTab();
          rdTab = RadTabStrip1.SelectedTab;
          Response.Write(RadTabStrip1.SelectedTab.Text);
      }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 03 Apr 2011 Link to this post

    Thanks Shinu!

    I see now I was trying to us the tab as a text value rather than the SelectedTab.Text value.
  5. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 04 Apr 2011 Link to this post

    Hmmm after more testing actually it is still null if I use it from any other tab than "Tab1" which is selected by default?!?
  6. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 05 Apr 2011 Link to this post

    An additional problem was that the selected tab was being lost on postback. I am now using my original code plus the extra below to make sure it stays selected

    protected void Page_Load(object sender, EventArgs e)
    {
       if (Page.IsPostBack)
       {
          RadTab rt = RadTabStrip1.FindTabByText(MyCurrentTabText); //global variable
          rt.SelectParents();
          rt.PageView.Selected = true;
       }
    }
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017