Excuting Javascript Commands From Cs
Is there a way that I can call a function like (fadeIn or fadeOut) from the .cs?
So thats my situation - I have a datalist of stuff. When A user clicks something on the list, it gives him some detail开发者_JS百科s about the thing he clicked and if he wants it, he clicks it again.
now, on the first click I changed the OnClientClick of the button to fadeOut the datalist. so on the second click (after he confirms the item) it fades out.
Problem is - If he clicks one thing than click another thing, the fadeOut occurs.
Lets say you have a javascript function named foo, and you want to execute it on the button click event. You can use the following code:
protected void btnFoo_Click(object sender, EventArgs e)
{
if(!ClientScript.IsStartupScriptRegistered("foo"))
ClientScript.RegisterStartupScript(GetType(), "foo", "foo();", true);
}
No. Your C# code only executes on the server.
You'll need another way to tell your javascript to run. Perhaps details about what you're trying to accomplish would help?
No. Your .cs file is going to run on the server, where as javascript runs on the client's machine in the browser. This means the code executes at different times. The best you could do is to use ClientScript.RegisterClientScriptBlock to run javascript on the page after the rendered html is returned to the client's browser.
I suspect that you may be wanting to handle an event on the server that is going to call the fadeIn and fadeOut functions. It might be helpful to review the asp.net page lifecycle to better understand why this isn't possible.
Is there a way that I can call a function like (fadeIn or fadeOut) from the .cs?
It depends.
If you're talking about a standard web page that uses ASP.Net to generate the response, then no, the server side C# executes to create the request, and it's work is done when the request is completed.
If you wanted to look at a technique referred to as long polling, then you could emulate this type of architecture, where in fact the C# code, in combination with Ajax, would control some of the client-side code, such as exemplified by Comet:
Comet described at Wikipedia
Eventually, web sockets will provide this type of ability (barring more security setbacks):
Web sockets described at Wikipedia
And, last, if you're talking about Windows Phone 7 development involving a WebBrowser component, you can invoke javascript as demonstrated in this article:
Invoking javascript in WebBrowser component
精彩评论