Is it possible to make a menu item behave like a button? By this I mean that instead of setting the NavigateUrl property to a new page, assign a method when the item is clicked?
You can add a click handler for the menu.
The MenuItemClick tells you which item was clicked.
<asp:menu id="NavigationMenu"
onmenuitemclick="NavigationMenu_MenuItemClick"
runat="server">
You could take action depending upon which item was clicked by the user.
void NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
{
if (e.Item.Text == "MyItem")
{
//do some processing
}
}
Ofcourse you can do it..you can call a server side function using__dopostback() for example
on your menu item's click you can call this Javascipt function
<script type="text/javascript">
function SaveWithParameter(parameter)
{
__doPostBack('btnSave ', parameter)
}
</script>
Try the below:
<asp:menuitem navigateurl="javascript:YourJScript()"
If you need to execute a method in serverside, use a hidden variable, then set a flag in javascript method and submit the page.
In serverside, check the flag in page load and call the method as required.
Thanks
Related
Is there a way to make a button click event run from the page load?
Yes, you can do that.
if your button has runat="server" you can access it from codebehind.
you could insert a click action on the button in the page_load event. but it's better to create a function with all actions to call both from button click and page_load.
<asp:Button id="btn1">Click here</asp:Button>
void Page_Load(object source, EventArgs e)
{
doThis();
}
void btn1_click()
{
doThis();
}
void doThis()
{
//click actions
}
Another way to do it is by javascript. find the button when document is ready and fire the click on the button.
You could wrap the logic behind the button click event in a function and call that function from the page load.
I'm using Div's, now I have to handle 2 Events with one Div-Click..
On the Button I would take, once onclick="" and OnClientClick.. but if I use a Div I can't use OnClientClick :(
Can you guys tell me, what I shall do, to get this? (there is one JavaScript method and one codebehind method, which I want to call) - till now I can call only one.
after wizards help:
got this:
<asp:Button ID="fakeButton" runat="server" Text="dummy" onclick="dummyButton_Click" style="display:none" />
and JS:
alert("div clicked");
document.getElementById("<%=fakeButton.ClientID%>").click();
So but he dont enters the Method:
protected void dummyButton_Click(object sender, EventArgs e) { } ..
i putted a breakpoint, he never entered, only showed the alert.. –
First, add dummy hidden button with the proper server side click event:
<asp:Button id="fakeButton" runat="server" OnClick="MyMethod" Text="dummy" style="display: none;" />
Second, have such code for the DIV:
<div onclick="DivClick();">text here...</div>
And finally such client side code:
function DivClick() {
//your client side code here...
//e.g.
alert("div clicked");
//now invoke server side click as well:
document.getElementById("<%=fakeButton.ClientID%>").click();
}
This will first execute your client side code e.g. the alert, then "auto click" the button and by this causing post back and the server side code to execute.
If you don't want full reload, try putting it all inside UpdatePanel.
I'm using nyroModal plugin. By simply putting a setup scripts in the head tag and define a class attribute with nyroModal will make that link open as a modal window.
Show
Now, Page1.aspx will open in a modal window. This page has a simple asp:button having a OnClick defined with empty body:
<asp:button ID="btn" runat="server" OnClick="btn_Click"/>
void btn_Click(Object sender, EventArgs e) { }
If we click on the above link a window appears with a button and when I click on that button modal windows disappears (or break) and redirected to Page1.aspx.
Any idea, why is this happening?
Try adding the following:
jQuery(function ($) { jQuery('.nyroModal').nyroModal({ 'blocker': '#Form' }); });
replace #Form with aspForm or whatever your form name is.
I have a lot of data to display in a GridView. Because there's so much information per row, I'd like to be able to display additional information when a user clicks on the row, so I thought a PopupExtender from the AJAX Toolkit would be perfect.
Ideally, I want the popup to display whenever any of the controls within the row are selected. I've been able to successfully attach the PopupExtender to a single control within the row, but I can't get the pop-up to attach to the row itself.
I would have thought that setting the PopupExtender's TargetControlId to the Row's ClientID within the RowDataBound event would work, but when I do this I get a runtime error:
TargetControlID of 'popupExtId' is not valid.
A control with ID 'gvList_ctl02' could not be found.
I noticed that the GridViewRow is rendered, the tr element does not include an id, so I also tried extending the GridView control to override the CreateRow method to render the id - using this method I was able to render the row's ID (e.g. gvList_ctl02), but the same runtime error was thrown when I added the PopupExtender back into the code.
I also tried binding the showPopup() javascript command to the row's onclick event to get the popup to display manually; whilst the click event is registered OK and is definitely triggered, the popup is still not shown.
Does anyone have any idea how to / if you can bind a PopupExtender to a GridViewRow?
My row bound code is as follows:
protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Bind the popup extender's target ID to the row ID
// This will cause a runtime error
PopupControlExtender pop = e.Row.FindControl("popupExtId") as PopupControlExtender;
pop.TargetControlID = e.Row.ClientID;
// Also bind the client side click handler to try to get the popup to show
// The alert is triggered and no javascript error is generated, but the popup does not display
e.Row.Attributes.Add("onclick", "alert('Row Clicked'); $find('" + pop.BehaviorID + "').showPopup();");
}
}
Many thanks.
If you're not opposed to using an ajax ModalPopupExtender, I use a little bit of javascript and some sneaky hidden button clicks to fire off my modal popups from within a grid view. I usually make my modal popup extender's target control id my hidden button, then, via javascript, fire my hidden button's click event to show the modal popup.
Here's my modal popup and hidden button markup.
<asp:Button ID="hiddenButton" runat="server" Text="" style="display:none"></asp:Button>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender2" runat="server"
TargetControlID="hiddenButton" PopupControlID="Panel1" CancelControlID="CancelButton"
BackgroundCssClass="modalBackground" Drag="True"/>
Here's my javascript to show my popup.
function showModal(btnID) {
btn = document.getElementById(btnID);
btn.click();
}
In my rowdatabound event, I call the javascript function showModal from button's onclick event.
Button myButton = (Button)e.Row.Cells[9].Controls[1];
matchButton.Attributes.Add("onclick", "showModal('" + hiddenButton.ClientID + "');");
Hope this might help point you in the right direction.
I have a link on the page and i want when the user is pressing it, to evaluate a method where i check if the usser is logged in. If not, i want to show up a modal popup.
I know how to do the back-end checking and how to redirect the user but i have problems
with the link button:
<asp:LinkButton ID="LinkButton1" runat="server"
OnClientClick="redirectToWishList()">LinkButton</asp:LinkButton>
When i press it, it does a full postback instead evaluating the method where i show up the modal pop.
Do you have any workaround to not fire a full postback but check the method where i am doing the redirect?
Ps: The method which has to fire:
public void redirectToWishList(object sender, EventArgs e)
{
ASP.usercontrols_loginpopup_ascx loginUserControl = (ASP.usercontrols_loginpopup_ascx)UtilsStatic.FindControlRecursive(Page, "loginPopUp");
ModalPopupExtender modal = (ModalPopupExtender)loginUserControl.FindControl("loginPopUp");
modal.Show();
}
You need to call redirectToWishList as a function (including the parens): redirectToWishList()
<asp:LinkButton ID="LinkButton1" runat="server"
OnClientClick="redirectToWishList();return false;">LinkButton</asp:LinkButton>
Also, if you return false in your function, you do not need to in the tag.
UPDATE
I misunderstood. The OnClientClick is for invoking JavaScript, not running methods in your code-behind. You need to OnClick instead.
Do you return false from your redirectToWishList function?
You shouldn't need the return false in the LinkButton declaration.