banner



How To Clear Value In Textbox Using Jquery

  • Remove From My Forums

 locked

Unable to clear Textbox

  • Question

  • User1769015664 posted

    I have several GridViews and Tables on a page. Next to a table; I have a button to clear out Text in the TD element's TextBox control "CourseName".  When I click on button; it clears out the CourseName field but it also wipes out data from the rest of the control on the page including GridViews. When I put this button in an UpdatePanel; the code behind gets executed but the field "CourseName" wouldn't clear out.

    <td><asp:TextBox ID="CourseName" runat="server"></asp:TextBox></td>

Answers

  • User2103319870 posted

    <td><asp:TextBox ID="CourseName" runat="server"></asp:TextBox></td>

    To clear the textbox you can use jquery like below

    Assign css style to textbox like given below

                          <asp:TextBox ID="CourseName" class="cssCourseName" runat="server" Text='<%#Eval("Student_Name")%>'></asp:TextBox>

    Then you can use the jquery code like below

                          <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>     <script type="text/javascript">         $(document).ready(function () {             $("#btnSubmit").click(function () {                 //clear the textbox                 $(".cssCourseName").val("");             });         });      </script>

    Complete Code

                          <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>         <script type="text/javascript">             $(document).ready(function () {                 $("#btnSubmit").click(function () {                     //clear the textbox                     $(".cssCourseName").val("");                 });             });          </script>         <asp:ScriptManager ID="ScriptManager2" runat="server" />         <asp:Button ID="btnSubmit" runat="server" Text="Clear" />         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">             <Columns>                 <asp:TemplateField HeaderText="Student_Name">                     <ItemTemplate>                         <asp:TextBox ID="CourseName" class="cssCourseName" runat="server" Text='<%#Eval("Student_Name")%>'></asp:TextBox>                     </ItemTemplate>                 </asp:TemplateField>             </Columns>         </asp:GridView>

    C#:

                          protected void Page_Load(object sender, EventArgs e)         {             if (!IsPostBack)             {                                 DataTable dt = new DataTable();                 dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Student_Id"), new DataColumn("Student_Name"), new DataColumn("Amount_In_Kitty"), new DataColumn("Require") });                 dt.Rows.Add(1, "Student1", 12, 25);                 dt.Rows.Add(2, "Student2", 34, 29);                 dt.Rows.Add(3, "Student3", 10, 11);                 dt.Rows.Add(4, "Student4", 45, 14);                 GridView1.DataSource = dt;                 GridView1.DataBind();             }
    • Marked as answer by Thursday, October 7, 2021 12:00 AM
  • User-1838255255 posted

    Hi NJ2,

    According to your description and needs, I make a sample that use jQuery to clear the textbox, I think you could through ID to get this element then clear this value.

    Sample Code:

    <head runat="server">     <title></title>     <script src="Scripts/jquery-2.1.0.js"></script>     <script>         $(function () {             $("#<%= btnBlock.ClientID %>").click(function () {                 debugger;                 $("#<%= TextBox2.ClientID %>").val("");                 $("#<%= TextBox3.ClientID %>").val("");                 $("#<%= TextBox4.ClientID %>").val("");             });         });     </script> </head> <body>     <form id="form1" runat="server">         <div>             <table style="width: 100%" runat="server">                 <tr>                     <th>Value1</th>                     <th>Value2</th>                     <th>Value3</th>                 </tr>                 <tr>                     <td>                         <asp:TextBox ID="TextBox2" runat="server">111</asp:TextBox></td>                     <td>                         <asp:TextBox ID="TextBox3" runat="server">222</asp:TextBox></td>                     <td>                         <asp:TextBox ID="TextBox4" runat="server">333</asp:TextBox></td>                 </tr>             </table>             <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>             <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">                 <ContentTemplate>                     <asp:GridView ID="GridView1" runat="server">                         <Columns>                             <asp:TemplateField>                                 <ItemTemplate>                                     <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Student_Id") %>'></asp:TextBox>                                 </ItemTemplate>                             </asp:TemplateField>                             <asp:BoundField DataField="Student_Name" HeaderText="Student_Name" />                             <asp:BoundField DataField="Amount_In_Kitty" HeaderText="Amount_In_Kitty" />                             <asp:BoundField DataField="Require" HeaderText="Require" />                         </Columns>                     </asp:GridView>                     <asp:Button ID="btnBlock" class="Button" Text="BlockCalls" runat="server"                         Width="100px" />                 </ContentTemplate>             </asp:UpdatePanel>         </div>     </form> </body>  protected void Page_Load(object sender, EventArgs e)         {             if (!IsPostBack)             {                 DataTable dt = new DataTable();                 dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Student_Id"), new DataColumn("Student_Name"), new DataColumn("Amount_In_Kitty"), new DataColumn("Require") });                 dt.Rows.Add(1, "Student1", 12, 25);                 dt.Rows.Add(2, "Student2", 34, 29);                 dt.Rows.Add(3, "Student3", 10, 11);                 dt.Rows.Add(4, "Student4", 45, 14);                 GridView1.DataSource = dt;                 GridView1.DataBind();             }         }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM

How To Clear Value In Textbox Using Jquery

Source: https://social.msdn.microsoft.com/Forums/en-US/52a97018-8827-4bff-bede-a9f91d2cf161

Posted by: martintagazier1947.blogspot.com

0 Response to "How To Clear Value In Textbox Using Jquery"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel