Clear TextBox inside ASP.Net GridView on TextChange event using C# and VB.Net

makumbi
 
on Jun 26, 2022 11:42 PM
301 Views

Please help how can I clear TextBox after saving data in the database.

Example I have a score TextBox fileld where I want to clear data after saving data.

I would not want data to remain in the TextBox.

Please help

<body>
    <form id="form1" runat="server" class="newStyle1">
        <asp:Panel ID="Panel1" runat="server" BorderColor="#FF3300" Font-Bold="True" Height="575px" Width="931px">
            <table class="auto-style1">
                <tr>
                    <td class="auto-style9" colspan="7">
                        <asp:ScriptManager ID="ScriptManager1" runat="server">
                        </asp:ScriptManager>
                        SELECT THE CLASS ,STREAM AND SUBJECT FROM THE LISTS BELOW AND CLICK [GO] TO START ENTERING MARKS</td>
                </tr>
                <tr>
                    <td class="auto-style9" rowspan="2"><strong>Class</strong></td>
                    <td class="auto-style13" rowspan="2"><strong>
                        <asp:DropDownList ID="DropDownList1Class" runat="server" DataSourceID="Classdr" DataTextField="CLASS" DataValueField="CLASS" AutoPostBack="True">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="Classdr" runat="server" ConnectionString="<%$ ConnectionStrings:SMIS2022ConnectionString %>" SelectCommand="SELECT [CLASS] FROM [CLASS]"></asp:SqlDataSource>
                        </strong></td>
                    <td class="auto-style6" rowspan="2"><strong>Subject</strong></td>
                    <td class="auto-style9" colspan="3" rowspan="2"><strong>
                        <asp:DropDownList ID="DropDownList3Subject" runat="server" DataSourceID="SqlDataSource1" DataTextField="subject" DataValueField="subject" Height="28px" Width="301px">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SMIS2022ConnectionString %>" SelectCommand="SELECT [subject] FROM [subjects]"></asp:SqlDataSource>
                        </strong></td>
                    <td class="auto-style11">TEACHERS&#39; LISTS FOR RECORDING MARKS</td>
                </tr>
                <tr>
                    <td class="auto-style12">Print Marks Entry Sheets</td>
                </tr>
                <tr>
                    <td class="auto-style20">Stream</td>
                    <td class="auto-style21">
                        <asp:DropDownList ID="DropDownList2Stream" runat="server" DataSourceID="Streamddr" DataTextField="Stream" DataValueField="Stream">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="Streamddr" runat="server" ConnectionString="<%$ ConnectionStrings:SMIS2022ConnectionString %>" SelectCommand="SELECT [Stream] FROM [Streamdata]"></asp:SqlDataSource>
                    </td>
                    <td class="auto-style16"></td>
                    <td class="auto-style23">
                        <asp:Label ID="lblTotalBK" runat="server" />
                    </td>
                    <td class="auto-style22">
                        <asp:Button ID="Button1" runat="server" Text="Go" />
                    </td>
                    <td class="auto-style22">
                        <asp:Button ID="Button3" runat="server" Text="Save Data" style="height: 26px" />
                    </td>
                    <td class="auto-style18">
                        <asp:Button ID="Button2" runat="server" Text="Print List" />
                    </td>
                </tr>
                <tr>
                    <td aria-hidden="True" class="auto-style20" hidden="hidden">
                        &nbsp;</td>
                    <td aria-hidden="True" class="auto-style20" colspan="6" hidden="hidden">
                        <asp:Label ID="k11" runat="server" Text="k11"></asp:Label>
                        <asp:Label ID="k12" runat="server" Text="k12"></asp:Label>
                        <asp:Label ID="k13" runat="server" Text="k13"></asp:Label>
                        <asp:Label ID="k14" runat="server" Text="k14"></asp:Label>
                        <asp:Label ID="k15" runat="server" Text="k15"></asp:Label>
                        <asp:Label ID="k16" runat="server" Text="k16"></asp:Label>
                        <asp:Label ID="k17" runat="server" Text="k17"></asp:Label>
                        
                    </td>
                </tr>
                <tr>
                    <td class="auto-style7" colspan="7">
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <ContentTemplate>
                                  <asp:GridView ID="StudentGridMarks" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanging="OnPageIndexChanging" PageSize="45">
                                      <Columns>
                                           <asp:TemplateField>
                                            <HeaderTemplate>
                                                <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" />
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" Enabled="False" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                          <asp:TemplateField HeaderText="Admno">
                                              <EditItemTemplate>
                                                  <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("admno") %>'></asp:TextBox>
                                              </EditItemTemplate>
                                              <ItemTemplate>
                                                  <asp:Label ID="Label3" runat="server" Text='<%# Bind("admno") %>' Visible="False"></asp:Label>
                                                  <asp:TextBox ID="admno" runat="server" Text='<%# Eval("admno") %>' Height="16px" Width="93px" ReadOnly="True"></asp:TextBox>
                                              </ItemTemplate>
                                          </asp:TemplateField>
                                          <asp:TemplateField HeaderText="Name">
                                              <EditItemTemplate>
                                                  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
                                              </EditItemTemplate>
                                              <ItemTemplate>
                                                  <asp:TextBox ID="Name" runat="server" Text='<%# Eval("Name") %>' Height="16px" Width="186px" ReadOnly="True"></asp:TextBox>
                                                  <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>' Visible="False"></asp:Label>
                                              </ItemTemplate>
                                          </asp:TemplateField>
                                          <asp:TemplateField HeaderText="Class">
                                              <EditItemTemplate>
                                                  <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Class") %>' Height="16px" Width="58px"></asp:TextBox>
                                              </EditItemTemplate>
                                              <ItemTemplate>
                                                  <asp:Label ID="Label2" runat="server" Text='<%# Bind("Class") %>' Visible="False"></asp:Label>
                                                  <asp:TextBox ID="Class" runat="server" Text='<%# Eval("Class") %>' Height="16px" Width="32px" ReadOnly="True"></asp:TextBox>
                                              </ItemTemplate>
                                          </asp:TemplateField>
                                           <asp:TemplateField HeaderText="Stream">
                                               <EditItemTemplate>
                                                   <asp:TextBox ID="TextBox4" runat="server" Height="16px" Text='<%# Bind("Stream") %>' Width="42px"></asp:TextBox>
                                               </EditItemTemplate>
                                               <ItemTemplate>
                                                   <asp:Label ID="Label4" runat="server" Text='<%# Bind("Stream") %>' Visible="False"></asp:Label>
                                                   <asp:TextBox ID="Stream" runat="server" Height="16px" Text='<%# Eval("Stream") %>' Width="41px" ReadOnly="True"></asp:TextBox>
                                               </ItemTemplate>
                                           </asp:TemplateField>
                                           <asp:TemplateField HeaderText="Score">
                                               <EditItemTemplate>
                                                   <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Score") %>'></asp:TextBox>
                                               </EditItemTemplate>
                                               <ItemTemplate>
                                                   <asp:Label ID="Label5" runat="server" Text='<%# Bind("Score") %>' Visible="False"></asp:Label>
                                                   
                                                    <asp:TextBox ID="Score" runat="server" Text='<%#Eval("score") %>' onkeyup="enter(this)" AutoPostBack="true" OnTextChanged="Score_TextChanged" Height="16px" Width="31px"></asp:TextBox>
                                                    </ItemTemplate>
                                           </asp:TemplateField>
                                           <asp:TemplateField HeaderText="Agg">
                                               <EditItemTemplate>
                                                   <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Grade") %>'></asp:TextBox>
                                               </EditItemTemplate>
                                               <ItemTemplate>
                                                   <asp:Label ID="Label6" runat="server" Text='<%# Bind("Grade") %>' Visible="False"></asp:Label>
                                                   <asp:TextBox ID="Agg" runat="server" Height="16px" ReadOnly="True" Text='<%# Eval("Grade") %>' Width="29px"></asp:TextBox>
                                               </ItemTemplate>
                                           </asp:TemplateField>
                                           <asp:TemplateField HeaderText="Remarks">
                                               <EditItemTemplate>
                                                   <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Remark") %>'></asp:TextBox>
                                               </EditItemTemplate>
                                               <ItemTemplate>
                                                   <asp:Label ID="Label7" runat="server" Text='<%# Bind("Remark") %>' Visible="False"></asp:Label>
                                                   <asp:TextBox ID="Remarks" runat="server" AutoPostBack="True" Height="18px" onkeyup="enter(this)" OnTextChanged="Remarks_TextChanged" Text='<%# Eval("Remark") %>' Width="431px"></asp:TextBox>
                                               </ItemTemplate>
                                           </asp:TemplateField>
                                           <asp:TemplateField HeaderText="UserName">
                                               <EditItemTemplate>
                                                   <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
                                               </EditItemTemplate>
                                               <ItemTemplate>
                                                   <asp:Label ID="Label8" runat="server" Visible="False"></asp:Label>
                                                   <asp:TextBox ID="UserName" runat="server" Text='<%# Eval("Username") %>' Height="16px" Width="106px"></asp:TextBox>
                                               </ItemTemplate>
                                           </asp:TemplateField>
                                           <asp:TemplateField HeaderText="DateTime">
                                               <EditItemTemplate>
                                                   <asp:TextBox ID="TextBox9" runat="server"></asp:TextBox>
                                               </EditItemTemplate>
                                               <ItemTemplate>
                                                   <asp:Label ID="Label9" runat="server" Visible="False"></asp:Label>
                                                   <asp:TextBox ID="DateTime" runat="server" Height="16px" Text='<%# Eval("time") %>' Width="151px"></asp:TextBox>
                                               </ItemTemplate>
                                           </asp:TemplateField>
                                      </Columns>
                                  </asp:GridView>
                                 <asp:HiddenField ID="hfRowIndex" runat="server" />
                                 <asp:Label ID="lblTotal" runat="server" />                         
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </td>
                </tr>
            </table>
        </asp:Panel>
    </form>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 26, 2022 11:45 PM

Hi makumbi,

For clearing the TextBox value you need to set the TextBox value with empty string inside the OnTextChanged event.

C#

protected void Score_TextChanged(object sender, EventArgs e)
{
    GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
    hfRowIndex.Value = row.RowIndex.ToString();
    (sender as TextBox).Text = string.Empty;
}

VB.Net

Protected Sub Score_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim row As GridViewRow = TryCast(TryCast(sender, TextBox).NamingContainer, GridViewRow)
    hfRowIndex.Value = row.RowIndex.ToString()
    TryCast(sender, TextBox).Text = String.Empty
End Sub