javascript 如何通过JavaScript发送asp.net控件的clientid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11307609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send clientid of asp.net control via JavaScript
提问by Rohit
I am trying to send the control id on button click in my following asp.net code:
我正在尝试在我的以下 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%>')"/>
and Javascript is:
和 Javascript 是:
function checkEmpid(id){
var idValue = document.getElementById(id).value;
alert(idValue);
return false;
}
In alert I am getting null while when I use following code:
在警报中,当我使用以下代码时,我得到了空值:
<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;
}
In alert I got value entered in text box. Please help me in solving this problem I want to send clientid of control as parameter for JS.
在警报中,我在文本框中输入了值。请帮我解决这个问题我想发送控件的客户端 ID 作为 JS 的参数。
回答by Adil
You can send client id of empid textbox to javascript this way.
您可以通过这种方式将 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;
}
or
或者
You can get client ID by '<%=empid.ClientID%>'
this way
您可以通过'<%=empid.ClientID%>'
这种方式获取客户端ID
function checkEmpid(){
var id = '<%=empid.ClientID%>';
alert("This is id of TextField with ID=empid >> "+id);
return false;
}
回答by freefaller
If you definitely want to pass the UserControl name via a parameter, I believe your only option would be to set the function call in the code-behind...
如果您确实想通过参数传递 UserControl 名称,我相信您唯一的选择是在代码隐藏中设置函数调用...
C#...
C#...
abc.OnClientClick = string.Format("return checkEmpid('{0}');", empid.ClientID);
VB.NET...
网络...
abc.OnClientClick = String.Format("return checkEmpid('{0}');", empid.ClientID)
(Updated, as I realised you wanted the TextBox ID, not the UserControl ID)
(更新,因为我意识到你想要 TextBox ID,而不是 UserControl ID)
回答by Rohit
alert('<%=empid.ClientID%>');
Try this to get ID
试试这个来获取 ID
回答by Johnny_D
Isn't it simplier to alert ID itseld?
提醒 ID 本身不是更简单吗?
alert('<%=empid.ClientID%>');