将参数值从一个方法传递给另一个方法aspx.net

时间:2015-01-27 09:34:52

标签: asp.net

我在这里有点困惑,我搞砸了,帮忙。在这行代码中:

dtCurrentTable.Rows [0] [" Kolicina"] = Convert.ToInt32(Label37.Text)+ Counter1;

在接下来的两个方法中Button6_Click和Button7_Click这个Label37.Text值应该在每次用户按下按钮时增加一个。所以我希望下一个方法中的这个标签值从每个按钮的当前值开始计数一次click.I使用viewState执行此操作,它只适用于其中一个按钮,在secound按钮上不起作用。在我看来它保留了Label37的先前值,但是每次点击btnTest_Click它都会在变量小数中有新值newValue.How如何解决这个问题?

      protected void btnTest_Click(object sender, EventArgs e)
     {

        if (Session["Markici"] != null)
        {

            var clickedRow = ((Button)sender).NamingContainer as GridViewRow;
            var clickedIndex = clickedRow.RowIndex;

            decimal old = dtCurrentTable.Rows[clickedIndex].Field<decimal>("Kolicina");

            decimal oldIznos = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkIznos");
            decimal VkDanok = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkDanok");
            string Cena1 = dtCurrentTable.Rows[clickedIndex].Field<string>("Cena1");
            int TarifaID = dtCurrentTable.Rows[clickedIndex].Field<Int16>("TarifaID");


            decimal newValue = old - 1; //
           Label37.Text = newValue.ToString();



            decimal newIznos = oldIznos - Convert.ToDecimal(Cena1);

            dtCurrentTable.Rows[clickedIndex].SetField("Kolicina", newValue.ToString());      
            dtCurrentTable.Rows[clickedIndex].SetField("VkIznos", newIznos.ToString());


            if (old == 0)
            {
                dtCurrentTable.Rows[clickedIndex].Delete();
                dtCurrentTable.AcceptChanges();

            }
            GridView2.DataSource = dtCurrentTable;
            GridView2.DataBind();
        }

         public int Counter
        {
        get
        {
            if (ViewState["Counter"] != null)
            {
                return Convert.ToInt32(ViewState["Counter"]);
            }

            else
                return 0;
        }
        set { ViewState["Counter"] = value; }
    }

    protected void Button6_Click(object sender, EventArgs e)
    {

        SqlConnection conn = new      SqlConnection(ConfigurationManager.ConnectionStrings["string2"].ConnectionString);

        conn.Open();

        ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;


        SqlCommand cmd1 = new SqlCommand("SELECT pkid,Artikal,Vid,EdMera,TarifaID,Cena1 FROM Artikli WHERE Artikal= 'N KREM SUPA OD SPANA]++' AND Cena1 = 130.00", conn);
        //brojac za kolicina 
        count++;
        decimal noofcount = count;
        Session["kolicina"] = noofcount;
        Label5.Text = Session["kolicina"].ToString();//ViewState["count"].ToString();//

        SqlDataReader reader = cmd1.ExecuteReader();

        if (reader.Read())
        {
            Label9.Text = (string)reader["pkid"].ToString();
            Label3.Text = (string)reader["Artikal"].ToString();
            Label23.Text = (string)reader["Vid"].ToString();           
            Label10.Text = (string)reader["EdMera"].ToString();
            Label7.Text = (string)reader["TarifaID"].ToString();               
            Label4.Text = (string)reader["Cena1"].ToString();



            decimal cena = Convert.ToDecimal(Label5.Text);//kolicina
            decimal kolicina = Convert.ToDecimal(Label4.Text);//cena

            //vkIznos

            decimal mnoz = cena * kolicina;
            Label6.Text = mnoz.ToString();
            Convert.ToDecimal(Label6.Text);


        }

        conn.Close();

        DataTable dtCurrentTable = (DataTable)ViewState["Markici"];

        if (Label37.Text == "0")
        {
            dtCurrentTable.Rows[0]["Kolicina"] = Label5.Text;
        }
        else
        {
            Counter = Counter + 1;

            dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + Counter;
        }

        dtCurrentTable.Rows[0]["Artikal"] = Label3.Text;
        dtCurrentTable.Rows[0]["Cena1"] = Label4.Text;
        dtCurrentTable.Rows[0]["VkIznos"] = Label6.Text;
        dtCurrentTable.Rows[0]["VkDanok"] = Label8.Text;
        dtCurrentTable.Rows[0]["EdMera"] = Label10.Text;
        dtCurrentTable.Rows[0]["ArtikalID"] = Label9.Text;
        dtCurrentTable.Rows[0]["Vid"] = Label23.Text;
        dtCurrentTable.Rows[0]["TarifaID"] = Label7.Text;

        dtCurrentTable.AcceptChanges();
        GridView2.DataSource = dtCurrentTable;
        GridView2.DataBind();
        RemoveDuplicates(dt);

    }

       public int Counter1
    {
        get
        {
            if (ViewState["Counter1"] != null)
            {
                return Convert.ToInt32(ViewState["Counter1"]);
            }

            else
                return 0;
        }
        set { ViewState["Counter1"] = value; }
    }
    protected void Button7_Click(object sender, EventArgs e)
    {



        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["string2"].ConnectionString);
        conn.Open();

        SqlCommand cmd2 = new SqlCommand("SELECT pkid,Artikal,Vid,EdMera,TarifaID,Cena1 FROM Artikli WHERE Artikal= 'N KREM SUPA OD SVE@I PE^URKI++' AND Cena1 = 160.00", conn);

        SqlDataReader reader = cmd2.ExecuteReader();
        count1++;
        float noofcount = count1;
        Session["kolicina2"] = noofcount;
        Label11.Text = Session["kolicina2"].ToString(); //ViewState["count"].ToString(); //

        if (reader.Read())
        {
            Label35.Text = (string)reader["pkid"].ToString();
            Label12.Text = (string)reader["Artikal"].ToString();
            Label33.Text = (string)reader["Vid"].ToString();
            Label34.Text = (string)reader["EdMera"].ToString();
            Label14.Text = (string)reader["TarifaID"].ToString();
            Label13.Text = (string)reader["Cena1"].ToString();




            //iznos
            decimal mnoz = Convert.ToDecimal(Label11.Text) * Convert.ToDecimal(Label13.Text);
            Label17.Text = mnoz.ToString();
            decimal.Parse(Label17.Text);

            decimal cena;
            cena = Convert.ToDecimal(Label13.Text);
            decimal kolicina;
            kolicina = Convert.ToDecimal(Label11.Text);
            decimal iznos;
            iznos = Convert.ToDecimal(Label17.Text);

            Session["cena1"] = Label13.Text;
            Session["iznos1"] = Label17.Text;






            conn.Close();
        }

        DataTable dtCurrentTable = (DataTable)ViewState["Markici"];

        if (Label37.Text == "0")
        {
            dtCurrentTable.Rows[0]["Kolicina"] = Label11.Text;
        }
        else
        {
            Counter1 = Counter1 + 1;

            dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + Counter1;// Label37.Text;//Convert.ToInt32(Label37.Text) + 3;
        }

        dtCurrentTable.Rows[0]["Artikal"] = Label12.Text;
        dtCurrentTable.Rows[0]["Cena1"] = Label13.Text;
        dtCurrentTable.Rows[0]["VkIznos"] = Label17.Text;
        dtCurrentTable.Rows[0]["VkDanok"] = Label18.Text;
        dtCurrentTable.Rows[0]["EdMera"] = Label34.Text;
        dtCurrentTable.Rows[0]["ArtikalID"] = Label35.Text;
        dtCurrentTable.Rows[0]["Vid"] = Label33.Text;
        dtCurrentTable.Rows[0]["TarifaID"] = Label14.Text;

        dtCurrentTable.AcceptChanges();
        GridView2.DataSource = dtCurrentTable;
        GridView2.DataBind();
        RemoveDuplicates(dt);
    }

0 个答案:

没有答案