验证使用JavaScript asp.net的textBox [英] validating asp.net textBox using Javascript

查看:222
本文介绍了验证使用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属性无法评估。使用的方法,在<一个描述href=\"http://stackoverflow.com/questions/5364426/infragistics-get-clientid-of-dropdown-in-rowedit-template/5364489#5364489\">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>

这篇关于验证使用JavaScript asp.net的textBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