Results 1 to 5 of 5

Thread: Form wont Clear after Submit

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    52

    Form wont Clear after Submit

    After the information is put into the SQL server the information in the form doesnt not clear out it just stays there then I have to delete them one by one which is really annoying. Pls can someone show me what Iam doing wrong.

    Thanks


    Code...


    <%@ Page Language="VB" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>

    <script runat="server">

    Dim conn As SqlConnection
    Dim param As SqlParameter
    Dim cmdcommand as sqlcommand

    Sub Page_Load(Src As Object, e As EventArgs)

    'Enter your User ID & Password appropriately

    conn = New SqlConnection("Data Source=xxx;User ID=xxxx;password=xxxx;Initial Catalog = xxxxx")


    conn.open()
    End Sub

    Sub btnSave_Click(sender As Object, e As EventArgs)

    'Stored Procedure name

    cmdcommand = new sqlcommand("Register",conn)
    cmdcommand.commandtype = commandtype.storedprocedure
    param = cmdcommand.parameters.add("ReturnValue",sqldbtype.int)
    param.direction = parameterdirection.returnvalue

    cmdcommand.parameters.add("@cartname",txtcartname.text)
    cmdcommand.parameters.add("@cartphone",txtcartphone.text)
    cmdcommand.parameters.add("@cartemail",txtcartemail.text)
    cmdcommand.parameters.add("@connection",txtconnection.text)
    cmdcommand.parameters.add("@address",txtaddress.text)
    cmdcommand.parameters.add("@city",txtcity.text)
    cmdcommand.parameters.add("@state",txtstate.text)
    cmdcommand.parameters.add("@zipcode",txtzipcode.text)
    cmdcommand.parameters.add("@country",txtcountry.text)
    cmdcommand.executenonquery()

    if cmdcommand.parameters("ReturnValue").value = 0 then
    lblStatus.text = "Registered Successfully"
    else
    lblStatus.text = "Error!, Please try again"
    end if
    conn.close()

    End Sub

    </script>
    </head>
    <title>Add Cart</title>
    <style type="text/css">
    <!--
    .style4 {
    font-size: 18px;
    font-family: "Times New Roman", Times, serif;
    color: #FFFFFF;
    }
    -->
    </style>
    <body>
    <table width="100%" border="0" cellPadding="0" cellSpacing="0" bgcolor="#336699">
    <tr>
    <td height="16" class="subHeading style8 style2 style4 style4">Add Cart</td>
    <td class="subHeading" width="19%" height="16">&nbsp;</td>
    <td width="25%" height="16" class="subHeading style4"><span class="subHeading "><span class="style3 ">Date:</span></span><%=DateTime.Now.ToString()%> </td>
    </tr>
    </table>
    <table width="100%" cellpadding="1" cellspacing="1">
    <tr>
    <td width="12%"><asp:HyperLink ID="RoleManager" runat="server" NavigateUrl="RoleManager.aspx" Visible="False">Role Manager</asp:HyperLink></td>
    <td width="88%" align="right"><asp:HyperLink ID="Account" runat="server" NavigateUrl="Register.aspx">Account</asp:HyperLink>
    <asp:HyperLink ID="SignOut" runat="server" NavigateUrl="Login.aspx">Sign Out</asp:HyperLink></td>
    </tr>
    <tr>
    <td colspan="2"><table class="searchLabel" cellspacing="0" cellpadding="0" width="100%" border="0">
    <tr>
    <td width="59%" colspan="2"><form runat="server">
    <table width="100%" border="0" bordercolor="#336699">
    <tr>
    <td width="23%"><strong>Cart Name</strong></td>
    <td width="77%"><asp:TextBox id="txtcartname" runat="server" Width="200px" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td height="22"><strong>Cart Phone</strong></td>
    <td><asp:TextBox id="txtcartphone" runat="server" Width="150px" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td><strong>Cart Email</strong></td>
    <td><asp:TextBox id="txtcartemail" runat="server" Width="200px" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td><strong>Connection Status </strong></td>
    <td><asp:TextBox id="txtconnection" runat="server" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td><strong>Address</strong></td>
    <td><asp:TextBox id="txtAddress" runat="server" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td><strong>City</strong></td>
    <td><asp:TextBox id="txtcity" runat="server" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td><strong>State</strong></td>
    <td><asp:TextBox id="txtstate" runat="server" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td><strong>ZipCode</strong></td>
    <td><asp:TextBox id="txtzipcode" runat="server" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td><strong>Country</strong></td>
    <td><asp:TextBox id="txtcountry" runat="server" CssClass="insidetext"></asp:TextBox></td>
    </tr>
    <tr>
    <td><span class="fieldcaptions">
    <asp:Button id="btnSave" onclick="btnSave_Click" runat="server" Text="Save"></asp:Button>
    </span></td>
    <td><span class="fieldcaptions"> </span>
    <asp:Label id="lblStatus" runat="server" ForeColor="#FF8000" Font-Bold="True" Font-Names="Garamond"></asp:Label></td>
    </tr>
    </table>
    </form></td>
    </tr>
    </table></td>
    </tr>
    </table>
    <p>&nbsp;</p>

    </body>
    </html>

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Form wont Clear after Submit

    You are not doing anything wrong. Since by default viewstate is set to Enabled for all controls, when you post back all input are restored/kept by asp.net. You can either set the EnableViewstate to false for all the input fields or just simply write a function which clears out all the input value. I will suggest 2nd options. As you need the viewstate incase user has entered wrong input and you dont want the form to be cleared..
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    52

    Re: Form wont Clear after Submit

    Can anyone give me any example of the code for this??

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Form wont Clear after Submit

    Here is an example i put together. If you are using HtmlControls then make sure you set them as Server Controls.

    VB Code:
    1. 'Usage
    2. ''''''''''''''''''''''
    3. Dim ctl As Control
    4.  
    5.         Me.ListBox2.Items.Clear()
    6.  
    7.         For Each ctl In Me.Controls
    8.             ClearControls(ctl)
    9.         Next
    10. '''''''''''''''''
    11.  
    12.  
    13. Private Sub ClearControls(ByVal ctl As Control)
    14.         Dim ct As Control
    15.  
    16.         For Each ct In ctl.Controls
    17.  
    18.             If ct.GetType() Is GetType(TextBox) Then
    19.                 CType(ct, TextBox).Text = ""
    20.             ElseIf ct.GetType Is GetType(RadioButton) Then
    21.                 CType(ct, RadioButton).Checked = False
    22.             ElseIf ct.GetType Is GetType(CheckBox) Then
    23.                 CType(ct, CheckBox).Checked = False
    24.             ElseIf ct.GetType Is GetType(ListBox) Then
    25.                 Dim lb As ListBox
    26.                 lb = CType(ct, ListBox)
    27.  
    28.                 If lb.Items.Count > 0 Then
    29.                     lb.SelectedIndex = -1
    30.                 End If
    31.             ElseIf ct.GetType Is GetType(DropDownList) Then
    32.                 Dim lb As DropDownList
    33.                 lb = CType(ct, DropDownList)
    34.  
    35.                 If lb.Items.Count > 0 Then
    36.                     lb.SelectedIndex = -1
    37.                 End If
    38.  
    39.             ElseIf ct.GetType() Is GetType(HtmlInputText) Then
    40.                 CType(ct, HtmlInputText).Value = ""
    41.             ElseIf ct.GetType Is GetType(HtmlTextArea) Then
    42.                 CType(ct, HtmlTextArea).Value = ""
    43.             ElseIf ct.GetType() Is GetType(HtmlInputRadioButton) Then
    44.                 CType(ct, HtmlInputRadioButton).Checked = False
    45.             ElseIf ct.GetType() Is GetType(HtmlInputCheckBox) Then
    46.                 CType(ct, HtmlInputCheckBox).Checked = False
    47.             ElseIf ct.GetType() Is GetType(HtmlSelect) Then
    48.                 Dim sel As HtmlSelect
    49.                 sel = CType(ct, HtmlSelect)
    50.  
    51.                 If sel.Items.Count > 0 Then
    52.                     sel.SelectedIndex = -1
    53.                 End If
    54.             End If
    55.  
    56.             If ct.Controls.Count > 0 Then
    57.                 ClearControls(ct)
    58.             End If
    59.         Next
    60.  
    61.  
    62.  
    63.     End Sub
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  5. #5
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Re: Form wont Clear after Submit

    I'm a noob, but could you do something like --

    Code:
    if cmdcommand.parameters("ReturnValue").value = 0 then
    Response.redirect("thispage.aspx?Success=true")
    else
    lblStatus.text = "Error!, Please try again"
    end if
    Then in the page load request the query string and set
    the success message in lblStatus.

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