How can I write this code:
<script type="text/javascript" id="igClientScript"> function showModalWin(w) { var w = $find('<%= modalwindow.ClientID %>'); w.set_windowState($IG.DialogWindowState.Normal); } </script>
in a separate js file?.
Thaks.
HI,
create a file with that code call it WDW.js and add the following script code to your aspx page
<script src="WDW.js" type="text/javascript"></script>
I knew this, but when I call the function from aspx I receveive error "Can't eval WDW...
The call is OK because if I write in WDW.js:
{ alert("OK"); // var w = $find('<%= modalwindow.ClientID %>'); // w.set_windowState($IG.DialogWindowState.Normal); }
it works fine.
It seems like the sintax
$find('<%= modalwindow.ClientID %>'
is not correct out of html code.
antfor said:It seems like the sintax $find('<%= modalwindow.ClientID %>' is not correct out of html code.
I notice that you already have "w" as a parameter in your function:
antfor said:function showModalWin(w)
With this in mind, you should get a reference to your WebDialogWindow first, then pass this reference as a parameter in your call your showModalWin() function. Your function would look like this:
<script type="text/javascript" id="igClientScript"> function showModalWin(w){ // var w = $find('<%= modalwindow.ClientID %>'); if (w && w != "undefined") w.set_windowState($IG.DialogWindowState.Normal);}</script>
The "if" statement is basically a check to make sure that you have some kind of object.
The w parameter is becausse all the test I'm doing ...
The problem is that I don't know how to instance the object in the aspx page.
I tried with:
....showModalWin($find('" + modalwindow.ClientID + "')))"/>
....showModalWin(document.getElementByID('modalwindow'))"/>
etc, etc, etc ...
Always get the error : Can't eval showModalWin...
After this, I begun to try it without parameters, directly in js code, but without success
You need to use the $find() function to get a reference to your WebDialogWindow. You also need to use its client-side ID, which means that you need to have a reference to the control to get at it.
Assuming that the server-side ID of your WebDialogWindow instance is "modalwindow", and assuming that this control exists on the page where you've put this JavaScript code, I suspect the following is what you're after:
showModalWin($find('<%= modalwindow.ClientID %>'));
You also need to make sure that you have a SCRIPT element on your page, whose "src" attribute points to the file that contains your showModalWin() function. Given the "Can't eval showModalWin" error message, it's possible that your browser can't find the function to be able to call it.
The call to .js file is right because If I write only an alert("xxx") it works ok.
.js file :
showmodalwin = function(w) { //only for test = alert("xxx"); this works ok when I try it
w.set_windowState($IG.DialogWindowState.Normal);}
.aspx file:
...<ig:WebDialogWindow ID="modalwin" runat="server" Height="300px" InitialLocation="Centered" Width="400px" Modal="True" WindowState="Hidden"> <ContentPane>... </ContentPane></ig:WebDialogWindow>...
<igtxt:WebImageButton ID="b1" runat="server" AutoSubmit="False"> <ClientSideEvents Click= "showmodalwin($find('<%= modalwin.ClientID %>'));"/>
When I click on the button I get: Can't eval $find('<%= VFiltros.ClientID %>'));Debugging show that js file receive w as null
I knew that my surrender would inspire to you
Nice approach and nice solution
Thank you very much.
Since your objective is to move all of your JavaScript code into a separate file, you can make this happen by using a little bit of server-side code.
Instead of using the ClientSideEvents element in the ASPX markup, you can connect the client-side Cick event by using server-side code. For instance, I used the following line of code in the server-side Init event of my WebDialogWindow:
protected void WebImageButton1_Init(object sender, EventArgs e){ WebImageButton wib = sender as WebImageButton; wib.ClientSideEvents.Click = string.Format("showModalWin($find('{0}'));", modalwindow.ClientID);}
With this, I could avoid putting any JavaScript functions directly on my ASPX page.
I've attached a sample project to this post demonstrating what I've done. This sample is written in Visual Studio 2008, targeting CLR 3.5, and using NetAdvantage for Web Client (ASP.NET CLR 3.5) 2009 Volume 1.
Thank you for the help.
I have 2 objectives:
1) Move all my JScript code to a separate files
2) Use client code for showmodalwindow.
I know that this works if i write the function in the aspx page but I don't want this.
I think that the key of all is what you said:
ClientSideEvents Click="showModalWin($find('<%= modalwindow.ClientID %>'));" />
... then the WebDialogWindow reference was not returned - it always entered the showModalWin() function as a null value. This is because the <%= %> substitution doesn't get evaluated in this scenario
So I give up.
I put together a test project, and I believe I see what's going on.
One issue appears to be an issue with registering external JavaScript files when you have a ScriptManager on the page. I used the following line to register my external JavaScript file on my page:<script type="text/javascript" language="javascript" src="WDW_Script.js" />
Initially, I had placed this in the HEAD of the page. This doesn't work in conjunction with a ScriptManager, and caused the ASP.NET AJAX framework to throw a number of errors at me. As a result, none of the ASP.NET AJAX functionality worked, including attempting to get a reference to my WebDialogWindow, yet JavaScript alerts worked fine - a result similar to what you described..
There are two solutions that I've found:
<asp:ScriptManager ID="ScriptManager1" runat="server"> <Scripts> <asp:ScriptReference Path="./WDW_Script.js" /> </Scripts></asp:ScriptManager>
Another issue is with the client-side Click event of WebImageButton. If I set the client-side Click event as follows:<ClientSideEvents Click="showModalWin($find('<%= modalwindow.ClientID %>'));" />
... then the WebDialogWindow reference was not returned - it always entered the showModalWin() function as a null value. This is because the <%= %> substitution doesn't get evaluated in this scenario - nor is it supposed to be.
Instead, I set the client-side Click event of the WebImageButton as follows:<ClientSideEvents Click="WebImageButton1_Click" />
... and declared the following JavaScript function on my ASPX page:
function WebImageButton1_Click(oButton, oEvent) {showModalWin($find('<%= modalwindow.ClientID %>'));}
This worked in my test.
Another alternative would be to set the client-side Click event of WebImageButton using server-side code, where you can get the ClientID property of your WebDialogWindow and substitute that ID to your function call directly.
I hope this information proves helpful.
Sorry, this is because I write the post directly.
When I click on the button I get: Can't eval $find('<%= modalwin.ClientID %>'));