ASP.NET, Visual Studio, C# and Javascript - asp.net

I have a simple ASPX page based of a master page. On the ASPX page, I have two drop downs and a button. When pressing the button, I want to execute some javascript. To do this, I have used the Button's attribute collection (Add("onclick", script)).
My script looks like this:
string script = "return confirm ('You have selected' + document.getelementbyid(DropDownList1).value + document.getelementbyid(DropDownList2).value)");
Button1.Attributes.Add("onclick", script);
The drop down box element names are spot on, but why doesn't the javascript fire? The button's click event (obviously) gets fired when I click the event, but nothing happens.
This is being done in C#. If I use ASPX and write a basic javascript function in the ASPX markup, intellisense doesn't even come up with a value property for documentgetelementbyid. Why? My browser is fully js enabled (Firefox) as I had this working before but when I added the masterpage and based my ASPX page of that, everything went pear shaped.
Why doesn't the above script works?
Thanks

had you try adding the client ID
string script = "javascript:return confirm ('You have selected' + document.getelementbyid('"+DropDownList1.ClientID+"').value + document.getElementByid('"+DropDownList2.ClienID+"').value)");

I'm not entirely sure of your environment but you may want to take a peek at the source that's being generated by your ASP page. Master pages add a prefix to control names "breaking" the getElementById call. Take a look at the following: http://west-wind.com/weblog/posts/4605.aspx to see if that corrects your problem.

case sensitive: document.getElementById?

Shouldn't DropDownList1 and DropDownList2 be in quotes?

When some JavaScript code seems to not work, it's probably because the syntax is invalid.
To prevent that you can:
Validate your current statement (I'm thinking that maybe the ids of the drop-downs should be surrounded by single quotes).
Try with a simple alert first and see if it works -- alert('Hello world'); --
If not, try playing with the OnClientClick property of the button.

You also should have the "javascript" in your attributes:
string script = "javascript:confirm ('You have selected' + document.getElementById(DropDownList1).value + document.getElementById(DropDownList2).value)");
Button1.Attributes.Add("onclick", script);

Related

Asp.Net CultureInfo switching: trying to avoid non postback calls

As if in this article I implemented an easy way to switch between languages on a website.
The issue is if I want to switch languages with a dropdown list, and leave the last selected language on the dropdown list control's view.
Every article I've seen about CulturalInfo-switching sends a Server.transfer() call right after the language switch, and that event causes the non-postback page reloading, including my dropdownlist control which realoads from default position.
I tried a response.redirect(), but it still is a non postback call
I think I need a way to check if it's a Server.transfer() call in the page load, but still haven't found a way to develop this
Thank you.
Assuming you followed the same approach as in that article, how about this:
string path = this.Request.Path;
if (path.LastIndexOf('?') > 0)
path += "&lang=" + senderLink.CommandArgument;
else
path += "?lang=" + senderLink.CommandArgument;
this.Server.Transfer(path);
Then, on the page that this.Server.Transfer executes, get lang value from this.Request.QueryString["lang"], and set the dropdown's SelectedItem appropriately.
Alternatively, keep Server.Transfer(Request.Path);, but on the receiving page, parse Session["MyCulture"] instead to set the dropdown. +1

Unable to call Javascript from Codebehind

