Reputation:
I am trying to send the control id on button click in my following asp.net 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.
Upvotes: 0
Views: 12115
Reputation: 148180
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;
}
Upvotes: 1
Reputation: 19963
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)
Upvotes: 1
Reputation: 4652
Isn't it simplier to alert ID itseld?
alert('<%=empid.ClientID%>');
Upvotes: 0