Results 1 to 8 of 8

Thread: ASP.NET and SetFocus...???

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Question ASP.NET and SetFocus...???

    In vb6 I would do:
    VB Code:
    1. If txtUsername.Text = vbNullstring Then
    2.    MsgBox "Please enter a correct woof!"
    3.    txtUsername.SetFocus
    4. End If
    But how do I do this on a web ASP.NET page?
    The txtUsername doesn't have a SetFocus method

    Woka

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    You would have to use client side script.

    (assuming you do validation on a button click)

    page script:
    Code:
    <script>
    	function IsEmpty()
    	{
    		if(document.getElementById("TextBox1").value == '')
    		{
    			alert('You must enter text!');
    			document.getElementById("TextBox1").focus();
    		}
    		
    		return false;
    	}
    </script>
    vb form code:
    VB Code:
    1. If Not Page.IsPostBack Then
    2.     Button1.Attributes.Add("OnClick", "script:return IsEmpty()")
    3. End If
    You have to be sure and return false from the script function to prevent postback.

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    You are kidding right?
    I am writing this using .NET 2003, and it doesn't have a setfocus method? Is that correct? If so, how bloody stupid

    *sigh*

    *sulk*

    Woka

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    You are not understanding how client/server side processing works.

    ASP.NET pages are stateless. How do you expect your server to remember that you want to set the focus to 'TextBox1' after the page has been posted?

    Whenever your page is posted back it can't possibly remember the value of a control unless you save the value to Session or Viewstate. That bogs down your server. You don't have to wait on the server to postback if you use a script.

    Why do you hate Javascript? Just about every custom control I have ever seen uses it at some point.

  5. #5

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    I am learning .NET

    I don'[t want to have to learn java at the same time, .NEt is bad enough on it's own

    But if it's the only way...oh, can it be done using vbscript?

    Woof

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    I know how you feel

    It took me a while to get used to the difference between client and server side processing. I hated the idea of Javascript. But you really have to learn very little script. I usually just search the net to find a script I need.

    I don't know if it's possible in VB script. It should be but I go with Javascript because it's used a lot more frequently.

  7. #7
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    Originally posted by Wokawidget
    I am learning .NET

    I don'[t want to have to learn java at the same time, .NEt is bad enough on it's own

    But if it's the only way...oh, can it be done using vbscript?

    Woof
    Try web validation controls. That gives your users a visual cue/explanation (depends how you set it up) on what's wrong with their entries without postback.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    JavaScript is not very difficult to learn. It's small, and simple. Just a couple lessons and you'll be familiar enough to accomplish what you need.

    Btw, I found that knowing ASP 3.0 and Javascript/html before hand was very helpful in understanding the entire concept of ASP.NET.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width