ASP.NET question

Alphathree33

Platinum Member
Dec 1, 2000
2,419
0
0
Okay, I've been doing ASP development for nearly ten years.

Lately, I've started doing some ASP.NET stuff.

Now I have a REALLLLLLLLLLY simple idea that I can't seem to implement, and so I'm here.

Scenerio:
User clicks button (and for the sake of argument, let's say it's very difficult to capture this particular click at the client-level).
My C# code handles button-click at the server level.

In the C# code, I'd like to add a textbox to the page, give it a certain background color, and add focus to it on the client side.

Now obviously I know how to change the background color of a textbox, and I know how to use the JavaScript focus() function.

But what I'm confused about is how to trigger a JavaScript function on the client side via some server-side code. Any ideas? (I can think of a number of hacks, but I'm imaginging there must be a clean way to do this.)
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Aren't you spewing a brand new html page when you do server side processing? Why can't you add onload=textbox.focus() to you document object? Or simply set the focus attribute of your object.
 

ArmchairAthlete

Diamond Member
Dec 3, 2002
3,763
0
0
Give me a second, I did this on a page at work the other day. I don't have it bookmarked at home.

You can make a sub that does something like Page.SetStartupScript("javascript goes here").


EDIT: It's Page.RegisterStartupScript()

 

Spydermag68

Platinum Member
Apr 5, 2002
2,616
99
91
From the Web Form drag and drop a new button. (Not an html button)

Right click the button and click on Run at server

or

Show the html code, find the button and add runat=sever, go back to the form view

Double click the button to goto the code behind the page.

The function name sould be something like: button_click ()

In this function change the background color, and make the textbox visible.

 

SALvation

Senior member
Apr 10, 2001
964
0
0
This is what I do in ASP.NET with VB:

I have a button on the aspx page named "btnSubmit" and a javascript function named "checkform".

In the code behind in the "Page_Load" sub routine I have the following line:

btnSubmit.Attributes.Add("onclick", "return checkform();")

Works flawlessly.
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
I do something very similar to armchair's code above. Our users have requested that we always focus on the first field, or if there is an error on a certain field, focus on that one. So I always register the startup script, and have a property that specifies which control to focus on.

As far as background color, I recommend using a stylesheet class for that. What we do is make the background slightly tan/pink and put a red border around the input area. I used to prefer making the label red, but the red border really works quite well.