我需要帮助。以下是我的代码。在这行代码中
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + 3;
而不是下一个方法中的3,当用户按下按钮时,这个Label37.Text值应该每次增加一次。所以我希望下一个方法中的这个标签值从点击按钮时的当前值开始计数一次。
protected void btnTest_Click(object sender, EventArgs e)
{
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");
newValue = old - 1;
// so i need label value from here to go in next method and increment++
Label37.Text = newValue.ToString();
decimal newIznos = oldIznos - Convert.ToDecimal(Cena1);
dtCurrentTable.Rows[clickedIndex].SetField("Kolicina", newValue.ToString());
dtCurrentTable.Rows[clickedIndex].SetField("VkIznos", newIznos.ToString());
}
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 sqlCmd = new SqlCommand();
// sqlCmd.CommandText = "SupaSpanak";
// sqlCmd.Connection = conn;
// sqlCmd.CommandType = CommandType.StoredProcedure;
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;
}
if (Label37.Text != "0")
{
//Here this current value of Label37.text need to be incremented for one each time when button is clicked.
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + 3;
}
答案 0 :(得分:0)
你需要viewstate,
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)
{
//other codes..
Counter = Counter + 1;
if (Label37.Text != "0")
{
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + Counter;
}
}