locked
Textbox OnTextChanged event fires twice when given some input RRS feed

  • Question

  • User2103449440 posted

    I have been trying to add row in gridview and the input fields are in the footer of the gridview. this is my code

    Aspx page:

    <asp:GridView ID="GridScripBase" runat="server" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" ShowFooter="true"
        CssClass="table table-responsive table-bordered table-hover AspGridView"
        OnRowCancelingEdit="GridScripBase_RowCancelingEdit"
        OnRowDeleting="GridScripBase_RowDeleting"
        OnRowEditing="GridScripBase_RowEditing"
        OnRowUpdating="GridScripBase_RowUpdating"
        OnRowDataBound="GridScripBase_RowDataBound">
        <PagerStyle CssClass="pagination" />
        <Columns>
            <asp:TemplateField HeaderText="Scrip Code" SortExpression="SCRIP_CODE">
                <ItemTemplate>
                    <asp:Label ID="lblScripCode" runat="server" Text='<%# Eval("SCRIP_CODE")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtScripCode" CssClass="AspTextbox" MaxLength="7" runat="server" OnTextChanged="txtScripCode_TextChanged" AutoPostBack="true" Width="80px" EnableViewState="true"></asp:TextBox>
                    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtScripCode" FirstRowSelected="false" ShowOnlyCurrentWordInCompletionListItem="true"
                        ServiceMethod="FetchScripCode" MinimumPrefixLength="1" CompletionSetCount="10" CompletionInterval="0" EnableCaching="true">
                    </cc1:AutoCompleteExtender>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Scrip Name" SortExpression="SCRIP_NAME">
                <ItemTemplate>
                    <asp:Label ID="lblScripName" runat="server" Text='<%# Eval("SCRIP_NAME")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtScripName" CssClass="AspTextbox disable" runat="server" ReadOnly="true" Width="150px"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Action Code" HeaderStyle-Width="90px" SortExpression="CORP_ACTION">
                <ItemTemplate>
                    <asp:Label ID="lblActionCode" runat="server" Text='<%# Eval("CORP_ACTION")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddlActionCode" runat="server" CssClass="AspDropDown" AutoPostBack="true"
                        OnSelectedIndexChanged="ddlActionCode_SelectedIndexChanged" Width="92px">
                        <asp:ListItem>---Select---</asp:ListItem>
                        <asp:ListItem>BC</asp:ListItem>
                    </asp:DropDownList>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Action Name" HeaderStyle-Width="130px" SortExpression="CORP_DESC">
                <ItemTemplate>
                    <asp:Label ID="lblActionName" runat="server" Text='<%# Eval("CORP_DESC")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtActionName" CssClass="AspTextbox disable" MaxLength="5" ReadOnly="true" runat="server" Width="137px"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Trans Date" HeaderStyle-Width="120px" SortExpression="trans_date">
                <ItemTemplate>
                    <asp:Label ID="lblTransdate" runat="server" Text='<%# Eval("trans_date","{0:dd/MM/yyyy}")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtTransdate" CssClass="AspTextbox disable" MaxLength="5" runat="server" ReadOnly="true" DataFormatString="{0:dd/MM/yyyy}"
                        AutoPostBack="true" Width="92px"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Effective Date" HeaderStyle-Width="120px" SortExpression="DIV_DATE">
                <ItemTemplate>
                    <asp:Label ID="lblDivdate" runat="server" Text='<%# Eval("DIV_DATE","{0:dd/MM/yyyy}")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtDivdate" CssClass="AspTextbox disable" MaxLength="5" runat="server" OnTextChanged="txtDivdate_TextChanged"
                        ReadOnly="true" AutoPostBack="true" Width="92px"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="New Base Price" HeaderStyle-Width="90px" SortExpression="DIV_AMT">
                <ItemTemplate>
                    <asp:Label ID="lbldivamount" runat="server" Text='<%# Eval("DIV_AMT")%>' onfocus="this.blur();"></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtdivamount" CssClass="AspTextbox disable" MaxLength="5" runat="server" ReadOnly="true" Width="65px" onfocus="this.blur();"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Close Rate" HeaderStyle-Width="78px" SortExpression="CLOSE_RATE">
                <ItemTemplate>
                    <asp:Label ID="lblCloseRate" runat="server" Text='<%# Eval("CLOSE_RATE")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtCloseRate" CssClass="AspTextbox disable" MaxLength="5" runat="server" Width="58px" ReadOnly="true" onfocus="this.blur();"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Multiplier Factor" HeaderStyle-Width="50px" SortExpression="MULTIPLIER_FACTOR">
                <ItemTemplate>
                    <asp:Label ID="lblMultFactor" runat="server" Text='<%# Eval("MULTIPLIER_FACTOR")%>' onfocus="this.blur();"></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtMultFactor" CssClass="AspTextbox disable" MaxLength="5" runat="server" ReadOnly="true" Width="70px" onfocus="this.blur();"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Adjusted Rate" HeaderStyle-Width="50px" SortExpression="ADJUSTED_RATE">
                <ItemTemplate>
                    <asp:Label ID="lblAdjustedRate" runat="server" Text='<%# Eval("ADJUSTED_RATE")%>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtAdjustedRate" Text='<%# Eval("ADJUSTED_RATE")%>' runat="server" Width="60px"></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtAdjustedRate" CssClass="AspTextbox" MaxLength="5" runat="server" Width="60px"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Clean Dirty Flag" HeaderStyle-Width="83px" SortExpression="CLEAN_DIRTY_FLAG">
                <ItemTemplate>
                    <asp:Label ID="lbldirtyFlag" runat="server" Text='<%# Eval("CLEAN_DIRTY_FLAG")%>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtDirtyFlag" CssClass="AspTextbox disable" MaxLength="5" runat="server" Width="62px" ReadOnly="true" onfocus="this.blur();"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
    
            <asp:TemplateField HeaderText="Actions" ShowHeader="false">
                <EditItemTemplate>
                    <asp:Button ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" />
                    <asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Button ID="ButtonEdit" runat="server" CommandName="Edit" Text="Edit" />
                    <asp:Button ID="ButtonDelete" runat="server" CommandName="Delete" Text="Delete" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Button runat="server" ID="BtnAdd" Text="Add" OnClick="BtnAdd_Click" OnClientClick="return Confirm();" />
                    <asp:Button ID="BtnClear" runat="server" Text="Clear" OnClick="BtnClear_Click" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle BackColor="#0089d0" ForeColor="White" HorizontalAlign="Left" />
        <PagerStyle BackColor="#0089d0" Font-Bold="true" ForeColor="White" />
        <EditRowStyle BackColor="#c0e6fa" />
    </asp:GridView>

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable grid = BindData();
            if (grid.Rows.Count > 0)
            {
                GridScripBase.DataSource = grid;
                GridScripBase.DataBind();
            }
            else
            {
                ShowNoResultFound(grid, GridScripBase);
            }
    
        }
    }

    method ShowNoResultFound shows empty row if no result is found in datatable

    private void ShowNoResultFound(DataTable source, GridView gv)
    {
        source.Rows.Add(source.NewRow());
        gv.DataSource = source;
        gv.DataBind();
        int columnsCount = gv.Columns.Count;
        gv.Rows[0].Cells.Clear();
        gv.Rows[0].Cells.Add(new TableCell());
        gv.Rows[0].Cells[0].ColumnSpan = columnsCount;
        gv.Rows[0].Cells[0].Text = "No Result Found!";
    }
    protected void txtScripCode_TextChanged(object sender, EventArgs e)
    {   
        string ScripCode = ((TextBox)GridScripBase.FooterRow.FindControl("txtScripCode")).Text;
        try
        {
            DataTable grid = BindData();
            if (grid.Rows.Count > 0)
            {
                GridScripBase.DataSource = grid;
                GridScripBase.DataBind();
            }
            else
            {
                ShowNoResultFound(grid, GridScripBase);
            }
            if (ScripCode.Length < 7 || ScripCode!="")
            {
                if (Regex.IsMatch(ScripCode, "^[0-9+]*$"))
                {
                    string name="";
                    foreach(DataRow row in dt.Rows)
                    {
                        if (row[0].ToString() == ScripCode)
                        {
                            name = row[3].ToString();
    break; } } ((TextBox)GridScripBase.FooterRow.FindControl("txtScripName")).Text = name; } else { ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Please enter valid Scrip Code.');", true); } } else { ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Please Select Scrip Code.');", true); } } catch (Exception ex) { ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Error" + ex.Message + "');", true); } }

    The problem here is when user enters text in txtscripcode ontxtchanged event is fired twice and when no data is present in the gridview then too an empty row is shown in the gridview with edit and delete button.

    Any help will be appreciated.

    Tuesday, October 3, 2017 6:31 AM

All replies