www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: clear a form in asp.net

  1. #1
    Join Date
    Aug 2004
    Posts
    5

    clear a form in asp.net

    Hi,

    I want to have a "Clear" Button for my form. I know in html you can put:

    <input type="reset" value="Clear Form" name="Reset" onclick="ResetControls(this.form)">

    It seems that this is a more complex case in asp.net. I am using using the validators that
    are in .net (i.e. RangeValidator, CustomValidator...). Does this make it more complex?
    Will the validator message reset wilt the control???

    What would be the best way to reset this form. I saw a post once (can't find it now)
    that said I could use Response.Redirect.

    Can I??? What is the correct syntax.

    Any help will be greatly appreciated.

  2. #2
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    To run a server side script the page has to be sent back to the server, so you might as well just redirect the user back to the form since his page will refresh anyway. That is the kind of cheating method, but it will work. I would use
    server.transfer("theformurl")
    when the reset button is clicked.

  3. #3
    Join Date
    Sep 2004
    Location
    Northeast, FL
    Posts
    332
    Maybe you could just use the onclick event of the reset button and use a function like this.

    sub reset_onClick()
    textbox1.text = ""
    'similar for rest of controls
    end sub

  4. #4
    Join Date
    Nov 2002
    Location
    Auburn, AL
    Posts
    9,222
    Originally posted by Cstick
    Maybe you could just use the onclick event of the reset button and use a function like this.

    sub reset_onClick()
    textbox1.text = ""
    'similar for rest of controls
    end sub
    You could, but the page would still be resent and you would have to type out every value, and reset radio buttons and check boxes.

  5. #5
    Join Date
    Sep 2004
    Location
    Northeast, FL
    Posts
    332
    Yeah, I suppose you are right. It would be much simpler to just redirect to the same page. In either case, a round trip is necessary.

  6. #6
    Join Date
    May 2003
    Posts
    599
    I'm new to ASP.NET, and I'm trying to do the same thing with my form. I tried adding "onserver.transfer" to the onClick event like this:
    Code:
    <asp:Button Text="Reset Form" onClick="onserver.transfer('contacts_edit.aspx')" runat="server"></asp:Button>
    ...but it doesn't reset the form. It results in this Compilation Error:
    BC30456: 'onserver' is not a member of 'ASP.contacts_edit_aspx'.

    Could you tell me what I'm doing wrong? Thanks for any help.
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

  7. #7
    Join Date
    Dec 2004
    Location
    Saint Petersburg, Florida
    Posts
    32
    In your HTML code leave this:

    <asp:Button Text="Reset Form" id="btnReset" runat="server"></asp:Button>


    In your code behind page write this:

    Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
    Me.Server.Transfer("contacts_edit.aspx")
    End Sub

    Good Luck!

    Just be carefull when using Server.Transfer. Here are some articles for a reference:

    http://www.developer.com/net/asp/article.php/3299641
    http://support.microsoft.com/?kbid=839521

  8. #8
    Join Date
    May 2003
    Posts
    599
    Wow, thanks for the speedy response dtch! I'll try this fix out asap.
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

  9. #9
    Join Date
    May 2003
    Posts
    599
    I tried that out, but received this error message:
    BC30506: Handles clause requires a WithEvents variable
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

  10. #10
    Join Date
    Dec 2004
    Location
    Saint Petersburg, Florida
    Posts
    32
    Hi,

    If you write your program in Microsoft Visual Studio.Net, when you add a web server button control, on your code behind page you get automatically the statement:

    Protected WithEvents btnReset As System.Web.UI.WebControls.Button

    If you don't have this line of code, your code will not work. Add this variable at the class level of your code behind page. If it doesn't work again, please, post some of your code, so we can have a better idea of what is going on.

    Good Luck!

  11. #11
    Join Date
    May 2003
    Posts
    599
    I'm actually using the ASP.NET Web Matrix GUI for my apps. I added that line of code before the other code to see what would happen, and received this error: BC30260: 'btnReset' is already declared as 'Protected Dim btnReset As System.Web.UI.WebControls.Button' in this class.

    Since I can't fit all of the code in this post with the limitations, I've created a text file at http://www.douglas-county.com/scriptlibrary/test.txt that contains the complete code for my test edit page. Thanks for any & all help.
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

  12. #12
    Join Date
    Dec 2004
    Location
    Saint Petersburg, Florida
    Posts
    32
    OK, try this:

    Don't declare the btnReset variable, because your code behind page resides within your html page.

    leave the function:

    Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Me.Server.Transfer("contacts_edit.aspx")
    End Sub

    And in your HTML code put this:

    <asp:Button id="btnReset" onclick="btnReset_Click" runat="server" Text="Reset Form"></asp:Button>

    Good Luck!

  13. #13
    Join Date
    May 2003
    Posts
    599
    It worked! So that I understand what needed to be done, does the function basically refresh the page when called by the OnClick event through the button?

    I'm just trying to understand how and why things work the way they do with ASP.NET/VB.NET, so that I don't have to repeat this question in the future. Thanks again for all of your help!
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

  14. #14
    Join Date
    Dec 2004
    Location
    Saint Petersburg, Florida
    Posts
    32
    Happy to hear it!

    How it works?

    When you click on the button:

    1. The page is posted back to the server and the Page_Load event is executed.
    2. Then the btnReset_Click event is executed.
    3. Since you have Server.Transfer("whatever.aspx") in btnReset_Click, the server transfers the execution to the "whatever" page you specify.
    4. When the navigation is transfered to that page, it loads it like it is loaded for a first time (which means that the code inside the "If not me.isPostBack then" will be executed) and all of the controls will get their initial values.

    If you can put breakpoints inside the above mentioned functions you will see what is going on.

    Good Luck!

  15. #15
    Join Date
    May 2003
    Posts
    599
    Great, thanks.
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles