Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Submit form after clearing field

Posted on 2008-10-24
Medium Priority
Last Modified: 2012-05-05

I have a form that has a search field on it.  When a person clicks search, the form is submitted and I am using the FormParameter in a Gridview to filter the records based on their entry into the field.  The default parameter for the FormParameter is %, so if the field is left blank and the form is submitted, all records are shown.

I have a second button called View All and when the user clicks on that, I want the field value to clear out then submit the form.

It is not working.  The first time I click the view all button, the form is sumbitted and the field is cleared out, but the gridview is still filtered.  The second time I click the view all button, I get the results I want, the form is sumbitted with the blank textbox and all records are shown.

How can I do this with one click of the view all button?  I thought maybe I could have javascript clear out the field, then submit, but I couldn't get that to work....

.cs and .aspx code below.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Email_List : System.Web.UI.Page
    protected void SetCounts(object sender, EventArgs e)
        string strConnection =
           "Data Source=SQLB21.webcontrolcenter.com;Integrated Security=false;Initial Catalog=webdata;User ID=martindale;Password=el3cTric!";
        SqlConnection myConn = new SqlConnection(strConnection);
        SqlCommand myCommand = new SqlCommand("sp_CountRows", myConn);
        myCommand.CommandType = CommandType.StoredProcedure;
        SqlParameter myParm = myCommand.Parameters.Add("@row_count", SqlDbType.Int);
        myParm.Direction = ParameterDirection.Output;
        // Use this syntax to get the value from the OUT parameter
        int row_count = (int)myParm.Value;
        rowcountLabel.Text = "Total Rows: " + row_count.ToString();
        int StartRecord = (GridView1.PageIndex * GridView1.PageSize) + 1;
        int EndRecord = (StartRecord - 1) + GridView1.Rows.Count;
        int CurrentPage = GridView1.PageIndex + 1;
        pageLabel.Text = "Displaying records " + StartRecord + " to " + EndRecord + " Page:" + CurrentPage;
    protected void searchButton_Click(object sender, EventArgs e)
    protected void clearButton_Click(object sender, EventArgs e)
        searchTextBox.Text = "";
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Email_List.aspx.cs" Inherits="Email_List" Title="Email Address List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<meta http-equiv="Content-Language" content="en-us" />
<meta name="GENERATOR" content="Microsoft FrontPage 6.0" />
<meta name="ProgId" content="FrontPage.Editor.Document" />
<meta name="description" content="Let Martindale quote on your electric motor maintenance or metal cutting needs today!	" />
<meta name="keywords" content="martindale electric, electrical test equipment, commutator, commstone, saw, saws, sanding, bur,
undercutter, vacuum, demagnitizer, brush seater" />
<meta name="revisit-after" content="15" />
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Let Martindale Quote on your Electric Motor Maintenance Tools Today!</title>
<link href="../styles.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
.style2 {font-family: Arial, Helvetica, sans-serif}
.style3 {font-size: 10px}
<script type="text/javascript">
// Google Internal Site Search script- By JavaScriptKit.com (http://www.javascriptkit.com)
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// This notice must stay intact for use
//Enter domain of site to search.
var domainroot="www.martindaleco.com"
function Gsitesearch(curobj){
curobj.q.value="site:"+domainroot+" "+curobj.qfront.value
<body leftmargin="0" topmargin="0">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse"
bordercolor="#111111" width="100%" id="AutoNumber1">
    <td bgcolor="#FF9933" align="center" width="15%"><img border="0"
    src="../images/spinningsaw2.gif" alt="Electric Motor Repair" width="60" height="59"></td>
    <td width="65%" bgcolor="#FF9933" align="center" >
    <a target="_self"
    href="http://www.martindaleco.com"><img src="../images/banner2.gif" border="0"
    alt="Electric Motor Maintenance-Martindale Products" width="219" height="52" style="margin:10px 0px 10px 0px;"></a><br />
    <ul id="nav" style="width:550px; margin:0px 0px 20px 0px;">
      <li id="lihome"><a href="../default.aspx">Home</a></li>
      <li id="liproducts"><a href="../productindex2.htm">Products</a></li>
      <li id="licontact"><a href="../contactus.htm">Contact Us</a></li>
      <li id="lirfq"><a href="../requestquote.aspx">Quotes</a></li>
      <li id="linew"><a href="../whatsnew.htm">What's New</a></li>
      <li id="liparts"><a href="../partssheets.htm">Parts Sheet/Instructions</a></li>
      <li id="liabout"><a href="../aboutus.htm">About Us</a></li>
      <li id="lilinks"><a href="../links.htm">Links/Shows</a></li>
	  <li id="li1"><a href="../clearance.htm">Clearance</a></li>
    <td width="20%" bgcolor="#FF9933" align="left" style="height: 129px">
    <table border="0" cellpadding="0" cellspacing="0">
    <td><div class="normal" onmouseover="this.className='highlight'" onmouseout="this.className='normal'" onclick="window.location.href('http://' + location.host + location.pathname + location.search)">English</div></td>
    <td><div class="normal" onmouseover="this.className='highlight'" onmouseout="this.className='normal'" onclick="window.location.href('http://translate.google.com/translate?u=http%3A%2F%2F' + location.host + location.pathname + location.search + '&sl=en&tl=es&hl=en&ie=UTF-8')">Española</div></td>
    <td><div class="normal" onmouseover="this.className='highlight'" onmouseout="this.className='normal'" onclick="window.location.href('http://translate.google.com/translate?u=http%3A%2F%2F' + location.host + location.pathname + location.search + '&sl=en&tl=fr&hl=en&ie=UTF-8')">Français</div></td>
    <form action="http://www.google.com/search" method="get" onsubmit="Gsitesearch(this)">
      <span class="style1"><span class="style3"><a href="mailto:sales@martindaleco.com?subject=Web Inquiry">
      <font color="#000000">sales@martindaleco.com</font></a><br />
        phone:&nbsp; 216-521-8567<br />
        fax:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 216-521-9476<br />
        Site Search:<br /></span></span>
      <input name="q" type="hidden" />
      <input style="width: 70px;padding: 1px 2px 0 2px;margin: 0;border: 1px solid #000;" name="qfront" type="text" size="5" /> <input
      style="font-size: 10px;color: #F93;background: #000;border: 2px solid #000;cursor: pointer;margin-bottom: 1px;" name="Submit" type="Submit" value="Search" /> 
    <form id="form1" runat="server">
<div align="center"> 
<table width="500">
<td width="15%">&nbsp;</td>
<td width="70%" valign="middle"> <h3 style="font-family:Tahoma;">
    <br />
    Martindale Email Marketing List</h3></td>
<td width="15%" style="font-size: x-small; font-family: Tahoma"><a href="CSV_List.aspx">view<br />
    Datawarehouse<br />
        <asp:TextBox ID="searchTextBox" runat="server"></asp:TextBox>
    <asp:Button ID="searchButton" runat="server" Text="Search" OnClick="searchButton_Click" />
    <asp:Button ID="clearButton" runat="server" Text="View All" OnClick="clearButton_Click" /><br /><br />
    <asp:Label ID="rowcountLabel" runat="server" Text="" Font-Bold="True" Font-Names="tahoma" Font-Size="Small"
        Width="334px"></asp:Label><br />
    <asp:Label ID="pageLabel" runat="server" Text="" Font-Bold="True" Font-Names="tahoma" Font-Size="Small"
        Width="334px"></asp:Label><br /><br />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid"
        BorderWidth="1px" CellPadding="3" DataKeyNames="db_key" DataSourceID="ObjectDataSource1"
        ForeColor="Black" GridLines="Vertical" PageSize="25"  OnDataBound="SetCounts">
        <FooterStyle BackColor="#CCCCCC" Font-Names="Tahoma" Font-Size="Small" />
            <asp:TemplateField HeaderText="Edit" ShowHeader="False">
                    <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True" CommandName="Update"
                     ImageUrl="~/images/disk.png" ToolTip="Save Changes" />
                    <asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" CommandName="Edit"
                        ImageUrl="~/images/pencil.png" ToolTip="Edit Email Addresses"  />
            <asp:TemplateField HeaderText="Delete" ShowHeader="False">
                    <asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="False"
                            CommandName="Cancel" ImageUrl="~/images/diskx.png" ToolTip="Discard Changes" />
                    <asp:ImageButton ID="ImageButton4" runat="server" CausesValidation="False" CommandName="Delete"
                        ImageUrl="~/images/cancel.png"  ToolTip="Delete Email Address"  />
            <asp:TemplateField HeaderText="Email Address" SortExpression="email_address">
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("email_address") %>' Width="255px"></asp:TextBox>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("email_address") %>' Width="255px"></asp:Label>
                <ItemStyle HorizontalAlign="Left" />
            <asp:TemplateField HeaderText="Date Added" SortExpression="date_added">
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("date_added", "{0:d}") %>' Width="71px"></asp:TextBox>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("date_added", "{0:d}") %>' Width="71px"></asp:Label>
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" Font-Names="tahoma" Font-Size="Small" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" Font-Names="Tahoma" Font-Size="Small" />
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <RowStyle Font-Names="Tahoma" Font-Size="Small" />
        <EditRowStyle Font-Names="Tahoma" Font-Size="Small" ForeColor="Black" BackColor="#FFFF80" />
    <br />
    <br />
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" OldValuesParameterFormatString="original_{0}" SelectMethod="GetEmailbySearch"
        TypeName="DataTableAdapters.tbl_Email_BlastTableAdapter" UpdateMethod="Update">
            <asp:Parameter Name="Original_db_key" Type="Int32" />
            <asp:Parameter Name="email_address" Type="String" />
            <asp:Parameter Name="date_added" Type="DateTime" />
            <asp:Parameter Name="Original_db_key" Type="Int32" />
            <asp:FormParameter DefaultValue="%" FormField="searchTextBox" Name="email" Type="String" />
    <table width="100%">
    <td bgcolor="black">
    <p align="center">
    <font face="Arial" size="1" color="#FF9933">
    <u><a target="_self" href="http://www.martindaleco.com">
    <font color="#FF9933"><span style="background-color: #000000">Home</span></font></a></u><span style="background-color: #000000"> |
    <a href="../MetalWorkingMicaSaws.htm"><font color="#FF9933">Metal Working Mica Saws</font></a> |
    <a href="../rotaryburs.htm"><font color="#FF9933">Rotary 
    Burs</font></a> | <a href="../Undercutters.htm"><font color="#FF9933">Undercutters</font></a> |
    <a href="../abrasives.htm"><font color="#FF9933">Abrasives</font></a> |
    <a href="../commutatorgrinders.htm"><font color="#FF9933">Commutator Grinders</font></a> |
    <a href="../Electricaltestequipment.htm"><font color="#FF9933">Electrical Test 
    Equipment</font></a> | <a href="../electricianshandtools.htm">
    <font color="#FF9933">Electrician's Hand Tools </font></a>| </span>
    <a href="../otherproducts.htm"><font color="#FF9933">
    <span style="background-color: #000000">Other Products</span></font></a></font>

Open in new window

Question by:Bonnie_K
  • 2
LVL 26

Accepted Solution

Anurag Thakur earned 2000 total points
ID: 22795689
on page load add the following line
clearButton.Attributes.Add("onClick", "Empty (" + clearButton.ClientID + ")");

in javascript function Empty add the follwoing line

function setControlValue(var name)
      document.getElementById(name).value = "";


Author Comment

ID: 22811054
Sorry it took so long for me to test this.  I get the same behavior with this, I have to click the view all button twice...


Author Comment

ID: 22811170
Never mind, got it working.  I ended up using the following:

    public void Page_Load(Object sender, EventArgs E)
            clearButton.Attributes.Add("onClick", "Empty ();");

function Empty()
document.form1.searchTextBox.value = "";

Thanks for your help,

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Loops Section Overview
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question