Does a RadTabStrip work in a MasterPage? The tabs do not appear selected when clicked.
Steps:
Create a new Web Application Project. By default it contains Home and About tabs inside a Master Page (Site.Master). The tabs are asp:MenuItem controls.
Comment out the asp:Menu control and drag a RadTabStrip in its place. Add two RadTabs. When you run the application each tab works as expected -- it appears to be selected when clicked.
Now add NavigateUrls to each RadTab pointing to the "~Default.aspx" and "~About.aspx" pages. When the application runs the tabs don't appear to be selected when clicked (although the correct page displays).
What's going on? How can I make this work?
Update: Here's how I altered the default code in Site.Master...
<div>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
</div>
<%--<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
</Items>
</asp:Menu>
</div>--%>
<div>
<telerik:RadTabStrip ID="RadTabStrip1" runat="server">
<Tabs>
<telerik:RadTab runat="server" Text="Root RadTab1" NavigateUrl="~/Default.aspx">
</telerik:RadTab>
<telerik:RadTab runat="server" Text="Root RadTab2" NavigateUrl="~/About.aspx" >
</telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
</div>
The answer...
Bojan Skrchevski's answer led me to this. I added this code to the Master Page's Page_Load event and it works:
using Telerik.Web.UI;
namespace WebApplication1
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
RadTab currentTab = RadTabStrip1.FindTabByUrl(Request.Url.PathAndQuery);
if (currentTab != null) currentTab.Selected = true;
}
}
}
I think that you also need to add runat="server" to the RadTab element if you want to use it like that. For example:
<telerik:RadTab Text="Home" NavigateUrl="Default.aspx" runat="server">
</telerik:RadTab>
You can also use ContentUrl in the corresponding telerik:RadPageView to navigate on the client side. Example:
<telerik:RadPageView ID="RadPageView1" runat="server" ContentUrl="Default.aspx">
UPDATE(on your update):
When you add runat="server" to the RadTab then it causes a postback. On postback the control is unable to determine which tab is selected even though it navigates to the specified page. Here's how it is solved in the Telerik example:
protected void Page_Load(object sender, System.EventArgs e)
{
string urlWithSessionID = Response.ApplyAppPathModifier(Request.Url.PathAndQuery);
RadTab tab = RadTabStrip1.FindTabByUrl(urlWithSessionID);
if (tab != null)
{
tab.SelectParents();
tab.PageView.Selected = true;
}
}
The only difference I can see in your code compared to my master page is SelectedIndex="0" on the RadTabStrip and Selected="true" on one of the Rad Tabs:
<telerik:RadTabStrip ID="RadTabStrip1" runat="server" SelectedIndex="0">
<Tabs>
<telerik:RadTab runat="server" Text="Root RadTab1" NavigateUrl="~/Default.aspx" Selected="true">
</telerik:RadTab>
<telerik:RadTab runat="server" Text="Root RadTab2" NavigateUrl="~/About.aspx" >
</telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
Related
ASP newbie here, in my website I need to set a session variable when I click the menu item( not on page load or pre init or init).
How can I achieve this, I have a menu control in my master page which has a sitemap file attached to it?
How to know when a particular menu item is clicked?
<asp:Menu ID="mainMenu" runat="server" DataSourceID="siteMapSource"
StaticDisplayLevels="10" Width="150px">
<StaticSelectedStyle CssClass="menuNodeSelected" />
<LevelMenuItemStyles>
<asp:MenuItemStyle Font-Bold="True" Font-Underline="False" />
</LevelMenuItemStyles>
<StaticMenuItemStyle CssClass="menuNode" />
</asp:Menu>
<asp:SiteMapDataSource ID="siteMapSource" runat="server" ShowStartingNode="False" />
Based on your code and documentation finded on msdn you should have something like this:
On Markup Code (that will result in HTML which will be sent to Client)
<asp:Menu ID="mainMenu" runat="server" DataSourceID="siteMapSource"
StaticDisplayLevels="10" Width="150px"
OnMenuItemClick="NavigationMenu_MenuItemClick">
<StaticSelectedStyle CssClass="menuNodeSelected" />
<LevelMenuItemStyles>
<asp:MenuItemStyle Font-Bold="True" Font-Underline="False" />
</LevelMenuItemStyles>
<StaticMenuItemStyle CssClass="menuNode" />
</asp:Menu>
<asp:SiteMapDataSource ID="siteMapSource" runat="server" ShowStartingNode="False" />
You should set a method to be called on server side OnMenuItemClick, this will rise the event of menu click. That event is (in our case): NavigationMenu_MenuItemClick.
On Code-Behind you can do whatever you want when an menu item is selected.
void NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
{
// Display the text of the menu item selected by the user.
Message.Text = "You selected " + e.Item.Text + ".";
Session["variable"] = e.Item.Text;
}
In e.Item.Text; you find what element has been selected.
Based on: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.menuitemclick(v=vs.110).aspx
The ASP:Menu has an Click event. You can handle this event to set the Session Variable.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.menuitemclick.aspx
Declare it:
<asp:Menu ID="mainMenu" runat="server" onmenuitemclick="NavigationMenu_MenuItemClick" ...
And handle it:
void NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
{
// Display the text of the menu item selected by the user.
Message.Text = "You selected " +
e.Item.Text + ".";
}
i am facing problem with telerik tab control.
i have two tabs which i have created using telerik tab control that is radtabstrip.
now i have one dropdownlist control which is inside radpageview of radmultipage.
now can any one provide me syntax of how to access drop downlist which is inside radpageview of
radmultipage???
i have tried this but facing error:
radpageview1.drodownlist1.item.selecteditem;
You need to use FindControl() like with any other nested control (e.g., a Panel). Here is an example:
protected void Page_Load(object sender, EventArgs e)
{
RadDropDownList rddl = RadPageView2.FindControl("RadDropDownList1") as RadDropDownList;
rddl.Visible = false;
}
assuming your structure looks like this (whether it is generated from the code-behind is irrelevant, you only need to recreate it early enough):
<telerik:RadTabStrip runat="server" ID="RadTabStrip1" MultiPageID="RadMultiPage1">
<Tabs>
<telerik:RadTab Text="first"></telerik:RadTab>
<telerik:RadTab Text="second"></telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
<telerik:RadMultiPage runat="server" ID="RadMultiPage1">
<telerik:RadPageView ID="RadPageView1" runat="server" Width="100%">
PageView
</telerik:RadPageView>
<telerik:RadPageView ID="RadPageView2" runat="server" Width="100%">
<telerik:RadDropDownList runat="server" ID="RadDropDownList1"></telerik:RadDropDownList>
</telerik:RadPageView>
</telerik:RadMultiPage>
What i'm trying to do is place a specific chart that is dynamically created on a specific tab. For example Chart A will go to ColorTab and Chart B will go to HistoryTab
<telerik:RadAjaxPanel ID="StripPanel" runat="server" Width="100%">
<telerik:RadTabStrip ID="TabStrip" runat="server" MultiPageID="Monetary" SelectedIndex="0">
<Tabs>
<telerik:RadTab Text="MonetaryTab">
</telerik:RadTab>
<telerik:RadTab Text="VTab">
</telerik:RadTab>
<telerik:RadTab Text="ColorTab">
</telerik:RadTab>
<telerik:RadTab Text="HistoryTab">
</telerik:RadTab>
<telerik:RadTab Text="AdTab">
</telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
</telerik:RadAjaxPanel>
This is what i'm trying
Control masterC = FindControl("MonetaryTab");
RadHtmlChart CarLotChart = ChartCreationClass.HTMLChartCreation();
masterC.Controls.Add(CarLotChart);
It never finds the control to place the chart.
Use recommendations from this thread to accomplish this task:
RadHtmlChart CarLotChart = ChartCreationClass.HTMLChartCreation();
TabStrip.Tabs["MonetaryTab"].Controls.Add(CarLotChart);
Refer to the required Tab via any of the following methods.
The RadTabStrip works in conjuction with RadmultiPage control. You first define the RafTabStrip and Tabs within it. Then have a RadMultiPage and define one page (type RadPageView) for each tab you have defined in TabStrip. The controls need to be added to the PageView and not to the tab directly.
I have created a small demo based on the TabStrip you have defined in the question.
Here is the TabStrip code:
<telerik:RadTabStrip ID="TabStrip" runat="server" MultiPageID="Monetary" SelectedIndex="0" >
<Tabs>
<telerik:RadTab Text="MonetaryTab">
</telerik:RadTab>
<telerik:RadTab Text="VTab">
</telerik:RadTab>
<telerik:RadTab Text="ColorTab">
</telerik:RadTab>
<telerik:RadTab Text="HistoryTab">
</telerik:RadTab>
<telerik:RadTab Text="AdTab">
</telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
Now here is the RadMultiPage definition:
<telerik:RadMultiPage ID="Monetary" runat="server" SelectedIndex="0" OnLoad="Monetary_Load">
<telerik:RadPageView runat="server" ID="monetaryTabPageView"/>
<telerik:RadPageView runat="server" ID="vTabPageView"/>
<telerik:RadPageView runat="server" ID="colorTabPageView"/>
<telerik:RadPageView runat="server" ID="historyTabPageView"/>
<telerik:RadPageView runat="server" ID="adTabPageView"/>
</telerik:RadMultiPage>
Notice that i am listening to OnLoad event of the RadMultiPage. In this demo - when the RadMultiPage loads, i will create charts and add it to eacg pageview we have. Here is the event handler code:
protected void Monetary_Load(object sender, EventArgs e)
{
AddChart(monetaryTabPageView);
AddChart(vTabPageView);
AddChart(colorTabPageView);
AddChart(historyTabPageView);
AddChart(adTabPageView);
}
private void AddChart(RadPageView pageView)
{
RadHtmlChart chart = new RadHtmlChart();
chart.ChartTitle.Text = pageView.ID + " chart";
pageView.Controls.Add(chart);
}
As you can see i just add the control directly to the pageview's controls collection.
NOTE: as a proof of concept i have created an empty chart i.e. a chart which has only title defined. you can go ahead and bind with some meaningful data if required.
here is the screenshot of how this looks:
Hope this answers your question.
Lohith (Tech Evangelist, Telerik India)
I have a menu item that when clicked is calling a C# codebehind function.
The function doesn't get called and the whole page get refreshed.
In aspx:
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" onmenuitemclick="NavigationMenu_MenuItemClick">
<Items>
<asp:MenuItem Text="Item caption" Value="#" />
</Items>
</asp:Menu>
In C#:
protected void NavigationMenu_MenuItemClick(object sender, MenuEventArgs e)
{
Response.Redirect("");
SomeFunction();
}
If I'm removing the Redirect call, the page get refresh and looks like a mess.
Any advice will be welcome.
To get a better answer, you many want to edit your question to better explain what you are trying to accomplish.
The Menu control without the NavigateUrl attribute emits HTML that includes a list (of your menu items) with hyperlinks that perform a postback via some Javascript.
<ul class="level1">
<li>
<a class="level1selected" href="#"
onclick="__doPostBack('ctl00$MainContent$NavigationMenu','#')">
Item caption</a>
</li>
</ul>
When handling the menu click event, if your intent is to redirect the user to another page I would recommend simply setting the NavigateUrl attribute on your menu items instead of using Response.Redirect().
<asp:MenuItem Text="Item caption" Value="#" NavigateUrl="~/TargetPage.aspx" />
If you set the NavigateUrl attribute, you'll notice the onclick event doesn't even get added to the anchor tags that make up the menu item's HTML.
<ul class="level1">
<li>
<a class="level1" href="TargetPage.aspx">Item caption</a>
</li>
</ul>
If you're not intending to use the menu for navigation, but rather to perform some logic and update the page when the menu item is clicked, you may consider putting your menu in an UpdatePanel and set up the menu to trigger an async postback.
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="NavigationMenu" />
</Triggers>
<ContentTemplate>
<asp:Label ID="label1" runat="server"></asp:Label>
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="true" IncludeStyleBlock="false" Orientation="Horizontal" onmenuitemclick="NavigationMenu_MenuItemClick">
<Items>
<asp:MenuItem Text="Item caption" Value="#" />
</Items>
</asp:Menu>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind:
protected void NavigationMenu_MenuItemClick(object sender, MenuEventArgs e)
{
SomeFunction();
}
private void SomeFunction()
{
//Performed some logic
label1.Text = "Altered the page on menu item click";
}
Code
<telerik:RadTabStrip ID="RadTabStrip1" runat="server" MultiPageID="RadMultiPage1" OnTabClick="RadTabScript1_TabClick" OnClientTabSelecting="onTabSelecting" > <%-- OnClientTabSelecting="onTabSelecting"> --%>
<Tabs>
<telerik:RadTab Text="Resources" runat="server" TabIndex="0" Selected="true" PageViewID="radPageResources"></telerik:RadTab>
<telerik:RadTab Text="Tasks" runat="server" TabIndex="1" PageViewID="radPageTasks"></telerik:RadTab>
<telerik:RadTab Text="Messages" runat="server" TabIndex="2" PageViewID="radPageMessages"></telerik:RadTab>
<telerik:RadTab Text="Files" runat="server" TabIndex="3" PageViewID="radPageFiles"></telerik:RadTab>
<telerik:RadTab Text="Feedback" runat="server" TabIndex="4" PageViewID="radPageFeedback"></telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
<telerik:RadMultiPage ID="RadMultiPage1" runat="server" OnPageViewCreated="RadMultiPage1_PageViewCreated" SelectedIndex="0" > <%-- RenderSelectedPageOnly="true" > --%>
<telerik:RadPageView ID="radPageResources" runat="server" TabIndex="0">
<telerik:RadGrid runat="server" ID="grdResource" PageSize="5" Skin="Default" AllowPaging="True"
AllowSorting="true" ShowFooter="true" AutoGenerateColumns="False" Width="97%"
OnItemCreated="grdResource_ItemCreated" OnNeedDataSource="grdResource_NeedDataSource"
OnDeleteCommand="grdResource_DeleteCommand" ShowStatusBar="True" GridLines="None"
OnItemDataBound="grdResource_ItemDataBound">
</telerik:RadGrid>
Like this i have 4 pageview tabs with indexes and every pageview tab contains a grid.If suppose i did changes in some x-grid and causes postback,then i need the x-grid should be shown after the postback. But i was showing the Resourcegrid.
codebehind
protected void RadMultiPage1_PageViewCreated(object sender, RadMultiPageEventArgs e)
{
RadMultiPage1.SelectedIndex = e.PageView.TabIndex;
}
but this event not firing in any case.
so how can i come back to the previous grid even when postback occurs?
try set selected index in page_prerender.
protected void Page_PreRender(object sender, EventArgs e)
{
//RadTab t1 = new RadTab();
//RadMultiPage m1 = new RadMultiPage();
m1.SelectedIndex = t1.SelectedIndex;
}
i don't know my solution can be helpful for this subject or not
I tried all methods and finally find myself method,
i suggest if your problem not solved make java script function and then call it from code behind
My Sample :
function OnClientClick1(i) {
var tabstrip1 = $find("<%=RadTabStrip1.ClientID %>");
tabstrip1.set_selectedIndex(1);
}
and then make function on your code to call that
private void ChangeTab(int index)
{
string scriptyKey = "Alert" + Guid.NewGuid().ToString().Replace("-", "_");
ScriptManager.RegisterStartupScript(Page, Page.GetType(), scriptyKey, "OnClientClick1("+index.ToString()+");", true);
}
I hope be useful