if(!IsPostBack)不能使用第二语言

时间:2015-07-28 08:24:57

标签: c# select count oledb ispostback

我的页面中有2个表格。 我可以在这两者之间进行选择,以选择语言。

在我的代码中我有这个:

    public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        username.Text = "[" + HttpContext.Current.User.Identity.Name + "]"; // identifica o user ligado
        useren.Text = "[" + HttpContext.Current.User.Identity.Name + "]";

        if (!IsPostBack)
        {
            string sFilePath = Server.MapPath("Database3.accdb");
            OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
            using (Conn)
            {
                Conn.Open();
                OleDbCommand myCommand = new OleDbCommand("SELECT COUNT(*) FROM colaborador WHERE username=@username", Conn);
                myCommand.Parameters.Add("?", OleDbType.VarChar).Value = HttpContext.Current.User.Identity.Name;
                int totalRegistos = (int)myCommand.ExecuteScalar();
                if (totalRegistos > 0)
                {
                        // Já registado
                        lblInfo0.Text = "O username já existe na base de dados";
                        Label1.Text = "You already answered before";
                        empresa.Enabled = false;
                        empresa2.Enabled = false;
                        telemovel.Enabled = false;
                        cmdSave.Visible = false;
                        business.Enabled = false;
                        business2.Enabled = false;
                        mobile.Enabled = false;
                        cmdSave.Visible = false;
                }
            }
        }



    }

    // gravar na base de dados PT
    protected void cmdSave_Click(object sender, EventArgs e)
    {
        string sFilePath = Server.MapPath("Database3.accdb");
        OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
        string insertCmd = "INSERT INTO colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@username)";
        using (Conn)     // insere na tabela colaborador os campos empresa, empres2, user os valores @ 
        {
            Conn.Open();
            OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
            myCommand.Parameters.AddWithValue("@Empresa", empresa.Text);
            myCommand.Parameters.AddWithValue("@Empresa2", empresa2.Text);
            myCommand.Parameters.AddWithValue("@Telemovel", telemovel.Text);
            myCommand.Parameters.AddWithValue("@username", HttpContext.Current.User.Identity.Name);
            Response.Write(" ");
            myCommand.ExecuteNonQuery();
            lblInfo.Text = "Dados guardados!";
            lblInfo.ForeColor = System.Drawing.Color.Green;
        }
    }  




    // gravar na base de dados EN
    protected void cmdSave_Click2(object sender, EventArgs e)
    {
        string sFilePath = Server.MapPath("Database3.accdb");
        OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
        string insertCmd = "INSERT INTO colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@useren)";
        using (Conn)
        {
            Conn.Open();
            OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
            myCommand.Parameters.AddWithValue("@Empresa", business.Text);
            myCommand.Parameters.AddWithValue("@Empresa2", business2.Text);
            myCommand.Parameters.AddWithValue("@Telemovel", mobile.Text);
            myCommand.Parameters.AddWithValue("@useren", HttpContext.Current.User.Identity.Name); 
            myCommand.ExecuteNonQuery(); 
            Label1.Text = "Saved Successfull!";
            Label1.ForeColor = System.Drawing.Color.Green;
        }
    }
}

它正在运作。在这两种语言中,用户都可以插入数据库。使用主要语言,用户甚至可以知道他是否已经回答。虽然当我选择第二语言时,我无法获得if (!IsPostBack)

如何修复我的代码?

0 个答案:

没有答案