如何通过JavaScript发送asp.net控件的clientid

时间:2012-07-03 08:59:51

标签: javascript asp.net

我试图在以下asp.net代码中点击按钮发送控件ID:

  <asp:TextBox ID="empid" runat="server" CssClass="input_box" onFocus="if (this.value == this.title) this.value='';" onBlur="if (this.value == '')this.value = this.title;" value="Enter employee ID" title="Enter employee ID" onClick="changecol(this)"></asp:TextBox>

 <asp:Button ID="abc" runat="server" Text="xxx" 
        CssClass="submit_button" onclick="abc_Click" OnClientClick="return checkEmpid('<%=empid.ClientID%>')"/>

和Javascript是:

function checkEmpid(id){
var idValue = document.getElementById(id).value;
alert(idValue);
return false;
}

在警报中,当我使用以下代码时,我将变为null:

<asp:TextBox ID="empid" runat="server" CssClass="input_box" onFocus="if (this.value == this.title) this.value='';" onBlur="if (this.value == '')this.value = this.title;" value="Enter employee ID" title="Enter employee ID" onClick="changecol(this)"></asp:TextBox>

 <asp:Button ID="abc" runat="server" Text="xxx" 
        CssClass="submit_button" onclick="abc_Click" OnClientClick="return checkEmpid()"/>

function checkEmpid(){
var idValue = document.getElementById('<%=empid.ClientID%>').value;
alert(idValue);
return false;
}

在警报中,我在文本框中输入了值。请帮我解决这个问题我想发送控件的clientid作为JS的参数。

4 个答案:

答案 0 :(得分:1)

您可以通过这种方式将empid文本框的客户端ID发送到javascript。

    abc.Attributes.Add("onclick", "return checkEmpid('" + empid.ClientID + "')");


    function checkEmpid(clientIdOfempid){      
        alert("This is id of TextField with ID=empid >> "+clientIdOfempid);
        return false;
    }

您可以'<%=empid.ClientID%>'这样获取客户ID

function checkEmpid(){
    var id = '<%=empid.ClientID%>';
    alert("This is id of TextField with ID=empid >> "+id);
    return false;
}

答案 1 :(得分:1)

如果您肯定想通过参数传递UserControl名称,我相信您唯一的选择是在代码隐藏中设置函数调用...

C#...

abc.OnClientClick = string.Format("return checkEmpid('{0}');", empid.ClientID);

... VB.NET

abc.OnClientClick = String.Format("return checkEmpid('{0}');", empid.ClientID)

(更新,因为我意识到你想要TextBox ID,而不是UserControl ID)

答案 2 :(得分:0)

警告ID本身不是更简单吗?

alert('<%=empid.ClientID%>');

答案 3 :(得分:0)

alert('<%=empid.ClientID%>');

尝试此获取ID