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";
}
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 have button, which should open a modal pop up with iFrame to a page.
Currently the button click opens a PostBackUrl,
I want a similar thing to happen here.
That is, I want to open the Iframe scr Page as postbackurl.
<asp:Button ID="btnCreateComp" runat="server" Text="Create Company" CssClass="button_style"
PostBackUrl="~/Company.aspx" />
<asp:Panel ID="Pnl1" runat="server" CssClass="PanelPopup">
<div>
<iframe id="iframe1" runat="server" height="500px" width="500px" src="" ></iframe>
<asp:Button ID="btnclose" runat="server" Text="Close" CausesValidation="false" />
</div>
</asp:Panel>
I think you should use jquery pop up because it is easier than modalpopupextender because I found it buggy some time there are a lot of pop up in jquery:
Jquery popup exmaples
I used this one and it is working perfectly (you can place what ever you want) for me:
Note:The best feature I like about Jquery it is not required that your control must have runat="server" it works with both HTML Controls and ASP.NET Controls.
Adding event handler to button:
<asp:Button ID="btnclose" runat="server" Text="Close" CausesValidation="false" onclick="btnClicked" />
In the code behind:
protected void btnClicked(object sender, EventArgs e)
{
iframe1.src="an http link";
//do not try set src to www.google.com because they are blocking Iframe
}
I have a repeater and it has a column of linkbuttons in it. I want to add those linkbuttons to targetcontrolid but it failed because they are in the repeater. So i create an additional invisible button like this :
<asp:Button ID="btnFakePopUp" runat="server" Text="" visible="false"
onclick="btnFakePopUp_Click"/>
And in i tried to link the linkbutton to the invisible button in this code :
protected void lbtnPosition_Click(object sender, EventArgs e) {
btnFakePopUp_Click(sender, e);
}
protected void btnFakePopUp_Click(object sender, EventArgs e)
{
popupJob.Show();
}
And this is my modalpopupextender code (my prefix is asp: so dont get confuse) :
<asp:ModalPopupExtender ID="popupJob" runat="server" PopupControlID="panelPopup" CancelControlID="popupClose" TargetControlID="btnFakePopUp"
Drag="true" PopupDragHandleControlID="panelPopup">
</asp:ModalPopupExtender>
<asp:Panel ID="panelPopup" runat="server" BackColor="#ebf0ff" Width="300px">
<div>
test<br />
<asp:Button ID="btnSave" runat="server" Text="Save" />
<asp:Button ID="btnApply" runat="server" Text="Apply" />
<input id="popupClose" type="button" value="Close" />
</div>
</asp:Panel>
The problems are :
1. The panelpopup is always shown...(it should be hidden, and only be shown when the user click the link button)
2. Nothing happened when i tried to click the link button (the panelpopup should be shown)
Thank you :D
For a btnFakePopup invisible you could set the display:none with CSS
example:
<asp:ImageButton ID="btnFakePopUp" runat="server" style="display: none"></asp:ImageButton>
I don't understand why, but setting btnFakePopUp visibility to true corrected the problem. Now my modalpopupextender is running smoothly.
I have a asp.net dropdown control to which tool tip has been added. It works fine on the mouse over. I also have a Modal Popup in that page. When I open and close the Modal Popup the tool tip in the drop are no more shown.
Below is my code.
<div class="col">
<div class="labelname required">
<asp:Label ID="lblsupplier" runat="server" Text="Supplier"></asp:Label>
</div>
<div class="labelvalue">
<asp:DropDownList ID="DdlSupplier" TabIndex="11" runat="server" AutoPostBack="true"
CausesValidation="false" OnSelectedIndexChanged="DdlSupplier_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqSupplier" runat="server" ControlToValidate="DdlSupplier"
ValidationGroup="SubmitGroupMain" InitialValue="0" ErrorMessage="Please Select Supplier Name"
SetFocusOnError="true" Display="None"></asp:RequiredFieldValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender7" runat="Server"
TargetControlID="reqSupplier" HighlightCssClass="validatorCalloutHighlight">
</ajaxToolkit:ValidatorCalloutExtender>
</div>
<asp:LinkButton runat="server" ID="lnkAddSupplier" CssClass="btnadd" ToolTip="New Supplier"
OnClick="lnkAddSupplier_Click"></asp:LinkButton>
<asp:Button runat="server" ID="popupshowAccount" Style="display: none" />
</div>
</div>
Code behind
foreach (ListItem item in DdlSupplier.Items)
{
item.Attributes.Add("title", item.Text);
}
DdlSupplier.Attributes.Add("onmouseover","this.title=this.options[this.selectedIndex].title");
Clicking on lnkAddSupplier opens up a popup. After closing the popup tool tip is not visible for the dropdown.
Please help on solving this issue.
Attributes won't be saved in Viewstate and that's why they will disappear across postbacks. Similar is the case with Custom Attributes.
That's why we have to use DropDownList Pre Render event to set attributes
protected void DropDownList1_PreRender(object sender, EventArgs e)
{
foreach (ListItem item in DropDownList1.Items)
item.Attributes.Add("title", "Something to test!!!!!!");
}
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>