最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to send clientid of asp.net control via JavaScript - Stack Overflow

programmeradmin0浏览0评论

I am trying to send the control id on button click in my following asp 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('<%=empid.ClientID%>')"/>

and Javascript is:

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.

I am trying to send the control id on button click in my following asp 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('<%=empid.ClientID%>')"/>

and Javascript is:

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.

Share Improve this question edited Mar 3, 2016 at 13:55 Kiquenet 15.1k36 gold badges151 silver badges247 bronze badges asked Jul 3, 2012 at 8:59 user1465587user1465587
Add a ment  | 

4 Answers 4

Reset to default 1

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...

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)

You can send client id of empid textbox to javascript this way.

    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

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

Try this to get ID

Isn't it simplier to alert ID itseld?

alert('<%=empid.ClientID%>');
发布评论

评论列表(0)

  1. 暂无评论