使用asp.net C#获取文本框值

时间:2014-02-14 07:07:58

标签: javascript asp.net

在下面的代码中我有一个文本框和一个隐藏字段。我的目标是获取文本框值和隐藏字段。我可以获取隐藏字段值。但我无法获取文本框值。请任何人帮我解决问题。 代码背后:

string strRegularExp = string.Empty;

                    if (ListOfRegularExpression != null)
                    {
                        for (int iRow = 0; iRow < ListOfRegularExpression.Count; iRow++)
                        {
                            strRegularExp += ListOfRegularExpression[iRow].ToString() + "~~";
                            hidRegExp.Value = strRegularExp;
                            if (iRow == ListOfRegularExpression.Count - 1)
                            {
                                strRegularExp = strRegularExp.TrimEnd("~~".ToCharArray());
                                txtField.Attributes.Add("onblur", "javascript:ValidateRegExp('" + txtField.Text + " ," + hidRegExp.Value + "');");//textbox value to get
                            }

                        }
                    }

                    hidRegExp.Value = strRegularExp;
                  //  txtField.Text = strRegularExp;
                    lbl.Text = "The value of the HiddenField control is " + hidRegExp.Value + "."; 
                }

代码:JS

function ValidateRegExp(txtInput) {
        //alert(txtInput);
        var mySplitInput = new Array();
        mySplitInput = txtInput.split(",");


       var hiddenValue = document.getElementById("<%=hidRegExp.ClientID%>").value;
        alert(txtInput);
        var mySplitResult = new Array();
        mySplitResult = RegExp.split("~~");

        for (i = 0; i < mySplitResult.length; i++) {
            //document.write("<br /> Array[" + i + " ]= " + mySplitResult[i]);
            var re = new RegExp(mySplitResult[i]);
            if (txtInput.match(re)) {
               // alert("Successful match");
            } else {
               // alert("No match");
            }

        }

    }
 <asp:TextBox ID="txtField" runat="server" ></asp:TextBox>
<asp:HiddenField ID="hidRegExp" runat="server" >
</asp:HiddenField >

1 个答案:

答案 0 :(得分:0)

你已经声明了两次变量hiddenValue Javascript在不停止程序的情况下引发了错误,因此你的脚本无效。

   var hiddenValue = document.getElementById(mySplitInput[1]);
    // alert(mySplitInput[1]);

   var hiddenValue = document.getElementById("<%=hidRegExp.ClientID%>").value;