I have a web forms application that uses signalR. I want to execute a method on the click of a button but when I click the button nothing happens. My hub class is defined as follows:
I also use the method "generateStatistics" in the hub class:
Now my client code is supposed to run the generateStatistics method on the click of a button. Here is the button code:
And finally this is what my client code looks like:
So, when I click the button nothing happens or at least the method isn't running because my breakpoint is never hit in the generateStatistics method. I did a bit of client javascript debugging in IE and it seems as though the connection is being made properly but the button click handler just doesn't seem to do anything. I guessed that it might be because the button is an asp server control with the runat specified but I turned it to an input html control and it still didn't seem to work or run the method. If you are wondering what the purpose of me using signalr here is that in the generateStatistics method I update the client with progress messages which you can infer from the javascript created method addProgress.
When you use ASP.NET, the HTML element ID will be different than what you specify on the control.
Try to use this instead:
$('#<%= btnStart.ClientID %>').click(function () { ... });
Related
Right now I have a radgrid that is filled with checkboxes. If I click on a check box it calls my code behind checkchanged. In check changed it checks and see if that item is active if its not it throws an alert. I want to change that alert to a confirm but the radajaxpanel doesn't appear to have a method for confirm so I figure I need to create one in javascript and call it in the code behind.
What I have now
else
{
RadAjaxPanel1.Alert("The event you selected is currently In-Active");
}
So I assume I would need to change that to a call of the javascript or something similar to call for a confirm and then return the response in the codebehind to either continue or stop.
EDIT: If this helps maybe what I am trying to explain is. On codebehind trigger a javascript method to run and return a value for my codebehind to evaluate and continue or stop running.
EDIT 2:Or what I want to change is on clientclick it calls the javascript and if the javascript return Yes then it calls the codebehind. Maybe that is more likely and possible.
I have code that when a user wants to page through a GridView, it asks them (using a JavaScript confirm) if they want to save the data from the grid.
So, I'm able to get the confirm to work (with the code-behind saving function), but I'm noticing that it's not firing the OnPageIndexChanging method - which basically defeats the purpose here.
So, to summarize, can JavaScript access the OnPageIndexChanging method?
Thanks a lot
The OnPageIndexChanging event is a server-side event, so it's not surprising that your Javascript handler isn't getting triggered.
I'm not sure if ASP.Net has a "built in" way to do this; but you can do it by attaching your own Javascript listeners. Here is the general approach (I'm using JQuery to make it easier):
Write a JQuery selector that gets all the paging links that you want to confirm.
Add a click listener for each of those links
Make the confirm function the handler for those listeners
So, the code would look something like this:
$("#grid a").each(function () {
$(this).click(function () {
return confirm("really?");
});
});
Notes
Here grid is the ID of the GridView control, so #grid a selects every a tag within my grid.
Using return confirm() returns false if the user does not confirm, which effectively cancels the click event.
I am working with a website that has javascript that does some changes on the page load. However, when I load the page and handle the DocumentCompleted event, this change isn't there. If I then continue paste the DocumentCompleted event, I can see the change happen. However I need this change to happen during DocumentCompleted so I can check some things.
Is there an other event I can subscribe to, or a way to cause the webBrowser to do all the javscript on page?
Edit: This is what I am talking about.
I loaded a sample page just to show you, and clicked the submit button with all fields empty to generate an the error.
Here is the result:
http://s8.postimage.org/zfv6stcar/sfsdfsdfds.jpg
Now if I take the HTML at that precise moment from that WebBrowser control, and render it somewhere else, those errors go away. The same thing happens when the server sends back those errors. If I handle the DocumentCompleted event and take the html, it isnt there. But after the event, it shows up in the control.
Hope you understand, it's hard to explain.
The problem seems to be that the DocumentCompleted event is being fired before the javascript. You should do some reading on how client side/server side things function.
One option is to make a separate method for the DocumentCompleted event and call it form the javascript after it has been completed. This would get the sequencing of these events working properly, but is not very ideal.
Alternatively, you could call the javascript code at the beginning of your DocumentCompleted event. The link below gives a pretty good explanation of how to go about that.
http://forums.asp.net/t/1117189.aspx/1
Personally, I would avoid using javascript and do the validation on the client side .NET, but I don't know enough about the website to really say.
EDIT:
This should be the script you are looking for. Alternatively here is a thread related to your issue. Sorry I don't have the exact code as I don't have a project to test this on.
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx
Calling JavaScript Function From CodeBehind
RE-EDIT:
What is happening on the link you provided in the comments, is that each textbox is calling some javascript as well as the submit button. The best way to examine this is using the "Inspect Element" in the right-click menu on Google Chrome. For example, doing this on the textbox would show that it is registered with a few events:
onfocus="$('f_tip_Username').style.display = 'inline'"
onblur="$('f_tip_Username').style.display = 'none'"
onchange="$('f_err_Username').style.display = 'none'"
The first the element with the ID 'f_tip_Username', sets the display style of that element to inline (visible).
The submit button calls the following:
onclick="return o_edit_profile_form.validate()"
Doing a find on "o_edit_profile_form" in the source code, you can find the exact javascript location that is being called. Enjoy!
FINAL EDIT (hopefully?):
Follow these steps: go to your site, right click and go view source. Do a find for "f_tip_Username". This is the ID of one of the div tags being used. The third entry of it, should be a "div tag" that is used under the first textbox to warn of "min 3 characters".
You'll notice above that in the code is a input type "text" with the Name "Username". Notice the three events it has registered in it:
onfocus="$('f_tip_Username').style.display = 'inline'"
onblur="$('f_tip_Username').style.display = 'none'"
onchange="$('f_err_Username').style.display = 'none'"
These either hide or make visible, the div tag we found (f_tip_username) and also a separate div tag (f_err_Username) which is the error message div tag. Let me know if you are not able to find these in the source. Follow the steps I provided and you will find it in the "view source" OR in the DocumentText.
Ok, I've got a lightbox with a small form (2 fields) in it, inside an UpdatePanel, and I want to close this lightbox (must be done via javascript) when the 'Save' button is pressed.
However, there is a need to have a server-side CustomValidator on the page, and I only want to close the lightbox if this returns as valid.
Does anyone know a way to trigger javascript (or jQuery) code from a server-side validator?
You can add a little snippet of code using the ScriptManager to execute after the response comes back to the UpdatePanel.
if (Page.IsValid){
ScriptManager.RegisterStartupScript(
customValidator1,
typeof(MyPageClass),
"closeBox",
"myLightBoxVariableOnThePage.close()",
true);
}
When that server side validator runs, it will send a whole new page to the browser. Anything that was shown in the browser before was destroyed, including any state kept in your javascript. If new page bears a strong resemblance to the old page, you should consider this a happy coincidence.
Therefore, the thing to do here is rather than executing a javascript function, have your CustomValidator make the correct changes to the page on success so that it's rendered to the browser correctly in the first place.
I have a button called btnSubmit where i set the Form action attribute to a URL like so.
Protected Sub btnSubmit_Click(ByVa....
Form.Attributes.Add("action", "http://now.eloqua.com/e/f2.aspx")
End Sub
This does work but for some reason it only works after I clicked the button the second time. Why is this and how can i fix this?
I am using ASP.NET 3.5 with VB.NET(C# code is also fine)
What I actually want to do it do some code on the submit and then as soons as everything is complete, then set the form action attribute where it must send the form data to another location at that URL.
Thanks in advance!
Fabian is right.
Your code executes on the serverside, after the first submit.
To do what you want, you'll need to emit some javascript using the scriptmanager, which executes in the client, since it will all have to happen before the submit happens in the first place.
Use Page.ClientScript.RegisterClientScriptBlock() to emit a script block with a suitable function which does your stuff, then sets the form's action attribute. Call that function from the button using the OnClientClick attribute.
It doesn't work the first time because the form on which the attributes are added is already rendered.
The first time you click the button, it sets the form attribute, the second time you click it, it's submitting the form that you edited the first time round...
You might want to set the form attribute at some other point in the page lifecycle.
If you need to retain the POST data between pages you might want to use Server.Tranfer. See here for a most excellent explanation: Using asp.net, how do I redirect a user and change the POST data at the same time?