在 aspx 页面中使用多个 aspx 用户控件实例化

4

我在使用一个aspx用户控件的时候遇到了问题,当我尝试通过JavaScript获取用户控件元素的值时,这个问题就出现了。如果在一个aspx页面中使用多个实例的用户控件,也会出现这个问题。

用户控件代码:

<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,因为该元素未定义。然而,在表单中只有一个用户控件实例时,它会正确地显示文本框的值。

是否有任何方法可以解决这个问题..


1
当您添加第二个控件时,由于您的函数名称不改变,因此会覆盖 ucFun 的第一个版本。 - Rubens Farias
感谢Rubens的回复。如果我需要验证UC字段,那么我必须在获取UC字段值时编写JS。使用多个UC实例将会失败。我们该如何实现呢? - Muthukumar Palaniappan
2个回答

10

在搜索一番后找到了一种不同的解决方案。

http://www.aspsnippets.com/Articles/Issue-JavaScript-in-WebUserControl-not-working-when-used-multiple-times-on-same-page.aspx

他们在JavaScript名称后附加了一个唯一的ID,这样就没有JavaScript冲突了。

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" />

这个可以工作,但当内容页有回发时就不行了,因为uniqueKey被更改了。 - hojjat.mi
这对我帮助很大。谢谢。 - Diane
感谢您的提示,@Muthukumar。使用'this.ClientID',即用户控件的ClientID,而不是您建议的uniqueKey,我得到了相同的结果。毕竟,它是唯一的并且已经为您生成。 - CAK2

0

请参考帖子中的类似问题

ASP.NET用户控件中的Javascript函数

您需要像下面的示例一样使用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>

我知道我们可以通过从服务器端注册脚本来限制脚本的注册。然而,我们无法获取实例的适当字段值,因为我们只为一个实例注册一个脚本。我的意思是,显示 <%=tbName.ClientID%> 将返回用户控件实例的适当字段值。 - Muthukumar Palaniappan
这并没有解决问题所提到的问题,它是一个完全不同的话题。 - jtate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接