It's possible that there's no way to do this, but I figure I would ask. I'm relatively new to asp.net, having played with it for about a week now. What I have right now is a page that calls a web service, polls it until it's done (with progress displayed in an UpdatePanel), then hides the progress text and instead displays the result (a recursive list of files with some metadata) by creating a TreeView and adding it to the UpdatePanel. What I would like is for clicking a node in the TreeView to update a second UpdatePanel with extended information (obtained server-side) about the node that was just clicked on. I don't see any way to call a codebehind function by clicking a TreeNode, but I can call Javascript code by setting the node's NavigateUrl to "javascript:function([the full path of the node])".
At this point, though, I'm kind of stumped. StackOverflow is full of correctly-answered questions about how to call back into the codebehind from javascript (using a WebMethod, or equivalent), but apparently you can't call code that isn't static, which would mean I couldn't modify the page itself, or for that matter, access the session or page state. StackOverflow is also full of questions about how to have javascript request that an UpdatePanel refresh itself (__doPostBack()), but without any way to communicate to the server what was clicked on, the UpdatePanel wouldn't know what to display.
Thus, the question, which I'm hoping has an answer: am I missing some clever way to have javascript on the page trigger a server-side function capable of taking a parameter and using it to do a partial postback of a different UpdatePanel?
Thanks!
Actually it's quite straightforward. Just place a LinkButton inside of second update panel (it can have an empty text and thus be invisible), in JavaScript call client-side .click() method of that control, and in ASP.NET handle server-side OnClick event.
Related
I have a caching problem on a button event in Java EE. In JSP, I call an inline JavaScript method in JSP page. This method requests a function in a Java class. Communication between the JSP and the method of java class occurs via structs. The problem is that after I modified this java class, all JSP screen links squeegee when the application calls the same JavaScript method. But this method is only called through a onClink() event is a screen button. Now all JSP buttons are calling this method, even if your event JavaScript onClink is calling another method. If the problem is not the call of JavaScript methods, how do I solve this problem? Refreshing this page does not.
An alternative would be to clear the cache only javascript. And keep the session java application and any other infromação of aplciação java web browser. The problem began to occur when I made an appointment in a java method using as parameter the "ApplicationForm" that takes the form data and use it as research. But for some reason this confunfoiu the browser
I do not want to sound ridiculous, but I can not put the code here.It is copyrighted and owned the company. But it is quite simple. Generic something would be.
JSP Page
The method includes call
// code jsp
<script>
incluirChamada function () {
// Calls the "javascript: incluirChamadaJava" java class via structs
}
the problem is that all the buttons call the incluirChamada () method. even if your onclink are specifying to another class
In the java code ...
javascript: incluirChamadaJava (ApplicationForm form) {
mehod slectUsingHibernate (Filter form) {
}
}
debug your code. You problem have some function that is not closed properly.
I want to get the answer of a window.prompt() alert box through the C# Code Behind file. It's just a one-liner of JavaScript code, so I thought I could even execute it from the Code Behind file. It doesn't really matter to me whether it's a <script> tag in the .aspx file or executed through the .aspx.cs file.
I thought of having the script executed (called from the C# part) and then having the return value assigned to a certain not visible field, but is there any better way to do it?
The obvious way would probably go something like this:
.aspx file:
<script>
function foo() {
document.getElementById('MyFieldID').value = window.prompt('Answer this question:', '');
}
</script>
.aspx.cs file:
////////////////////////////////////////////////
//MyFieldID.Text now contains whatever I want//
//////////////////////////////////////////////
What do you say? Is there any better way?
Better way is always opinion based. What I'd say is you have a few options, all depend on what you're doing. HTTP and ASP.NET provide us a few means of sending data to the server, there are 3 main ones before HTML5:
Query string
Form values
AJAX calls
If you're redirecting the user to a new page after they answer the prompt, you can just send them to yournewurl.aspx?promptAnswer=*whatever*
If you're doing a postback, then you can use a form value (this looks like what you're doing in your example). You can put an <asp:HiddenField> on the page and populate it from JavaScript before submitting the form.
If you need just the prompt response, but are not attempting to reload the page, you can make an AJAX call that sends the variable to the server (this still uses #1 or #2 to send the data, it just does it without reloading the page).
Which of those three options works best depends on your implementation. However, your solution should work just fine. However, since the control you'd likely be using to stuff the value into is a HiddenField it would be in MyFieldID.Value not MyFieldID.Text. The only other thing you have to deal with is if your MyFieldID is nested in some other controls (like a ContentPlaceHolder) such that the ClientID has naming containers pretended to it so it's really something like ContentPlanceHolder1_MyFieldID when accessed from JavaScript.
I have a ASP.Net Repeater control with a Table inside it. Is it possible to run a JavaScript function directly AFTER I call MyRepeater.DataBind()? I've been trying different things, but nothing is triggering the JavaScript function.
Thanks
Databinding occurs on the server in a postback as part of the Page Lifecycle process. In other words, excluding partial-postbadks, at the time this happens any existing DOM in the browser is destroyed. A whole new page is constructed on the server and transmitted to the browser, so that a new DOM can be built and rendered.
What all that means is that you want to think in terms of running your javascript in the page's onload event. One way to make this happen is using the ClientScriptManager.
Javascript can be called from server side by using RegisterStartupScript and RegisterClientScriptBlock methods.
http://www.mindfiresolutions.com/Register-clientside-startup-script-from-serverside-code-286.php
No. The javascript isn't even going to render and run until the code-behind has executed and the page delivered to the client. So it won't matter if adding the script is the first thing you do in the code-behind or the last thing you do (or directly after the DataBind()).
When using the ClientScriptManager Class, look at your code behind and you'll see the dynamic javascript is added just before the ending </form> tag (although it still may be possible to accomplish what you want to do, just with a different approach).
Well I found a solution, not sure it's the cleanest way to do it, but for my application's context it works:
I ran the javascript code after a partial postback using: Sys.WebForms.PageRequestManager.getInstance().add_endRequest();
Again, not the cleanest but suits the needs I have.
Thanks for all your input
in my applciation (asp.net - using vb as the code behind language) the server side (vb) pageload code runs first, and when this completes it runs the javascript onload. Is there anyway to run the javascript onload first? My javascript gets the value of a variable from the parent window (uses iframes), and uses that to set controls in the child window. The pageload deals with the control set (if the value from the parent is a certain value, then different checkboxes will automatically be checked).
The only other solution to this issue I can think of is to check the value at the moment to see if the boxes are checked when another function to generate the page's html runs. The issue with this is that the function is called from javascript using pagemethods and needs to be shared, and due to this I am unable to check in the start of this function to see if the checkbox is checked. Thanks for your help.
No, javascript always runs on the client after the server has finished sending the response to the client.
You'll have to change your javascript to run against the generated html.
The answer to your question is no.
However if I understand what you're trying to do, you want to modify controls based on a value from the server. Why not just send an AJAX request to get that value, then update the controls accordingly.
With that being said, it does seem like the architecture of your application is a bit in shambles so if all you need is a bandaid, maybe you can generate that server value, render it within an id or hidden input element, then read that value with your JS.
If you are just using it to get a value from the parent window you could use a bit of a workaround that I have used before. From your parent window you could set the source of the iframe and append a query string on to it that would have your value. Then from the page within the iframe you could check the querystring to get the value and then check your check boxes accordingly.
In the load pr prerender, depending on when you set your variable that you need, of your main page you could put something like iframeWhatever.Attributes.Add("src", "../filepath/page.aspx?ValueNeeded=" + ValueNeeded.ToString)
I am pretty sure that this would be able to do what you are currently trying to accomplish with javascript.
Seems like an easy thing, but no quite so apparently.
I have a look up page that does a complicated search of a database (first name, last_name ...) that is used as a common look up across an application. It run through a colorbox popup and then callsback to a generic js function on the calling page to set the result on the calling page as well as close the popup.
The problem is this requires alot of setup on the calling page, including writing the callback function that sets the values. (I can't modify the lookup page, have to work with it as is)
I've pulled the layout into a user control and now want to push the callback function to the page ONLY if another copy of the function doesn't already exist. This is the question. In my ascx code behind, how can I look to see if the page already contains the js function I"m looking for so that I don't duplicate that function?
Hopefully that's clear as mud. I've been googling this for a while and haven't found anything other than how to use js to determine if a js function exist (not what I need). Any help would be appreciated.
Use the RegisterClientScriptBlock method to add the script to the page:
Page.ClientScript.RegisterClientScriptBock(Page.GetType(), "callback", theScript, true);
The script will only be added to the page as long as there isn't one already added with the same type and name.