验证使用JavaScript asp.net的textBox
我有我的网页上一个asp.net TextBox控件,每当用户点击搜索按钮,我想验证为空白文本框搜索按钮。我想使用的OnClientClick事件,并传递参数作为我的JavaScript函数将要被从外部JS调用。
I have an asp.net TextBox control on my page and a search button whenever user clicks on the search button i want to validate the TextBox for Blank. i want to use a onClientClick event and pass the parameter as my Javascript function will be going to be called from external JS.
下面是我的尝试。
<asp:TextBox ID="search" runat="server">
</asp:TextBox>
<script language="javascript" type="text/javascript">
function voidsearch(s) {
alert(document.getElementById(s).value);
}
</script>
<asp:ImageButton ID="img1" runat="server" ImageUrl="Dotnetnuke.ico"
OnClientClick="voidsearch('<%= search.ClientID %>'); return false;" />
但这是抛出错误。所需的对象。我也通过this.search ..但同样的错误。我不明白,因为我已经事先声明控件,然后叫其ID为什么我收到此错误。
but this is throwing error. Object Required. i also passed this.search.. but same error. i dont understand why i am getting this error as i have first declared control and then called its ID.
请任何人能帮助我的。
看来,搜索文本框被放到作INamingContainer,所以它是ClientID属性无法评估。使用的方法,在infragistics得到下拉的clientID的在Rowedit模板螺纹:
It seems that the "search" TextBox is placed onto INamingContainer, so it is ClientID property cannot be evaluated. Use the approach, described in the infragistics get clientID of dropdown in Rowedit template thread:
<asp:TextBox ID="search" runat="server" OnInit="search_Init"></asp:TextBox>
<asp:ImageButton ID="img1" runat="server" ImageUrl="Dotnetnuke.ico"
OnClientClick="voidsearch();" return false;" />
protected void search_Init(object sender, EventArgs e) {
TextBox txt = (TextBox)sender;
string script = string.Format("var _{0} = document.getElementById('{1}');", txt.ID, txt.ClientID);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "ANY_KEY", script, true);
}
<script language="javascript" type="text/javascript">
function voidsearch() {
alert(_search);
alert(_search.value);
}
</script>