aspx c#。网站上的2x文本框无法正常工作

时间:2014-09-07 12:47:30

标签: c# button textbox

我在一个网站上有两个文本框和两个按钮。问题是第二个文本框和第二个按钮不起作用。第一个文本框+按钮运行良好:

    int NoOfDigTextBoxEngine;
    protected void TextBoxADDEngine_TextChanged(object sender, EventArgs e)
    {
        NoOfDigTextBoxEngine = TextBoxADDEngine.Text.Length;
    }

    protected void ButtonADDEngine_Click(object sender, EventArgs e)
    {

        String strConnString = ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT Engine, Created, LastChange, WhoInserted, WhoLastModified, Disable FROM PartsEngine";
            cmd.Connection = con;
            sda.SelectCommand = cmd;

            if (FillWhoIsLogged > 0)           // ----------------Wypełnianie tebeli kiedy jest ktos zalogowany--- //
            {
                if (NoOfDigTextBoxEngine == 7)
                {
                    try
                    {
                        Convert.ToInt32(TextBoxADDEngine.Text);

                        con.Open();
                        cmd.CommandText = ("INSERT INTO PartsEngine ([Engine], [Created], [WhoInserted], [Disabled]) VALUES ('" + TextBoxADDEngine.Text + "', GETDATE(), '" + FillWhoIsLogged + "', '1');");
                        cmd.ExecuteNonQuery();
                        GridView3.DataBind();
                        TextBoxADDEngine.Text = string.Empty;
                        con.Close();
                    }
                    catch
                    {
                        Response.Write("<script type='text/javascript'> alert('Nr Silnika może zawierać jedynie cyfry.')</script>");
                        TextBoxADDEngine.Text = string.Empty;
                    }
                }
                else
                {
                    Response.Write("<script type='text/javascript'> alert('Nr Silnika musi mieć 7 cyfr.Podano: " + NoOfDigTextBoxEngine + " ')</script>");
                    TextBoxADDEngine.Text = string.Empty;
                }
            }
        }
    }

但是第二个(是相同的)不想工作。

    int NoOfDigTextBoxGear;
    protected void TextBoxADDGear_TextChanged(object sender, EventArgs e)
    {
        NoOfDigTextBoxGear = TextBoxADDGear.Text.Length;
    }
    protected void ButtonADDGear_Click(object sender, EventArgs e)
    {

        String strConnString = ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT Gear, Created, LastChange, WhoInserted, WhoLastModified, Disable FROM PartsGear";
            cmd.Connection = con;
            sda.SelectCommand = cmd;

            if (FillWhoIsLogged > 0)           // ----------------Wypełnianie tebeli kiedy jest ktos zalogowany--- //
            {
                if (NoOfDigTextBoxGear == 7)
                {
                    try
                    {
                        Convert.ToInt32(TextBoxADDGear.Text);

                        con.Open();
                        cmd.CommandText = ("INSERT INTO PartsGear ([Gear], [Created], [WhoInserted], [Disabled]) VALUES ('" + TextBoxADDGear.Text + "', GETDATE(), '" + FillWhoIsLogged + "', '1');");
                        cmd.ExecuteNonQuery();
                        GridView5.DataBind();
                        TextBoxADDGear.Text = string.Empty;
                        con.Close();
                    }
                    catch
                    {
                        Response.Write("<script type='text/javascript'> alert('Nr Skrzyni może zawierać jedynie cyfry.')</script>");
                        TextBoxADDGear.Text = string.Empty;
                    }
                }
                else
                {
                    Response.Write("<script type='text/javascript'> alert('Nr Skrzyni musi mieć 7 cyfr. Podano: "+ NoOfDigTextBoxGear +" ')</script>");
                    TextBoxADDGear.Text = string.Empty;
                }
            }
        }
    }

当我在第二个文本框中写东西然后点击按钮 - 总是NoOfDigTextBoxGear = 0 ...为什么?

对我来说没有任何意义,因为这段代码(第二个文本框和按钮)与第一个(第一个文本框和按钮)相同。

1 个答案:

答案 0 :(得分:0)

哦......我刚看到这个:

    <asp:TextBox ID="TextBoxADDGear" runat="server" Visible="False" 
                 Width="96px"></asp:TextBox>

我没有添加ontextchanged(!)

                 ontextchanged="TextBoxADDGear_TextChanged"

现在一切都好。