我在aspx页面中使用多个aspx用户控件实例时遇到问题。当我尝试通过Java脚本获取User控件元素值时会发生这种情况。
用户控制代码:
<script type="text/javascript">
function ucFun()
{
var a = document.getElementById("<%=tbName.ClientID%>");
alert(a.value);
return false;
}
</script>
<asp:Label Text="Name" runat="server" ID="lblname"></asp:Label>
<asp:TextBox ID="tbName" runat="server" ></asp:TextBox>
<asp:Button ID="btSubmit" runat="server" Text="Go" OnClientClick="ucFun()" />
网络表单代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:cont runat="server" ID="ucID" />
<uc:cont runat="server" ID="Cont1" />
<uc:cont runat="server" ID="Cont2" />
</div>
</form>
</body>
</html>
单击Go按钮,它会显示null int he alert box,因为元素未定义。 但是,当我在表单中有一个用户控件实例时,它正确显示了文本框值。
我们有什么方法可以写这个......
答案 0 :(得分:5)
经过一番搜索后找到了不同的解决方案。
在这里,他们使用javascript名称附加uniqueID,因此与javascripts没有冲突。
<强> UserControl.CS 强>
protected string uniqueKey;
protected void Page_Load(object sender, EventArgs e)
{
this.uniqueKey = Guid.NewGuid().ToString("N");
this.Button1.Attributes["onclick"] = "return DisplayMessage_" + uniqueKey + "();";
}
<强> UserControl.aspx 强>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UC_TestCS.ascx.cs" Inherits="UC_TestCS" %>
<script type ="text/javascript">
function DisplayMessage_<%=uniqueKey %>() {
var message = document.getElementById("<%=TextBox1.ClientID %>").value;
alert(message);
return false;
}
</script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Show Message" />
答案 1 :(得分:0)
请参阅帖子中的类似问题
Javascript functions inside ASP.NET User Control
您需要使用ClientScriptManager,如下例所示
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
String csname2 = "ButtonClickScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
String cstext1 = "alert('Hello World');";
cs.RegisterStartupScript(cstype, csname1, cstext1, true);
}
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
{
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
cstext2.Append("Form1.Message.value='Text from client script.'} </");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
</form>
</body>
</html>