I have button in my page that when click on fires a jquery method that shows a div, but if the browser doesn't support javascript I want to fire a code behind method (using a postback). Is this possible? I'm already using this code but i can't see any results:
<asp:Button ID="Button1" runat="server" Text="Upload Files Here" Width="187px" onClick="CodeBehindMethod" OnClientClick="show_upload_box();return false"/>
and this is my jquery code:
function show_upload_box() {
$("#pop_up_background").css({
"opacity": "0.4"
});
$("#pop_up_background").fadeIn("slow");
$("#signup_pop_up_box").fadeIn('slow');
window.scroll(0, 0);
}
If the browser doesn't support JavaScript then your button would not work.
In ASP.NET, the runat="server" instruction is actually tied to a JavaScript method that submits a form (view page source to see that method).
Change or remove the value of that property to prevent the post back to your server.
Related
I'm using the <asp:LinkButton />’s OnClick function on the server side to fetch data. I'm using OnClientClick to open a popup and populate data. But the page refreshes when I click the button. Please see the attached code and help me to fix the issue.
<asp:LinkButton ID="lnkEdit_Bill" runat="server" CssClass="lnkAddressButton" OnClientClick="javascript:ShowDialog_Both_New('Invoice','edit');" OnClick="lnkEdit_Bill_Click_new" >Edit</asp:LinkButton>
Google event.preventDefault(), I believe i've used that previously to prevent a postback. Also remove the OnClick, if that's not what you want, and just use OnClientClick.
Bro, if you don't want the postback, just fire the javascript. and you definitely don't need the server control. which you can present your code as below:
Edit
Let's says if you still insist want to user ASP.NET server control of LinkButton, then you can do something like this:
<asp:LinkButton ID="lnkEdit_Bill" runat="server" CssClass="lnkAddressButton"
OnClientClick="ShowDialog_Both_New('Invoice','edit'); return false;" >
Edit
</asp:LinkButton>
I have a webpage with a control to handle all the comments so I don't have to copy the code from one page to another. When I click the button, the code to handle the submit is not happening. I want the page to have no .axd references.
This is my form statement on the .aspx page
<form id="formAlpha" method="post" runat="server" action="">
<Comm:Comm ID="comments" runat="server" />
</form>
The processing for saving the comments is in the control.aspx
In the code of the webpage, I set the action to be
formAlpha.Action = Request.RawUrl;
This is my submit button in the control.
<asp:button ID="cmdSubmit" CausesValidation="false" UseSubmitBehavior="true" text="Submit" runat="server" OnClick="cmdSubmit_Click" OnClientClick="return ValidateSubmission();" />
The JS code executes correctly, it displays an alert box and then returns true. THe page reloads but the click event doesn't work. The load event fires up again and the IsPostBack is false. Its not submitting but reloading.
I'm using WebForms C#, not MVC
Just an idea taken from the official MSDN page, remove the "return" (and maybe the semicolon too) keyword from the onClientClick declaration:
OnClientClick="ValidateSubmission()"
I'm using CodeMirror in an ASP.NET web application. The web app uses UpdatePanel (ajax).
In ajax postback, I'm not able to get updated text from the CodeMirror textbox on server side and after the postback, the text gets reset. This WORKS if I don't use an update panel. What am I missing?
Below is the code mirror code:
editor = CodeMirror.fromTextArea(document.getElementById("<%=txtLua.ClientID%>"), {
matchBrackets: true,
theme: "neat",
pollInterval: 100,
continuousScanning: 500
});
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<asp:TextBox runat="server" ID="txtLua" Height="320" Width="600" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" Text="btn" />
</ContentTemplate>
</asp:UpdatePanel>
Is there an asp.net/C# sample for using CodeMirror? Any help is appreciated.
The short answer is: create a javascript event hook that fires early (before the UpdatePanel begins to do its work) and manually calls CodeMirror's .save() function.
The problem seems to arise because the auto-magic form.submit override that CodeMirror supplies is triggered after the ScriptManager has already passed the ViewState of the panel back to the server. This means the server only receives the TextBox in its initial state (CodeMirror hasn't put what you've typed into it yet). I checked the DOM events in Chrome and ScriptManager's hook was consistently ahead of the form.submit override that CodeMirror added.
I got around this by adding an .onclick to the submit button right after CodeMirror loaded. Using your example:
var editor = CodeMirror.fromTextArea(document.getElementById("<%=txtLua.ClientID%>"), {
leaveSubmitMethodAlone: true, //since you don't need this anymore, no reason to complicate your DOM
matchBrackets: true,
theme: "neat",
pollInterval: 100,
continuousScanning: 500
});
window['cmLocalStateEvent'] = function () { editor.save(); };
//saveButton = document.getElementById("<%=btn.ClientID%>"); //grab the save button
//if (saveButton) {
// saveButton.onclick = function () { editor.save(); }; //make it do what the submit action was going to do
//}
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<asp:TextBox runat="server" ID="txtLua" Height="320" Width="600" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClientClick="if(window['cmLocalStateEvent'])window.cmLocalStateEvent();" OnClick="btn_Click" Text="btn" />
</ContentTemplate>
</asp:UpdatePanel>
Now the .onclick is ahead of the ScriptManager hook, and will fire first. Infact, if you put an OnTextChanged= and AutoPostBack= on the TextBox it'll fire even before the button that you just clicked does.
Essentially, the trick is to get CodeMirror's save to apply before ScriptManager submits the ViewState back to the server.
EDIT:
I've found since posting this, that you'll run into a troublesome issue if your submit button is also inside the UpdatePanel (which yours is). The .onclick will not persist after the initial submit and you'll be reset to a clean button with no event hooks. My current working solution to that scenario is adding a window['cmLocalSaveEvent'] function and adding the client .onclick to the ASP template (so the panel refresh puts it back for you). Updated code above to reflect this.
This is how I managed to make it work, just in case someone else needs it. After instantiating editor, I'm keeping the textbox aligned every time the blur event occurs.
editor.on('blur', function () {
arguments[0].save();
});
I have
<div id="ulAndil" runat="server">
</div>
and button
<asp:Button ID="btnAssignJudToCourse" runat="server" Text="تاكيد "
CssClass="button" CausesValidation="false"
OnClientClick="javascript:getShape();"
OnClick="btnAssignJudToCourse_Click" Visible="false" />
At runtime I create a lot of sortable html controls and then click confirm
I want to save inner html of this div before postback of button in session or anything
Using jQuery so when user click the button jQuery take inner html of div and stores it in cookie session any thing to store it after postback I can retrieve it
Please I want the exact code for this jQuery or javascript method and how to call on button
Why don't you put the generated HTML in a hidden field and postback.
Say getShape() is your javascript function
function getShape()
{
//your validations applied
document.getElementById('hdnHTML').value = document.getElementById('ulAndil').value;
return true;
}
where hdnHTML is the hidden element of HTML.
my button on server works on alertMsg() how ever doesnt work on playSelected()
my button on Html works on playSelected() and alertMsg()
anyone can figure out for me why?
javascript
function playSelected() {
var a = "Video/" + document.getElementById("TextBox2").value + ".flv";
jwplayer("mediaplayer").setup({
flashplayer: "jwplayer/player.swf",
file: a,
image: "jwplayer/preview.jpg"
});
}
function alertMsg() {
alert("testing123");
}
button in html
input type="button" runat="server" value="Click me!" onclick='playSelected()'
button in server
asp:Button ID="Button2" runat="server" text="call javascript" OnClientClick="playSelected(); return true;" UseSubmitBehaviour="false"
<asp:Button runat="server"
OnClientClick='playSelected(); return true;'
UseSubmitBehaviour="false" />
Make sure your java script is allowed on the clietn browser. Also do postbacks logic in your page_load event. I would rather create the Button on overriden CreateChildControls method of the page, and on overriden OnPreRender wireup my java script and register the script with ClientScriptManager method RegisterStartupScript. So that you can have nice page life cycle control.