I know my issue is very common and similar types has been asked many times over here, but my problem is somewhat different. I am working on ASP.Net 4.0 web application where on my required page i have ajax toolkit 4 calendar control, toolkitscript manager and few asp controls. Now from that pop up i am doing a save operation on button click. What i want is to close popup window after successful save. Problem is not in saving but after saving , automatically closing the popup screen. I have tried the following ways:
RegisterStartUpScriptBlock(this.GetType,"closeForm","return window.close();") and all other required params
ClientScript.RegisterStartUpScript()--- alongwith params and with both return window.close(); and window.close() also with self.close();
Also i have under the title tag...
I think i have tried all the ways , i can. I feel i am lost. Please help me out....
if your using a script manager on the page...
first create a function to close the calendar in js in your html...
function closeCalendar(){
....
}
then on the codebehind use this to call that js function
string script = string.Format(#"closeCalendar()");
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
If the popup you mean is the AJAX Toolkit Modal Popup, you can just call popup.Hide(); in codebehind.
If it is a browser window, did you try to remove the return part from your code?
Note that windows.close() will not work unless the popup is opened via window.open();
Also, did you try to just put the script tag inside PlaceHolder control that is hidden (server-side Visible=false) by default and only shown when you need?

At what asp.net page event are clientIDs assigned?

I want to do something like this:
Panel divPanel = new Panel();
divPanel.ID = "divPanel";
this.Page.Controls.Add(divPanel);
string script = "function alertID(){alert("the id is: "+divPanel.ClientID+");}";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scripttests", script);
But when I put this code in page_load, I don't get the full id, which is ctl00_ContentMain_divPanel, I just get divPanel.
Is there another event I should use? How do I make this work?
PreRender is fine, however it should work in Page_Load as well.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid(VS.71).aspx
Is your script rendering code running in page_load as well? They are together in your example above, but perhaps they were just copy/pasted together?
If this code is in a custom control, you also need to add the INamingContainer interface to the control declaration for ClientID to work as you expect:
http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx
Additionally, if this is a custom control, you should add to the Controls collection of the control itself, not the containing page. ie: this.Controls.Add() instead of this.Page.Controls.Add()
In your example you are adding directly to the Page controls collection. This would mean that the client id should be divPanel. Perhaps the client id you expect it to be is the incorrect one.

Get a controls full ClientID from just ID in asp.net (client side)

This problem is the bane of my asp.net coding life.
I have a control, in code, and I want to get its full ClientID but I cannot until I add it to the page.
However I cannot add it to the page yet. I want to add some javascript to the onclick.
I can add something like "myMethod(this)" to the onclick, which is great and sidesteps the problem, but if I want a select all button for example, that runs "myMethod(theItem); myMethod(theItem2); myMethod(theItem3)" then I can no longer use 'this'.
I can't do "myMethod(document.getElementByID('" + myObject.ClientID + "'))'" or whatever either, because the ClientID isn't set yet.
So is there a javascript method that I can paste into my page that will find the element from its ID (ClientID and ID are the same before it is added to the page)?
You can add it to the page then add the javascript. As long as you still have a reference to the control, you can put it wherever, then any updates will happen to it in-place.

Is there any reason why an asp:Button will work but an asp:LinkButton will not?

I am building an admin portal for a helpdesk. On one page I have a dropdown with a LinkButton, and another a dropdown with a Button. Both buttons redirect to the page they are clicked from by firing off a Response.Redirect(), like so:
Response.Redirect(String.Format("article.aspx?action={0}", ActionDropDown.SelectedValue), False)
This allows me to use one page for multiple functions.
Anyway, the page that the LinkButton does NOT work on has a WYSIWYG editor, FCKeditor, in it and FireBug throws the following error when I click the LinkButton:
FCKeditorAPI is not defined
- WebForm_OnSubmit()()
- WebForm_OnSubmit()
- __doPostBack("ctl00$Content$LinkButton1", "")
- Sys$WebForms$PageRequestManager$_doPostBack("ctl00$Content$LinkButton1", "")
- (?)()()
- javascript:__doPostBack('ctl00$Content$LinkButton1','')
- var editor = FCKeditorAPI.GetInstance('ctl00_Content_NewArticleEditor');
No errors when I use a button. Any thoughts?
Javascript is NOT disabled in the client.
Not a full answer here, but a couple of things to check would be that you ensure JavaScript turned on on the client? (sounds like it from the error).
The most obvious thing to check would be what the button is actually doing on the click. Is it rendered as a submit button, or is it doing a postback?
LinkButton uses javascript. Check that it's not turned off on the browser.
I think that it's because LinkButton is rendered into his own <form>, which is inherited in the FCKEdit, or maybe FCKeditor overrides it's form's onSubmit behaviour?
Generally, when a browser encounters a JavaScript error, all script after that point fails to work.
Is the FCK editor working as you expect it to? Are all the JavaScript files that it requires in place correctly? You can check this on the Scirpt tab of FireBug:
Select the Script tab, ensure that script is enabled, and then in the top row of FireBug, you should see something like:
Clicking on the page path allows you to inspect the other JS includes:
Check that each of the requested files isn't returning an error of some kind.
Finally, are you using the FCKEditor.Net wrapper to access the FCK Editor, or just the editor as it stands?

Resources