这是我的页面加载:
protected void Page_Load(object sender, EventArgs e)
{
Table table = this.GetTable();
PlaceHolder1.Controls.Add(table);
}
private Table GetTable()
{
var table = new Table() { Width = System.Web.UI.WebControls.Unit.Percentage(100.00) };
var row = new TableRow();
row.Cells.Add(new TableCell() { Text = "Weapon", BackColor = System.Drawing.Color.Gold, ForeColor = System.Drawing.Color.Black });
row.Cells.Add(new TableCell() { Text = "Cost(of each resource)", BackColor = System.Drawing.Color.Gold, ForeColor = System.Drawing.Color.Black });
row.Cells.Add(new TableCell() { Text = "Power Per Soliders", BackColor = System.Drawing.Color.Gold, ForeColor = System.Drawing.Color.Black });
row.Cells.Add(new TableCell() { Text = "Amount To Buy", BackColor = System.Drawing.Color.Gold, ForeColor = System.Drawing.Color.Black });
row.Cells.Add(new TableCell() { Text = "Currently Have", BackColor = System.Drawing.Color.Gold, ForeColor = System.Drawing.Color.Black });
table.Rows.Add(row);
var Weap = new List<Weapon>();
//////////////////////////////
Dictionary<string, string> dic2 = (Dictionary<string, string>)Session["CurrentUser"];
string ArmyName = dic2["UserName"].ToString();
SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=true");
SqlCommand cmd = new SqlCommand("SELECT WeaponN,WeaponName, Cost, AttackPAdd FROM Weapons WHERE Class=@class", sqlConnection1);
cmd.Connection = sqlConnection1;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@class", "Attack");
SqlCommand cmdd = new SqlCommand();
cmdd.Parameters.AddWithValue("@zip", ArmyName);
cmdd.CommandType = CommandType.Text;
cmdd.Connection = sqlConnection1;
sqlConnection1.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
int number = reader.GetInt32(0);
string Weapon = reader.GetString(1);
int golda = reader.GetInt32(2);
int attack = reader.GetInt32(3);
var u1 = new Weapon { number = number + "", WeaponN = Weapon, Cost = golda + "", AttackPAdd = attack + "" };
Weap.Add(u1);
}
cmdd.Parameters.AddWithValue("@name", "");
foreach (Weapon u in Weap)
{
row = new TableRow();
row.Cells.Add(new TableCell() { Text = u.WeaponN });
row.Cells.Add(new TableCell() { Text = u.Cost });
row.Cells.Add(new TableCell() { Text = u.AttackPAdd });
var Textbox = new TextBox()
{
ID = u.number,
Text = "0"
};
var TextBoxCell = new TableCell();
TextBoxCell.Controls.Add(Textbox);
row.Cells.Add(TextBoxCell);
cmdd.Parameters["@name"].Value = u.number;
cmdd.CommandText = "SELECT Amount FROM [Weapon-User] WHERE ArmyName=@zip and WeaponN=@name";
int have = Convert.ToInt32(cmdd.ExecuteScalar());
row.Cells.Add(new TableCell() { Text = "Currently Have: " + have });
var button = new Button()
{
ID = "btt"+u.number,
Text = "Buy"
};
button.Command += AttClick_Clicks;
button.CommandArgument = u.number;
var buttonCell = new TableCell();
buttonCell.Controls.Add(button);
row.Cells.Add(buttonCell);
table.Rows.Add(row);
}
return table;
}
这是错误的onclick事件:
protected void AttClick_Clicks(object sender, CommandEventArgs e)
{
string name = e.CommandArgument as String;
int num = int.Parse(name);
Dictionary<string, string> dic2 = (Dictionary<string, string>)Session["CurrentUser"];
string ArmyName = dic2["UserName"].ToString();
SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=true");
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("@zip", ArmyName);
cmd.Parameters.AddWithValue("@num", num);
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.CommandText = "SELECT COUNT(*) FROM [Weapon-User] WHERE ArmyName=@zip and WeaponN=@num";
int check = Convert.ToInt32(cmd.ExecuteScalar());
if (check > 0)
{
////////////////////// problem accures here
TextBox myTextBox = (TextBox)FindControl(name);
string value = myTextBox.Text;
///////////////////
int add = int.Parse(value);
cmd.CommandText = "SELECT Amount FROM [Weapon-User] WHERE ArmyName=@zip and WeaponN=@num";
int amount = Convert.ToInt32(cmd.ExecuteScalar());
amount = amount + add;
cmd.CommandText = "SELECT Cost FROM Weapons WHERE WeaponN=@num";
int cost = Convert.ToInt32(cmd.ExecuteScalar());
cost = cost * add;
int golda = 0;
cmd.CommandText = "SELECT Gold FROM [Users] where ArmyName=@zip";
golda = Convert.ToInt32(cmd.ExecuteScalar());
///////
int irona = 0;
cmd.CommandText = "SELECT Iron FROM [Users] where ArmyName=@zip";
irona = Convert.ToInt32(cmd.ExecuteScalar());
///////
int wooda = 0;
cmd.CommandText = "SELECT Wood FROM [Users] where ArmyName=@zip";
wooda = Convert.ToInt32(cmd.ExecuteScalar());
///////
if (golda >= cost && irona >= cost && wooda >= cost)
{
cmd.Parameters.AddWithValue("@new", amount);
cmd.CommandText = "UPDATE [Weapon-User] SET Amount=@new WHERE ArmyName=@zip and WeaponN=@num";
cmd.ExecuteScalar();
cmd.Parameters.AddWithValue("@gg", golda - cost);
cmd.Parameters.AddWithValue("@ww", wooda - cost);
cmd.Parameters.AddWithValue("@ii", irona - cost);
cmd.CommandText = "UPDATE User SET Gold=@gg,Wood=@ww,Iron=@ii WHERE ArmyName=@zip";
cmd.ExecuteScalar();
}
else
{
string message = "You dont have enough resources";
string url = "Shop.aspx";
string script = "{ alert('";
script += message;
script += "');";
script += "window.location = '";
script += url;
script += "'; }";
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "alert", script, true);
}
}
else
{
TextBox myTextBox = (TextBox)FindControl(name);
string value = myTextBox.Text;
int add = int.Parse(value);
cmd.CommandText = "SELECT Cost FROM Weapons WHERE WeaponN=@num";
int cost = Convert.ToInt32(cmd.ExecuteScalar());
cost = cost * add;
int golda = 0;
cmd.CommandText = "SELECT Gold FROM [Users] where ArmyName=@zip";
golda = Convert.ToInt32(cmd.ExecuteScalar());
///////
int irona = 0;
cmd.CommandText = "SELECT Iron FROM [Users] where ArmyName=@zip";
irona = Convert.ToInt32(cmd.ExecuteScalar());
///////
int wooda = 0;
cmd.CommandText = "SELECT Wood FROM [Users] where ArmyName=@zip";
wooda = Convert.ToInt32(cmd.ExecuteScalar());
///////
if (golda >= cost && irona >= cost && wooda >= cost)
{
cmd.Parameters.AddWithValue("@new", add);
cmd.CommandText = "INSERT INTO [Weapon-User] (ArmyName,WeaponN,Amount,Class) Values (@zip,@num,@new,'Attack')";
cmd.ExecuteScalar();
cmd.Parameters.AddWithValue("@gg", golda - cost);
cmd.Parameters.AddWithValue("@ww", wooda - cost);
cmd.Parameters.AddWithValue("@ii", irona - cost);
cmd.CommandText = "UPDATE User SET Gold=@gg,Wood=@ww,Iron=@ii WHERE ArmyName=@zip";
cmd.ExecuteScalar();
}
else
{
string message = "You dont have enough resources";
string url = "Shop.aspx";
string script = "{ alert('";
script += message;
script += "');";
script += "window.location = '";
script += url;
script += "'; }";
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "alert", script, true);
}
}
Response.Redirect(Request.RawUrl);
}
并且在这里我只需要添加详细信息但它必须这样我可以发布它,所以你将能够看到代码 在这里我只需要添加详细信息,但必须如此,我可以发布它,所以你将能够看到代码
答案 0 :(得分:0)
您可以使用Page.FindControl()
并将结果转换为正确的值(方法返回值为`System.Web.UI.Control):
TextBox myTextBox = (TextBox)FindControl(e.CommandArgument.ToString());
获取值(Text
属性):
string value = myTextBox.Text;
编辑:FindControl
没有搜索控件层次结构;根据您更新的问题,您的文本框似乎位于Table控件中,该控件位于占位符控件中。您需要遍历表格行以查找正确的文本框。有关详细信息,请参阅this MSDN article,特别是标题为&#34;在命名容器层次结构中定位控件的部分&#34;
答案 1 :(得分:0)
您不希望为按钮和文本框提供相同的ID,这在.Net中无效。此外,你必须弄清楚要在FindControl中使用哪个容器来获取值,但是或多或少都是这样的..
var Textbox = new TextBox()
{
ID= "txt" + u.number,
Text = "0"
};
var button = new Button()
{
ID = "btn" + u.number,
Text = "Buy"
};
if (check > 0)
{
//Get here the textbox value of the textbox that her id = (e.CommandArgument as string)
var id = (sender as Button).ID.Replace("btn","txt");
var txtBox = (TextBox)Page.FindControl(id);
var value = txtBox.Text;
}