href link to trigger asp.net c# button event - javascript

I have a navigation bar consisting of list items. Right at the end of the nav bar I want a logout link:
//other links
...
<li>
logout
</li>
I have an event in my C# code called Logout_Click(object sender, EventArgs e) that I want to run when the user clicks the above navbar link, but obviously ordinary <a> tags don't trigger ASP.NET click events.
So I thought I'd be clever and create a HIDDEN <asp:Button> called logout that does trigger the Logout_Click function, and then get the JavaScript to click the button for me.. here is my code:
Log Out
<form runat="server">
<asp:Button ID="logout" runat="server" onclick="Logout_Click" Visible="false" />
</form>
But it still didn't work.
Can someone help me?

Try:
Log Out

Can you try without the Visible="false" on the asp:button?
And if it works hide it with css instead, style="display:none;"

I think the script needs the client side id of your asp.net control:
javascript:document.getElementById('<%=logout.ClientID').click()

Or you can use the __doPostBack function ...
http://wiki.asp.net/page.aspx/1082/dopostback-function/enter link description here

Why not you are using Asp.net LinkButton ?
It's easy to do this work with LinkButton.

Related

<asp:Button> event not firing when within element affected by jQuery

I am creating an ASP.NET with C# calendar application. When a user clicks on a day, I am using a jQuery Popup Overlay which will allow the user to add an 'appointment' and click a button to process the entered information in the codebehind.
The Popup overlay basically hides an HTML element and makes it visible when another element is clicked (great examples by following link above).
The Code:
<asp:Button ID="ButtonA" runat="server" OnClick="Button_Click" Text="Button A" />
<asp:Label ID="Label" runat="server" Text="Foo"></asp:Label>
<div id='slide'>
<div id="content">
Content of the 'popup panel'
<asp:Button ID="ButtonB" runat="server" OnClick="Button_Click" Text="Button B" />
</div>
</div>
CodeBehind:
public void Button_Click(Object sender, EventArgs e)
{
Label.Text = "Bar";
}
Javascript:
$(document).ready(function () {
$('#slide').popup({
//options
});
});
I tried adding the AutoPostBack="true" parameter to the buttons, but it doesn't make a difference (should be automatic regardless, correct?). I also tried adding the runat="server" parameter to the div elements, but that made the issue worse.
I am afraid that I don't understand the nature of the issue enough to provide more information than this - Sorry!
It's likely that the jQuery plugin is overriding the behaviour of the button. You could use the onclose callback from the URL you provided to have a function like so:
function() {
__doPostBack("ctl00$MainContent$ButtonB","");
}
Failing that, you could have another button outside the popup div which you do a click() event on manually during the onclose event of the popup, but that seems like a bit of a hack!

how to open an aspx page as popup on button click

I have created an aspx page "Analyse.aspx", I want to pass an argument to this page when calling it on a button click of another page say 'Master.aspx' by-
OnClientClick="javascript:window.open('Analyse.aspx?Param=');"
how to do it?
"Could not load type" can happen due to various reasons and not because of popup. Try to call Analyse.aspx directly in your browser and make sure it works. To troubleshoot read Parser Error Message: Could not load type 'webmarketing'
UPDATE
Your code is not well formed.
Instead of
OnClick="javascript:window.open(Analyse.aspx?Param=""');"
must be
OnClick="javascript:window.open('Analyse.aspx?Param=');"
UPDATE #2
To wire a js code to js onclick event you should use the OnClientClick() event
<asp:Button ID="btnDeDupCheck" runat="server"
OnClientClick="javascript:window.open('PieChart.aspx?Param=');"
Text="De-Duplication Check" Visible="False" />
The OnClick() event is used to run a .net code.
UPDATE #3
To add values from asp.net controls you need to inject asp.net code. Read How to pass a value into a javascript function within an aspx page onClientClick or OnClientClick Javascript:confirm get value in textbox?
<asp:Button ID="btnDeDupCheck" runat="server"
OnClientClick="javascript:window.open('PieChart.aspx?Param=<%=txt1.ClientID%>');"
Text="De-Duplication Check" Visible="False" />
I finally got the it,successful executed code-
OnClientClick="javascript:window.open('Analyse.aspx?Param=');"
but my new query is how to pass parameter from current page to this new page 'Analyse.aspx',
say my parameter is - "txtPwd.text"
and how to catch the argument on pageload of Analyse.aspx

