Asp.Net:Light ajax (get) call that doesn't rerender the page? - c#

Let say I have:
<asp:ImageButton ID="btnNoticeClose" runat="server"
ImageUrl="~/img/btn_spara_green.gif" ToolTip="Stäng"
OnClientClick="jQuery('#Notice').hide();"
Click="btnNoticeClose_Click"/>
How do I call a btnNoticeClose_Click without rerendering anything? As I don't need to rerender anything I don't want to use an UpdatePanel? or do I?

try:
OnClientClick="jQuery('#Notice').hide();return null;"
or write full function in js:
function CloseThis()
{
$("#Notice").hide();
return true;
}
OnClientClick="javascript:CloseThis();"
EDIT:- Well you want to call server side method as well and don't want to render anything. Then only option left is using page method and using jQuery AJAX. Please head here to Dave's blog for more information on this.

Related

How to call javascript function and c# code on asp:button?

When a user clicks a button on ASP.net page, I need to
Save file from asp:fileUpload in a folder on a server - I guess this needs to be done in C#, like in How to correctly use the ASP.NET FileUpload control
Run a javascript function like in How to call javascript function from asp.net button click event
Is there a way to combine C# and Javascript to achieve what I need? If not, how should I do it?
Try using the onClientClick property of the asp:button element.
Ex, on your .aspx file:
<script type="text/javascript">
function myFunction()
{
alert('hi');
}
</script>
...
<asp:button id="Button1"
usesubmitbehavior="true"
text="Open Web site"
onclientclick="myFunction()"
runat="server" onclick="Button1_Click" />
And in your code behind (.aspx.cs)
void Button1_Click (object sender, EventArgs e)
{
if (this.FileUpload1.HasFile)
{
this.FileUpload1.SaveAs("c:\\" + this.FileUpload1.FileName);
}
}
More info at
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx
Note that no JavaScript actually "runs" until the server-side code (C# in this case) has entirely completed and the resulting page is returned to the client. Once that page renders on the client, then JavaScript runs on the client.
So in order to execute your JavaScript code, all you need to do is include it in the page being returned to the client. There are a number of ways to do this, and the options depend on whether you're using WebForms or MVC.
You might use something like RegisterStartupScript in WebForms, for example. Or, you could just have the JavaScript code exist in a PlaceHolder control with Visible=false and only make the control visible in the response which intends the JavaScript code to run. (Roughly the same method is also easily usable in MVC by just wrapping the JavaScript code in a server-side condition to determine whether to render it or not.)
The main thing to remember is that you're not "running the JavaScript code from C#" or anything like that. There's a hard separation between server-side and client-side code. The server-side code ultimately builds the page that it sends back to the client, and that page can include JavaScript code to run on that client.

how to call a javascript function from codebehind in asp.net

protected void addSchoolButtonClick(object sender, ImageClickEventArgs e)
{
Page.ClientScript.RegisterStartupScript(GetType(), "MyKey1", "SchoolSearchPopUp();", true);
/*Some code*/
}
I am developing a website in asp.net.At a Hyperlink onclick event i want to call a javascript function"SchoolSearchPopUp()".this function is for creating a new popup window and it is working correctly.But my problem is ,a javascript function is calling or pop window opens only after executing the rest of the code in that function and that code need's some data that occurs as a result of popup.How can i create the popup before executing the rest of code in that function.
Change your postback trigger to something within the popup.
I don't think javascript can be called from code behind. C# is running from the server and java is all client side. There's a good explanation to a similar question here: http://forums.asp.net/t/1117189.aspx
If you need to execute a javascript function, you could try changing the hyperlink to a button and making use of the OnClientClick property. This executes script client side rather than calling a method on the server.
<asp:button id="Button1"
text="Click Here"
onclientclick="SchoolSearchPopUp()"
runat="server" />
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx
You will need to write JavaScript on the page to handle the click of the button first and then call to the page method on the server.
Add an OnClientClick attribute to your button element and run your JavaScript method from there:
<asp:Button ID="TestButton" OnClientClick="SchoolSearchPopup()" Text="Click Me" OnClick="addSchoolButtonClick" runat="server"/>
<script type="text/javascript">
function SchoolSearchPopup()
{
alert("Popup");
}
</script>
If you want to execute some javascript before your postback you will need to register your hyperlink's click event to a js method, then submit your post to the server after performing whatever client side logic you are looking to run. (not the other way around, using RegisterStartupScript)
Example:
$("#myHyperLink").click(function() {
// do page logic, in your case show a modal window
$("#myModalDivContainer").show();
// submit your post to the server... replace targetClientID with ID of server control you're posting to
__doPostBack('targetClientID', '');
// NOTE: If you want to perform an AJAX request instead simply use some jQuery here instead. it's up to you how to handle the request from this point :)
});
Hope this helps!

Call server method using JavaScript or jQuery

I wanted to know if you can call server side method using JavaScript or jQuery whilst doing a postback?
I have created a anchor button and want it to work same way as a linkbutton i.e. do postback and call a event. So i thought it could be possible of using JavaScript?
How would i do this? I have done alot of searches on net but could not find anything..
;
Have you tried searching for ajax?
http://api.jquery.com/category/ajax/
There's lots of tutorials online. If you're using dot net then there's also a JQuery plugin called dot net ajax which I find really useful. However ajax is a huge topic and your question is really broad so it's hard to be more specific!
What you could do is send an AJAX call to the server. But it's a really big topic, you can find a lot of information on the internet.
The simplest form with jQuery is
<a href="javascript:void(0);" id="SOMEID" >Call the server</a>;
$('#SOMEID').click(function(){
$.get('url/on/server', function(data){
//do something with data returned from server
});
});
This makes an Asynchronous call to the url url/on/server and then waits for an answer (since the call is Asynchronous the user can keep on doing things on the page)
Okay you need to postback. Take a look at this article --> http://weblogs.asp.net/mnolton/archive/2003/06/04/8260.aspx
The only catch is you have to call the GetPostbackReference every time a postback happens.
If you want to do postback from anchor tag using javascript, you can do
document.forms[0].submit(); //This will post your form
OR
__doPostBack();
OR
If you want to know which control caused postback.
JavaScript
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("n etscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
alert(theform.__EVENTARGUMENT.value);
theform.submit();
}
With following ASPX code to make it work
<input type="hidden" name="__EVENTTARGET" />
<input type="hidden" name="__EVENTARGUMENT" />
Hope this is what you are looking for.

How can i invoke javascript function within the webbrowser control?

I want my website/webpage to be navigated to a new window which displays the report.Even the Website gets redirected to a login page rather than the report page the moment when the javascript function is invoked.I have given my requirement below.can anybody help me out in finding the solution?
InvokeScript doesnot work in the following case.
<a onclick="return showReport('RPTType=SReport');" id="ctl00_tbr_lnkbtnS" class="blckclr" href="javascript:__doPostBack('ctl00$tbr$lnkbtnS','')">S</a>
I used the following statement
webBrowser1.Document.InvokeScript("showReport", new String[] { "RPTType=SReport" });
Thanks in advance....
I'm not sure I understand the question correctly, but I would think you should return false from the onclick event of the link if you're going to handle it with javascript, otherwise it will execute the javascript and then redirect to the href url.

how to write javascript in asp.net in code behind using C#

How can I write JavaScript code in asp.net in code behind using C#?
For example:
I have click button event when I click the button I want to invoke this java script code:
alert("You pressed Me!");
I want to know how to use java script from code behind.
Actually, this is what you need:
string myScriptValue = "function callMe() {alert('You pressed Me!'); }";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "myScriptName", myScriptValue, true);
Copy all of javascript into string and then register it into your aspx page in code-behind. Then in aspx page, you can call the javascript function whenever you want. You should write this code in Page_Load method in C# page.
Have a look at the ScriptManager class' RegisterClientScriptBlock and RegisterStartupScript methods.
One way to put some javascript onto the page into a specific location do this:
ASP.Net
<script type="text/javascript">
<asp:Literal id="litScript" runat="server" />
</script>
C#
litScript.Text = "alert("Hello!");"
Of course, you can put anything in there, and I'd recommend a javascript library.
Using the Scriptmanager is also an option.
Not an answer, but a suggestion.
Mixing your js within your code-behind can come back to haunt you, I agree with Adrian Magdas.
Anytime you need to make a simple change/update to your javascript you'll have to re-build your project, which means re-deploying instead of simply pushing out a single .js file.
Something like:
btnSomething.ClientClick = "alert('You pressed me!');";
You might also want to read up on the ScriptManager control and outputting blocks of script.
The right answers usually is "You don't". It's better to define your code in a .js file and use jQuery to hook-up the desired events.
$(document).ready(function() {
$('#myBtn').click(function() {
alert('Handler for .click() called.');
});};
If you want to register a script that will be used in connection with an UpdatePanel (AJAX) use ScriptManager class as Sani Huttunen pointed.
Otherwise you should use the class ClientScriptManager (methods Page.ClientScript.RegisterClientScriptBlock or Page.ClientScript.RegisterStartupScript)
As other user pointed, normally registering a script on the code behind can and should be avoided. It's not a very nice practice and you should do it only in cases where you have no other option.
Response.Write("<script language='javascript'>alert('You pressed Me!');</script>");

Categories