Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding an onClick event to a texbox - What the hell…!!!

Status
Not open for further replies.

sunil128

Programmer
Mar 20, 2008
45
GB
Hi all im still all very new to this area so apologies in advance for the poor explanation of the problem.

My app is written VS2005,C# & ASP. I am trying to get an onClick event working with a textbox, but as many of you will probably know that no such event exist with textboxes (which seems silly to me btw). Anyway after trawling thru the internet it appears what I need to do I use the attributes collection to add an “onClick” event to the text box

For example:
Code:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            this.txtDealtWith.Attributes.Add("onClick", "txtclick");

		…other code etc etc

		}
	  }

Now my understanding is that with this line I can then write a line in the aspx code to handle the onClick event.

Code:
<asp:TextBox ID="txtDealtWith" runat="server" onClick="txtDealtWith_onClick" ToolTip="Dealt With Date" AutoPostBack="True"></asp:TextBox>

Which then means I can write a method in the code behind (.cs) to handle txtDealtWith_onClick.

Code:
protected void txtDealtWith_OnClick(object sender, EventArgs e)
        {

        }


But when I step through the code, clicking on the textbox does nothing, it does not hit the code I have written. What have I done worong? Am I missing something? Do I still need javascript in the aspx?

Also I do not understand what the second part of the Add.Attributes statement is doing? i.e What does “txtclick” (in this instance) actually refer to?

Code:
txtDealtWith.Attributes.Add("onClick", "txtclick");

Please help cus this is driving me mad!

Thanks in advance.
 
the attributes you are adding are related to client code (js) not server code (c#).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for your response jmeckley, however can you expand on that comment a bit more, i kinda know what you mean, but i am a newbie newbie!

And can you also suggest a solution?
 
with web programming you have client code (javascript, css, html dom) and server code (c#, vb, compiled objects, etc).

asp.net is a framework, not a programming language. I say this because some developers confuse the 2. thinking they write code is asp.net. you write code is c#/vb. asp.net is just a framework to receive requests and send responses.

webforms is an "engine", which runs on top of asp.net, for producing html. M$ pushes this method the most. (there are other html "engines" which run on the asp.net framework, but that's another post.)

---

when you are adding attributes to a textbox you are manipulating the client DOM. this does nothing for the TextBox server control.

the thing about webforms is it works well when you use it the way M$ thinks you should use it. and it's near impossible to use it any other way. Textboxes don't have an on click event. to do you need to wire up some js code which would emulate a postback and have code on the server to handle the request.

It's not impossible, but it's not pretty. google a phrase like "asp.net textbox onclick postback" as a starting point for research.

before you go too much further i would recommend researching 1. how the web works (get/post, statelessness, forms, request/response, etc)
2. how asp.net works asp.net life cycle, postback, viewstate, webforms.
3. start simple (textbox and button) then more to more advanced user interaction (onclick for textbox).

if you get into database interactions I would recommend avoiding the DataSource controls offered in webforms. they appear to be convenient and simple, but they can make life difficult when the "don't work".

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top