Calling c# from JS via button

So what I want to do is click on a button which will cause JS code to run which will cause another button to click and button #2 will be runat="server" and cause a C# function to run onClick.
But using the below does not cause the C# function to run even though simply clicking the button2 does cause it to run.
function button1_click() {
document.getElementById('button2').click();
__doPostBack('#button2', 'onclick');
$('#button2').trigger('click');
// neither worked (tried them seperately)
}
<asp:Button runat="server" Visible="true" text="you should not see this" ClientIDMode="Static" ID="button2" onclick="button2_Click" formnovalidate />
Useful knowledge:
- button one is in a client form
- button two is in a runat="server" form
This code
$('#button2').trigger('click');
should work.
Have you checked that the ID 'button2' is not changed during runtime?
Sometimes, when using master pages or user controls, the client ID of an element with runat="server" attribute will get a prefix at run time.
Use a debug console (depends on your browser, try pressing F12) to check the effective client ID.
Also, have you checked that button1_click() function is actually called?

Unable to make ASP.NET control button fade out with JQuery

I have a simple enough problem: I have an ASP.NET control button and I want to make it fade out and then call some function (such as an alert) using JQuery. Here is what I have so far:
ASP Code for the Button:
<div id="begin">
<span id="startButtonSpan">
<asp:Button ID="startButton" class="startButton" runat="server" Text="Click Me!" OnClientClick="startButtonClick()"/>
</span>
</div>
JavaScript:
function startButtonClick()
{
$("#startButtonSpan > input").fadeOut(500, callAlert());
}
function callAlert()
{
alert("Made it here...");
}
When I click the button, the alert displays but the page does not even seem to try to perform the fadeOut. When I close the alert, the button is still there, staring at me.
Can anyone see any mistakes or does anyone have any suggestions on how I might be able to achieve the intended goal of fading out my button? Fadeout is really just my way of testing whether I can manipulate ASP controls using jQuery, so more than just the simple fadeOut, this is me trying to learn how to do that.
I tried a slightly more simple jQuery call using the code below, but it does not seem to work either:
ASP Portion:
<div id="begin">
<span id="startButtonSpan">
<asp:Button ID="startButton" class="startButton" runat="server" Text="Click Me!" OnClientClick="startButtonClick()"/>
</span>
</div>
<div id="jQueryTest" style="display:none;">
Block for testing jQuery.
<h1 id="testMessage">Child element for the ASP div.</h1>
</div>
Javascript Portion:
function startButtonClick()
{
$("#jQueryTest").css("display", "block");
$("#jQueryTest").show();
}
For this example, the text does display, but it immediately disappears again.
Any help or suggestions as to what I might be doing wrong would be greatly appreciated.
Use the class as a selector $('.startButton') instead of the ID since ASP.Net controls change their IDs dynamically when rendered by appending its Page & Control information.
$(".startButton").fadeOut(500, callAlert);
Or, if you're adamant about using the ID, here is another way to handling the selector,
$("#<%=startButton.ClientID %>")
Or, as Jacob suggested in his answer, you could ClientIDMode="Static", but this works only if your application is .Net 4.0 or above.
Also, use CssClass instead of class
<asp:Button ID="startButton" Csslass="startButton" runat="server" Text="Click Me!" />
The first example has 2 problems.
1. You should write
$("#startButton").fadeOut(500, callAlert);
and not
$("#startButton").fadeOut(500, callAlert());
2. For ASP.NET you must set ClientIDMode="Static" ortherwise asp.net will alter your id.
<asp:Button ID="startButton" ClientIDMode="Static" ... OnClientClick="startButtonClick()"/>
How about the fact that your code is fine (although other answers here should be considered) but your button is making a post back to the server and simply your browser does not have enough time to render the fade effect.
To test this, add a return false; to the OnClientClick property. This will of course cancel your action on the server but you will obtain the fade effect:
<asp: Button ... OnClientClick="startButtonClick();return false;"></asp:Button>
To work around this and still submit your request, you can try to use the ASP.NET __doPostBack method in JavaScript
ASP.NET:
<asp:Button ID="startButton" class="startButton" runat="server" Text="Click Me!" OnClientClick="startButtonClick(this);return false;"/>
JavaScript:
function startButtonClick(button)
{
$("#startButtonSpan > input").fadeOut(500, function(){__doPostBack(button.name, "")});
}
The __doPostBack method takes two arguments: the name of the control that is doing the postback and a postback argument that can be use to send more info on the server. In the case of the asp:Button, the name of the button should be sufficient to send the request without a problem.
Using this technique you will fade the button on the client and also trigger the action on the server. I cannot guarantee that this exact code will work (I don't have access to a dev environment right now) but you should get the idea.
If I could, I would like to provide another answer for those that use MasterPages and find that you can't always use $("#<%= SomeContentControl.ClientID %>") when working with Content controls.
What I do is set the MasterPage ID in my Init() like this:
protected void Page_Init( object sender, EventArgs e )
{
// this must be done in Page_Init or the controls
// will still use "ctl00_xxx", instead of "Mstr_xxx"
this.ID = "Mstr";
}
Then, you can do something like this with your jQuery:
var masterId = "Mstr",
$startButton = getContentControl("startButton"),
$message = $("#jQueryTest");
function getContentControl( ctrlId )
{
return $("#" + masterId + "_" + ctrlId);
}
function hideStartButton()
{
$startButton
.stop(true, true)
.fadeOut("slow", showMessage);
}
function showMessage()
{
$message
.stop(true, true)
.fadeIn("slow");
}
$startButton.on("click", hideStartButton);
Here is a jsFiddle that has the Mstr_ prefix already inserted as if ASP.NET rendered it.

Cannot call modal popup of master page in child page

I am using a modal popup for to display login box. The modal popup is in the master page and associated with a LogIn link therein. Now I want to call the same modal popup in a child page using a different link button. The modal popup can be called by the LogIn link of master page but I want to add this second control (linkButton) in child page, which can call the modalpop of master page.
I tried this in child page:
function LogIn2()
{
$find("programmaticModalPopupBehavior").show();
}
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="LogIn2">Log in</asp:LinkButton>
Reference: multiple TargetControls for the ModalPopup
How to call the functional modal popup of master page from a control in child page??
Update:
This is in masterpage:
<ajax:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="linkLog" CancelControlID="linkClose" BackgroundCssClass="cssModal" PopupControlID="panelPopUp" BehaviorID="programmaticModalPopupBehavior" PopupDragHandleControlID="panelDrag"></ajax:ModalPopupExtender>
Add an OnClientClick for the LinkButton, like this:
<asp:LinkButton ID="LinkButton1" runat="server" Text="Login 2" OnClientClick="return LogIn2()" ...>
Put the JavaScript function in your master page, and after showing the popup, have the method return false to prevent the LinkButton from doing a postback:
function LogIn2()
{
$find("<%=ModalPopupExtender1.UniqueID%>").show();
return false;
}
As an alternative, you may also add return false to the OnClientClick of the LinkButton, after the LogIn2 function has executed, like this:
<asp:LinkButton ID="LinkButton1" runat="server" Text="Login 2" OnClientClick="LogIn2();return false;" ...>
I m not sure about this but let me confirm are you using the control of ajax like given below
<asp:ModalPopupExtender ID="ImageUploaderEx" runat="server" BehaviorID="ImageUploaderBID" TargetControlID="Link Name" CancelControlID="Cancel" PopupControlID="Panel to be open" OnCancelScript="hideUploader()"></asp:ModalPopupExtender>
If Yes then you can use this same modal with a different name, If this doesnt work let me know I will try my best
try this if it works
((ModalPopupExtender)Page.Master.FindControl("ModalPopupExtender1")).Show();
and add this as well
using AjaxControlToolkit;

Categories