PDA

View Full Version : نوشتن توابع جاوا اسکریپت در کدبیهایند



iekrang
شنبه 24 دی 1384, 12:11 عصر
من چطور میتونم تابع window.open رو توی pageload ام بنویسم؟
("window.open("Pop-Up.htm","newWin","Width=200,height=200
اگر امکان داره من رو راهنمایی کنید.

mehdi_394
شنبه 24 دی 1384, 13:02 عصر
برای کنترلی که می خواهید پنجره جدید را باز کند این کد را اضافه کنید:


button1.attributes.add("onclick","window.open(www.barnamenevis.org)"(

iekrang
شنبه 24 دی 1384, 13:41 عصر
من سوالم اینه که کنترلی ندارم برای باز کردن پنجره.میخواهم با باز شدن صفحه یعنی pageload این اتفاق بیفتد.

Behrouz_Rad
شنبه 24 دی 1384, 14:09 عصر
به تگ body یه ID نسبت بده. runat رو برابر با server قرار بده.
یه متغیر از نوع Protected و به نام ID تگ body تعریف کن و با استفاده از متد Add خاصیت Attributes اش، به روال onload تگ body مقدار بده.
در فایل aspx:


<body id="X" runat="server">

در قسمت Declarations وب فرم:


Protected X As System.Web.UI.HtmlControls.HtmlGenericControl

در Page_Load:


X.Attributes.Add("onload","myfunction();")

موفق باشید.

Identifier
شنبه 24 دی 1384, 15:47 عصر
آقا بهروز جوابتون عالی بود . سوال من نبود ولی یک جایی شبیه این مشکل رو داشتم راه حل خیلی جالبی بود
واقعا مرسی

iekrang
یک شنبه 25 دی 1384, 10:32 صبح
ممنون آقای راد.
یه سوال دیگه : من میخواستم اینکار رو به این خاطر انجام بدم که فقط یک بار popup باز بشه یعنی از IsPostBack استفاده کنم ولی اینطوری نشد و هر بار این popup باز میشه.میشه بگید اشتباهم کجاس؟

javad3151
یک شنبه 25 دی 1384, 10:56 صبح
در دات نت 2.0 امکانات جدید و جالبی در این زمینه ارائه شده متن زیر بخشی از یک کتاب است:


1.5. Insert Client Script into a Page
There are times when you need to insert client-side JavaScript into your page to implement client-side functionalities. Take the example of an eBanking web application. If the user has not been active for a certain period of time after logging in, the application will prompt the user with a pop-up window asking if the user would like to continue. Employing a pop-up window is more likely to draw the user's attention than simply displaying the message on the web page, and this is best implemented with client-side script.


--------------------------------------------------------------------------------
Note: You can now insert client script into your web page as naturally as writing your server-side code.
--------------------------------------------------------------------------------

In ASP.NET 2.0, you can insert client-side script by using the ClientScript property of the Page class.

1.5.1. How do I do that?
To see how you can insert a client script into an ASP.NET 2.0 web application, you will create an application that displays the current time in a JavaScript window when the application is loaded.

In Visual Studio 2005, create a new web site project and name it C:\ASPNET20\chap01-ClientScript.

Double-click the default Web Form to switch to the code-behind.

In the Form_Load event, insert a client script onto the page using the RegisterClientScriptBlock( ) method. The following example inserts a JavaScript code that displays the current time on the server. The time will be displayed in a window, as shown in Figure 1-17.

Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
'---inserting client-side script
Dim script As String = _
"alert('Time on the server is " & Now & "');"
Page.ClientScript.RegisterClientScriptBlock( _
Me.GetType, "MyKey", script, True)
End Sub


--------------------------------------------------------------------------------
Tip: The RegisterClientScriptBlock( ) method is also available in ASP.NET 1.x and appears under the Page class. However, its namespace has changed in ASP.NET 2.0 and appears under the ClientScriptManager class. You can get an instance of the ClientScriptManager class via Page.ClientScript.
--------------------------------------------------------------------------------


Figure 1-17. Executing a client script




The parameters of the RegisterClientScriptBlock( ) method are:




Type

The type of the calling page




Key

The key to identify the script




Script

Content of the script to be sent to the client side




AddScript

Indicates whether the script should be enclosed within a <script> block. The script generated on the client-side would look like this:

<script type="text/javascript">
<!--
alert('Time on the server is 7/18/2004 12:07:23 PM');//
-->
</script>


If the AddScript parameter is set to False, the script will not be executed on the client side; instead, it simply will be shown on the web page.


--------------------------------------------------------------------------------
Tip: Note that in ASP.NET 1.1 the key could be an empty string (but not null). In ASP.NET 2.0, an empty string supplied for the key will generate a runtime error.
--------------------------------------------------------------------------------

1.5.2. What about...
...including a script file?

Instead of inserting strings of client-side script into your application, you might have a much more sophisticated client-side application that is saved in a separate file. In this case, it is more effective for you to include the file directly rather than insert the scripts line by line.

Suppose you have a script file saved as hello.js and its content contains JavaScript code (without the <script> tag):

alert("Hello world, from JavaScript");


You can include this script in your page through the RegisterScriptInclude( ) method:

'--including script files
Dim scriptURL As String = "./hello.js"
Page.ClientScript.RegisterClientScriptInclude( _
Me.GetType, "MyKey", scriptURL)


The generated output looks like this:

<script src="./hello.js" type="text/javascript"></script>

javad3151
یک شنبه 25 دی 1384, 10:59 صبح
از این کد هم می تونید استفاده کنید:


if not ispostback then
response.write("<script> .....</script>"(
end if

Behrouz_Rad
یک شنبه 25 دی 1384, 19:16 عصر
من میخواستم اینکار رو به این خاطر انجام بدم که فقط یک بار popup باز بشه یعنی از IsPostBack استفاده کنم ولی اینطوری نشد و هر بار این popup باز میشه.میشه بگید اشتباهم کجاس؟

در این صورت، پس از PostBack، باید روال نسبت داده شده به تگ body رو با استفاده از متد Remove خاصیت Attributs حذف کنید.
روال Page_Load را به شکل زیر اصلاح کنید:


If Not IsPostBack Then
X.Attributes.Add("onload","myfunction();")
Else
X.Attributes.Remove("onload")
End If



در دات نت 2.0 امکانات جدید و جالبی در این زمینه ارائه شده

در متنی که ارسال فرمودید، چیز جدیدی مشاهده نمیشه!
متد RegisterClientScriptBlock در ASP.NET 1.1 نیز وجود دارد و در ASP.NET 2.0 تنها نیم اسپیس آن (همان طور که در متن، ذکر شده) تغییر یافته.

موفق باشید.

iekrang
دوشنبه 26 دی 1384, 12:34 عصر
معذرت میخواهم.کد شما کامل و بی عیب بود آقای راد.مشکل جای دیگه بود.
بازم ممنون.