textbox textchanged event always firing

别说谁变了你拦得住时间么 提交于 2019-12-24 11:39:59

问题


I have a form with a few text-boxes and a Grid-view. There is this one text-box called "Inward Number", whose text-changed event fires all the time. When i want to fire another control's event, it simply fires the text-changed event of the Inward Number text-box. This happens with all of the other controls. Can someone please help?

protected void txtInward_No_TextChanged(object sender, EventArgs e)
{
   a = 'A';
   NewRow();
}

I have used the correct names, but still this error occurs.

<asp:TextBox ID="txtInward_No" runat="server" Width="220px" CssClass="txtbox" 
                    EnableViewState="False" 
                    AutoPostBack="True" ontextchanged="txtInward_No_TextChanged">
</asp:TextBox>

new row just makes a new row in my grid.

private void NewRow() { int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtnew = new DataTable();
            dtnew = (DataTable)ViewState["CurrentTable"];
            DataRow drr = null;
            if (dtnew.Rows.Count > 0)
            {
                for (int i = 1; i <= dtnew.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("TextBox3");
                    TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox4");
                    TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[6].FindControl("TextBox5");

                    drr = dtnew.NewRow();
                    if (txtInward_No.Text =="1")
                    {
                        drr["InwardNumber"] = txtInward_No.Text + b.ToString();                           
                    }
                    else 
                    {
                        drr["InwardNumber"] = txtInward_No.Text + a.ToString();
                    }
                    drr["Bags"] = box1.Text;
                    drr["GrossWt"] = box2.Text;
                    drr["TareWt"] = box3.Text;
                    drr["NetWt"] = box4.Text;
                    drr["DCBillWt"] = box5.Text;

                    rowIndex++;                       
                }
                dtnew.Rows.Add(drr);
                ViewState["CurrentTable"] = dtnew;
                GridView1.DataSource = dtnew;
                GridView1.DataBind();                    
            }
        }
        PrevData();
        b++;
        a++;
   }

回答1:


http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.ontextchanged.aspx

A TextBox control must persist some values between posts to the server for this event to work correctly. Be sure that view state is enabled for this control.

Try enabling ViewState for TextBox.




回答2:


I think what's probably happening is that ASP.NET relies on ViewState to decide whether the TextBox's value has changed. But you've disabled ViewState on this control. I'm not sure why it can't use ControlState instead, but you could try enabling ViewState for this TextBox and see if this has an effect.

The MSDN page on the OnTextChanged event mentions this.



来源:https://stackoverflow.com/questions/6215046/textbox-textchanged-event-always-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!