带有asp.net 3.5 Web应用程序的JavaScript [英] javaScript with asp.net 3.5 web applcation
本文介绍了带有asp.net 3.5 Web应用程序的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试学习Javascript.我添加了一个在e按钮上添加的Web表单.从后面的代码中,我尝试在按钮的客户端单击上添加javascript函数,代码如下:
I am trying to learn Javascript. I have add one web form on which i added on e button. From code behind I tried to add javascript function on button''s client click code is as follows:
string javascriptString = @"<script type='text/javascript'>
function showmsg()
{
var msg='are you want to countinue';
return confirm(msg);
}
";
Type t = this.GetType();
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsClientScriptBlockRegistered(t, "blockScript"))
{
cs.RegisterClientScriptBlock(t, "blockScript", javascriptString);
}
Button1.Attributes.Add("OnClientClick", "showmsg()");
此代码写在Page_load方法上.
但是当我运行时,Web应用程序的按钮无法呈现,请帮帮我
aspx页面是:
this code is written on Page_load method.
but when i am runnig the web application Button doesnt get rendered please help me out
aspx page is:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="testRSSFeed.WebForm4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
推荐答案
这是不正确的:
Button1.Attributes.Add("OnClientClick", "showmsg()");
相反,请尝试:
Button1.Attributes.Add("onclick", "showmsg()");
这将根据您的需要将客户端onclick绑定在一起.
还有一件事,在您的javascriptString
中,使用</script>关闭字符串. (看起来现在好像丢失了.)
This is incorrect:
Button1.Attributes.Add("OnClientClick", "showmsg()");
Instead, try:
Button1.Attributes.Add("onclick", "showmsg()");
This would tie the client onclick as per your need.
One more thing, in yourjavascriptString
, close the string with </script> (looks like missing right now.)
您忘记关闭< script>标签,并且不要在脚本属性中使用OnClientClick.请使用onclick.
或通过标记实现
You forget to close <script> tag and don''t use OnClientClick in script attribute use onclick.
or achive this by markup
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="testRSSFeed.WebForm4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return confirm('Are you want to continue?')" />
</div>
</form>
</body>
</html>
--Pankaj
--Pankaj
这篇关于带有asp.net 3.5 Web应用程序的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文