Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取javascript中的控件_C#_Asp.net_Javascript_Visual Studio_Validation - Fatal编程技术网

C# 获取javascript中的控件

C# 获取javascript中的控件,c#,asp.net,javascript,visual-studio,validation,C#,Asp.net,Javascript,Visual Studio,Validation,我有几个只包含日期的文本框。如果一个文本框中有日期,并且用户尝试提交而未在另一个日期文本框中输入日期,则在提交之前将停止提交。我希望通过以下javascript函数实现这一点: function ClientValidate(sender, args) { // Get Both form fields var txtdate1 = document.getElementById('<%=txtdate1.ClientID%>'); var

我有几个只包含日期的文本框。如果一个文本框中有日期,并且用户尝试提交而未在另一个日期文本框中输入日期,则在提交之前将停止提交。我希望通过以下javascript函数实现这一点:

function ClientValidate(sender, args) {
       // Get Both form fields
       var txtdate1 = document.getElementById('<%=txtdate1.ClientID%>');
       var txtdate2 = document.getElementById('<%=txtdate2.ClientID %>');

    // do you client side check to make sure they have something
       if (txtdate1.value != '' && txtdate2.value == '') {

        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
    if (txtdate2.value != '' && txtdate1.value == '') {

        args.IsValid = false;

    }
    else {
        args.IsValid = true;
    }
}
这是验证器

    Dim custval As New CustomValidator
    custval.ID = "ValidPage"
    custval.ClientValidationFunction = "ClientValidate"
    custval.ErrorMessage = "You Must Enter a 'From' Date and a 'To' Date"
    custval.ErrorMessage = "You Must Select a Vendor"
    custval.SetFocusOnError = True
    custval.ControlToValidate = "txtdate1"
    custval.EnableClientScript = True
我的问题是javascript找不到这两个文本框,因为我是在代码中创建它们的
有什么想法吗?

在您的验证程序代码中尝试一下

custval.ControlToValidate = txtdate1.ClientID

我认为除了按照Chackey的建议将验证器绑定到控件之外,还需要在代码中生成脚本,以获取对控件的引用

问题是,您试图添加对控件的引用,该控件在站点运行之前不存在。换句话说,您正在尝试在日期文本框存在之前创建javascript以引用日期文本框(在生成页面之前它们不存在)。因此,这些行:

`var txtdate1 = document.getElementById('<%=txtdate1.ClientID%>');       
`var txtdate2 = document.getElementById('<%=txtdate2.ClientID %>');
这将创建两个javascript变量txtdate1和txtdate2,可用于引用日期元素


区别在于查找控件的脚本是在创建控件的同时在代码中生成的

在创建日历控件的同时,使用ClientScript管理器注册javascript

我不知道在哪里定义控件,但它们应该在page类中定义为受保护的属性,如下所示:

Protected txtdate1 As New TextBox
Protected txtdate2 As New TextBox

Protected bttndate1 As New ImageButton
Protected bttndate2 As New ImageButton

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

    bttndate1.ID = "bttndate1"
    Page.Form.Controls.Add(bttndate1)

    txtdate1.ID = "txtdate1"
    txtdate1.Width = 65
    Page.Form.Controls.Add(txtdate1)

    Dim calex1 As New AjaxControlToolkit.CalendarExtender
    calex1.TargetControlID = "txtdate1"
    calex1.Format = "MM/dd/yyyy"
    calex1.PopupButtonID = "bttndate1"

    '**************** date box2 ***************

    bttndate2.ID = "bttndate2"
    bttndate2.Style.Add("cursor", "pointer")
    Page.Form.Controls.Add(bttndate2)

    txtdate2.ID = "txtdate2"
    txtdate2.Width = 65
    Page.Form.Controls.Add(txtdate2)

    Dim calex2 As New AjaxControlToolkit.CalendarExtender
    calex2.TargetControlID = "txtdate2"
    calex2.Format = "MM/dd/yyyy"
    calex2.PopupButtonID = "bttndate2"


    MyBase.OnLoad(e)
End Sub

我是客户经理的对手。上述解决方案可能会起作用,但另一种方法是在ClientValidate()方法之外用JavaScript声明几个变量

将文本放在页面的某个位置(只要它具有runat=“server”属性,就可以将其放在任何位置)


这并不比上面的想法好或坏,这只是一种不同的方式。

你能给我更多的细节吗?我不确定我是否完全理解,但它似乎是合法的。将解释更改为(希望)更清楚的解释。
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "var txtdate1 = document.getElementById('" + txtdate1.ClientID + "');", true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "var txtdate2 = document.getElementById('" + txtdate2.ClientID + "');", true);
Protected txtdate1 As New TextBox
Protected txtdate2 As New TextBox

Protected bttndate1 As New ImageButton
Protected bttndate2 As New ImageButton

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

    bttndate1.ID = "bttndate1"
    Page.Form.Controls.Add(bttndate1)

    txtdate1.ID = "txtdate1"
    txtdate1.Width = 65
    Page.Form.Controls.Add(txtdate1)

    Dim calex1 As New AjaxControlToolkit.CalendarExtender
    calex1.TargetControlID = "txtdate1"
    calex1.Format = "MM/dd/yyyy"
    calex1.PopupButtonID = "bttndate1"

    '**************** date box2 ***************

    bttndate2.ID = "bttndate2"
    bttndate2.Style.Add("cursor", "pointer")
    Page.Form.Controls.Add(bttndate2)

    txtdate2.ID = "txtdate2"
    txtdate2.Width = 65
    Page.Form.Controls.Add(txtdate2)

    Dim calex2 As New AjaxControlToolkit.CalendarExtender
    calex2.TargetControlID = "txtdate2"
    calex2.Format = "MM/dd/yyyy"
    calex2.PopupButtonID = "bttndate2"


    MyBase.OnLoad(e)
End Sub
var textdate1;
var textdate2;
<asp:Literal ID="litJSVars" runat="server" />
this.litJSVars.Text = "textdate1 = document.getElementById('" + txtdate1.ClientID + "');\\n";
this.litJSVars.Text += "textdate2 = document.getElementById('" + txtdate2.ClientID + "');";