1

我创建了一个具有一些自定义客户端验证的用户控件。我通过 RegisterStartupScript 嵌入 Javascript,并通过 RegisterExpandoAttribute 将信息传递给验证。但是,用户控件在 PageLoad 上不可见,当我使用 document.getElementById 时,我得到空值。

这是我当前的代码:

    public void Page_Load(object sender, EventArgs e) 
    { 
            ClientScriptManager cs = Page.ClientScript; 
            Type cstype = this.GetType(); 
            if (!cs.IsStartupScriptRegistered(cstype, "ValidatorType")) 
            { 
                String DateValidator; 
                DateValidator = "<script type=\"text/javascript\">\n"; 
                DateValidator += "function ValidateDate(source, args) {\n"; 
                DateValidator += "   var ddDay = document.getElementById(source.day);\n"; 
                DateValidator += "   var day = ddDay.selectedIndex;\n"; 
                DateValidator += "   var ddMonth = document.getElementById(source.month);\n"; 
                DateValidator += "   var month = ddMonth.selectedIndex;\n"; 
                DateValidator += "   var ddYear = document.getElementById(source.year);\n"; 
                DateValidator += "   var year = ddYear.selectedIndex;\n"; 
                DateValidator += "   if (day == 0 || month == 0 || year == 0)\n"; 
                DateValidator += "      args.IsValid = false;\n"; 
                DateValidator += "   else\n"; 
                DateValidator += "      args.IsValid = true;\n"; 
                DateValidator += "   }\n"; 
                DateValidator += "</script>"; 
                cs.RegisterStartupScript(cstype, "ValidatorType", DateValidator); 
            } 

            cs.RegisterExpandoAttribute(reqDueDate.ClientID, "month", ddMonth.ClientID); 
            cs.RegisterExpandoAttribute(reqDueDate.ClientID, "day", ddDay.ClientID, false); 
            cs.RegisterExpandoAttribute(reqDueDate.ClientID, "year", ddYear.ClientID, false); 
    }

我收到的错误是:

Uncaught TypeError: Cannot set property 'month' of null 
Uncaught TypeError: Cannot read property 'selectedIndex' of null 
4

2 回答 2

1

在服务器端不可见的控件不会出现在服务器生成的客户端上的 html 中。所以它应该是空的。使用 javascript 使其不可见隐藏,并在需要时使用 javascript 使其再次可见。

该脚本应在控件上呈现后执行。

在客户端隐藏服务器控件

document.getElementById('<%= pnlMail.ClientID %>').style.display = 'none'; 

在客户端隐藏 HTML 控件

document.getElementById('htmlControlID').style.display = 'none'; 
于 2012-07-03T16:19:58.510 回答
0

一个更“服务器端”的方法是在服务器上设置值,然后你不依赖于 javascript。澳交所:

    <asp:TextBox runat="server" ID="uxToHide" />

代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        uxToHide.Style.Add("display", "none");
    }

使用 PlaceHolders 会增加一点复杂性,但还不错。澳交所:

    <asp:PlaceHolder ID="uxToHide" runat="server" />

代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox tb = new TextBox();
        tb.Style["display"] = "none";
        uxToHide.Controls.Add(tb);

        ShowChildren(uxToHide);
    }

    public static void HideChildren(Control container)
    {
        if (container == null || container.Controls == null || container.Controls.Count == 0)
        {
            return;
        }
        foreach (Control c in container.Controls)
        {
            if (c is WebControl)
            {
                ((WebControl)c).Style["display"] = "none";
            }
            HideChildren(c);
        }
    }

    public static void ShowChildren(Control container)
    {
        if (container == null || container.Controls == null || container.Controls.Count == 0)
        {
            return;
        }
        foreach (Control c in container.Controls)
        {
            if (c is WebControl)
            {
                WebControl wc = (WebControl)c;
                if (wc.Style["display"] == "none")
                {
                    wc.Style.Remove("display");
                }
            }
            ShowChildren(c);
        }
    }
于 2012-07-03T17:24:11.417 回答