我正在为班级做一个项目,但无法弄清楚这个让我彻夜难眠的小豌豆。我正在动态创建一个页面,它为数据库中的每个条目创建一个面板。现在的问题是我为每个面板上的所有按钮使用一个按钮。我需要能够知道按下了什么按钮,以便我可以在会话中将 CarID 发送到另一个页面。
namespace SportBucks
{
public partial class Auction : System.Web.UI.Page
{
string GCarID = "";
protected void Page_Load(object sender, EventArgs e)
{
bool bWinnerID = false;
bool bWiningBet = false;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if ((string)Session["LoggedIn"] == null)
{
Response.Redirect("Default.aspx");
}
else
{
Service s = new Service();
int Total = s.CarsCount();
for (int loop = Total; loop > 0; loop--)
{
Panel Panel1 = new Panel();
Panel1.ID = "pnl" + loop.ToString();
Panel1.Style["position"] = "absolute";
Panel1.Style["left"] = "155px";
Panel1.Style["top"] = "250px";
Panel1.Style["Height"] = "200px";
Panel1.Style["Width"] = "1000px";
Panel1.BackColor = System.Drawing.Color.Black;
if (loop >= 2)
{
int Top = (250 * (loop - 1));
Panel1.Style["top"] = (250 + Top + 20).ToString() + "px";
}
form1.Controls.Add(Panel1);
string Details = s.LoadCars(loop);
if (Details != null)
{
string CarID = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(CarID + "|", "");
string Man = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(Man + "|", "");
string Class = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(Class + "|", "");
string Type = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(Type + "|", "");
string Description = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(Description + "|", "");
string Reserve = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(Reserve + "|", "");
string Starting = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(Starting + "|", "");
string Begun = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(Begun + "|", "");
Begun = Begun.Substring(0, Begun.IndexOf(" "));
string End = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(End + "|", "");
End = End.Substring(0, End.IndexOf(" "));
string WinnerID = "";
string WinningBet = "";
if (Details.Substring(0, 1) == "|")
{
bWinnerID = false;
Details = Details.Substring(1);
}
else
{
WinnerID = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(WinnerID + "|", "");
bWinnerID = true;
}
if (Details.Substring(0, 1) == "|")
{
Details = Details.Substring(1);
bWiningBet = false;
}
else
{
WinningBet = Details.Substring(0, Details.IndexOf("|"));
Details = Details.Replace(WinningBet + "|", "");
bWiningBet = true;
}
string PicUrl = Details.Substring(0, Details.IndexOf("|"));
int Counter = PicUrl.Split(';').Length - 1;
if (Counter >= 1)
{
Image image1 = new Image();
image1.ID = "img" + CarID;
image1.ImageAlign = ImageAlign.AbsMiddle;
image1.Visible = true;
string Pic1 = PicUrl.Substring(0, PicUrl.IndexOf(";"));
image1.ImageUrl = Pic1;
image1.Style["Left"] = "10px";
image1.Style["position"] = "absolute";
image1.Style["top"] = "10px";
image1.Height = 180;
image1.Width = 230;
Panel1.Controls.Add(image1);
}
Label label1 = new Label();
label1.ID = "lblDescription" + CarID;
label1.Text = Description;
label1.ForeColor = System.Drawing.Color.Lime;
label1.BorderStyle = BorderStyle.Groove;
label1.BorderColor = System.Drawing.Color.Violet;
label1.Style["Left"] = "500px";
label1.Style["position"] = "absolute";
label1.Style["top"] = "30px";
Panel1.Controls.Add(label1);
Label label2 = new Label();
label2.ID = "lblBegun" + CarID;
label2.Text = "Auction Start: " + Begun;
label2.ForeColor = System.Drawing.Color.Lime;
label2.Style["Left"] = "270px";
label2.Style["position"] = "absolute";
label2.Style["top"] = "30px";
Panel1.Controls.Add(label2);
Label label3 = new Label();
label3.ID = "lblEnd" + CarID;
label3.Text = "Auction End: " + End;
label3.ForeColor = System.Drawing.Color.Lime;
label3.Style["Left"] = "270px";
label3.Style["position"] = "absolute";
label3.Style["top"] = "50px";
Panel1.Controls.Add(label3);
Label label4 = new Label();
label4.ID = "lblReserve" + CarID;
label4.Text = "Reserve: " + Reserve;
label4.ForeColor = System.Drawing.Color.Lime;
label4.Style["Left"] = "270px";
label4.Style["position"] = "absolute";
label4.Style["top"] = "70px";
Panel1.Controls.Add(label4);
Label label5 = new Label();
label5.ID = "lblMan" + CarID;
label5.Text = "Manufacturer: " + Man;
label5.ForeColor = System.Drawing.Color.Lime;
label5.Style["Left"] = "270px";
label5.Style["position"] = "absolute";
label5.Style["top"] = "90px";
Panel1.Controls.Add(label5);
Label label6 = new Label();
label6.ID = "lblClass" + CarID;
label6.Text = "Class: " + Class;
label6.ForeColor = System.Drawing.Color.Lime;
label6.Style["Left"] = "270px";
label6.Style["position"] = "absolute";
label6.Style["top"] = "110px";
Panel1.Controls.Add(label6);
Label label7 = new Label();
label7.ID = "lblType" + CarID;
label7.Text = "Type: " + Type;
label7.ForeColor = System.Drawing.Color.Lime;
label7.Style["Left"] = "270px";
label7.Style["position"] = "absolute";
label7.Style["top"] = "130px";
Panel1.Controls.Add(label7);
if (bWinnerID == true)
{
Label label8 = new Label();
label8.ID = "lblWinnerID" + CarID;
label8.Text = "WinnerID: " + WinnerID;
label8.ForeColor = System.Drawing.Color.Lime;
label8.Style["Left"] = "270px";
label8.Style["position"] = "absolute";
label8.Style["top"] = "130px";
Panel1.Controls.Add(label8);
}
if (bWiningBet == true)
{
Label label9 = new Label();
label9.ID = "lblWinningBet" + CarID;
label9.Text = "WinningBet R: " + WinningBet;
label9.ForeColor = System.Drawing.Color.Lime;
label9.Style["Left"] = "270px";
label9.Style["position"] = "absolute";
label9.Style["top"] = "130px";
Panel1.Controls.Add(label9);
}
/*int View = s.TimesView(CarID);
Label label10 = new Label();
label10.ID = "lblView" + CarID;
label10.Text = "Times View: " + View;
label10.ForeColor = System.Drawing.Color.Lime;
label10.Style["Left"] = "1000px";
label10.Style["position"] = "absolute";
label10.Style["top"] = "130px";
Panel1.Controls.Add(label10);*/
Button btnView = new Button();
btnView.ID = CarID;
btnView.Text = "View Car";
btnView.ForeColor = System.Drawing.Color.DeepSkyBlue;
btnView.BackColor = System.Drawing.Color.Gray;
btnView.BorderColor = System.Drawing.Color.Violet;
btnView.Style["top"] = "110px";
btnView.Style["left"] = "800px";
btnView.Style["Height"] = "20px";
btnView.Style["position"] = "absolute";
btnView.BorderStyle = BorderStyle.Outset;
btnView.Click += new EventHandler(this.btnView_Click);
GCarID = CarID;
Panel1.Controls.Add(btnView);
}
}
}
}
void btnView_Click(object sender, EventArgs e)
{
Session["CarID"] = GCarID;
Response.Redirect("View.aspx");
}
}}