Link to home
Start Free TrialLog in
Avatar of zachvaldez
zachvaldezFlag for United States of America

asked on

Click a textbox and navigate to a page

What is the javascript code or other approaches if server side code won't do
to navigate to a page when a textbox is clicked since Textbox does not have a click event?
Avatar of Amar Bardoliwala
Amar Bardoliwala
Flag of India image

Hello zachvaldez,

why would you want to navigate on textbox click? It is not something that is used generally.

I am not much aware of .NET textboxes but in html text boxes click event is there.

Look at following link

http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events.aspx


Hope this will help you.

Thank you

Amar Bardoliwala
Avatar of leakim971
add this in the head section of the page :
<script type="text/javascript">

window.onload = function() {
   document.getElementById("<%= TextBox1.ClientID %>").onclick = function() {
            location.href = "http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27868878.html";
   }
}

</script>

Open in new window


where TextBox1 is the ID of your textbox

http://beyondrelational.com/modules/2/blogs/61/posts/11206/all-about-client-id-mode-in-aspnet-4.aspx
Avatar of zachvaldez

ASKER

there is a syntax error looking for a missing ";" somewhere..
not in my code... generally not in javascript
ok I found the error and it worked .However, If I set the textbox to readonly, this thing would would not work.
I don't want the ability to edit the textbox but can only be clicked and navigate to the url.
Is it possible to set textbox  not editable  with keyup event?
>If I set the textbox to readonly, this thing would would not work.

That's wrong until you're using .net to do that....

use readonly="readonly"

<asp:TextBox id="TextBox1" runat="Server" ></asp:TextBox>

Open in new window


<script type="text/javascript">

window.onload = function() {
   document.getElementById("<%= TextBox1.ClientID %>").setAttribute("readonly", "readonly");
   document.getElementById("<%= TextBox1.ClientID %>").onclick = function() {
            location.href = "http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27868878.html";
   }
}

</script>

Open in new window

Is it possible to add another textbox "textbox2" for instance and navigate it to a diff. url?
I tried to do that but did not work.
Should I repeat the window.onload for it to work?
>Is it possible to add another textbox "textbox2" for instance and navigate it to a diff. url?

yes

>I tried to do that but did not work.

so post a link to you page or you code...
well I just add the code..

window.onload = function() {
   document.getElementById("<%= TextBox1.ClientID %>").setAttribute("readonly", "readonly");
   document.getElementById("<%= TextBox1.ClientID %>").onclick = function() {
            location.href = "https://www.experts-exchange.com/questions/27868878/Click-a-textbox-and-navigate-to-a-page.html";

 document.getElementById("<%= TextBox2.ClientID %>").onclick = function() {
            location.href = "school.aspx";

   }
}
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forgot to mention I also have a query string added at the end of

location.href = "school.aspx?hid=xxxxx"; for example

How will I add the querystring value?
create a new question...