Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried this sample code but this is not correct...
ASP.NET
<div>
        <asp:TextBox ID="TextBox1" runat="server" />
        <asp:Button ID="Button1" OnClientClick="getControlID('<%=TextBox1.ID %>');" runat="server" Text="Click Here" />
        <asp:TextBox ID="TextBox2" runat="server" />
        <asp:Button ID="Button2" OnClientClick="getControlID('<%=TextBox2.ID %>');" runat="server" Text="Click Here" />
</div>

JavaScript
<script type="text/javascript">
        function getControlID(obj) {
            var txtControl = document.getElementById(obj);
            txtControl.value = "It's working!";
            return false;
        }
    </script>

When I click Button1 then I want to display "It's working!" message in TextBox1 or when I click Button2 then I want to display that message in TextBox2 with using single function.
Posted
Comments
Thomas Daniels 2-Nov-12 9:19am    
What do you mean with 'is not correct'?
Do you get an error, or there happens nothing?

Your code is almost there. Make the following slight alteration:

HTML
<%=TextBox1.ClientID %>

<%=TextBox2.ClientID %>


instead of:

HTML
<%=TextBox1.ID %>

<%=TextBox2.ID %>


More info about ClientID:

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.100%29.aspx[^]
 
Share this answer
 
v2
Comments
Dinesh Ambaliya 2-Nov-12 9:46am    
Thanks, but problem is that getControlID('<%=TextBox1.ClientID %>') javascript consider it to string!
jim lahey 2-Nov-12 9:49am    
of course, but you pass that value to getElementById, which expects a string.
Dinesh Ambaliya 2-Nov-12 9:47am    
I have not used master page. It's just example for testing.
jim lahey 2-Nov-12 9:49am    
I never mentioned a master page.
Dinesh Ambaliya 2-Nov-12 9:56am    
I mean getControlID('<%=TextBox1.ClientID %>') will pass '<%=TextBox1.ClientID %>' string in function not control id.
The problem is TextBox2.ID returns the sever-side ID of the control, not the one rendered to the browser. If you open you page in Chrome , right click the button and select "Inspect Element" or view the source code, you'll see what I mean.



You could do this:

ASP.NET
<asp:Button ID="Button1" OnClientClick="getControlID('<%=TextBox1.ClientID %>');" runat="server" Text="Click Here" />
<asp:TextBox ID="TextBox2" runat="server" />
<asp:Button ID="Button2" OnClientClick="getControlID('<%=TextBox2.ClientID %>');" runat="server" Text="Click Here" />


To get the client ID.

Personally, I think the following is simpler ignoring any errors I have in the code:

ASP.NET
<div>
        <asp:TextBox ID="TextBox1" runat="server" />
        <asp:Button ID="Button1" OnClientClick="getControlID(this);" runat="server" Text="Click Here" />
        <asp:TextBox ID="TextBox2" runat="server" />
        <asp:Button ID="Button2" OnClientClick="getControlID(this);" runat="server" Text="Click Here" />
</div>
<script type="text/javascript">
        function getControlID(control) {
            control.value = "It's working!";
            return false;
        }
    </script>

Assuming I've got it right (I haven't tested the code) the button itself will be passed, removing the need for the GetById.

On o further point, you sould take a look at jquery: it really simplifies client work.
 
Share this answer
 
Comments
Dinesh Ambaliya 2-Nov-12 9:49am    
Thanks for your answer. I have not used master page. And you second example you declared 'this' keyword which refer to button itself I want textbox id.
Satyendra Kumar(Analyst Programmer) 2-Nov-12 16:09pm    
Hi,
If you want id of the control then you can use this.id in the above example...
Keith Barrow 2-Nov-12 9:56am    
The this keyword code is just a suggestion, which is why I added it second, it will perform faster as long as you are acting on the control being passed.

As for the masterpage, this is only partially relevant to the client ID (if I understand what you mean) as it might still be different even if there is not a master page in user.
See:
http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx
for an explanation of how the client side ID is built up, and the options you to select how the server id becomes the client id.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900