0

我正在为班级做一个项目,但无法弄清楚这个让我彻夜难眠的小豌豆。我正在动态创建一个页面,它为数据库中的每个条目创建一个面板。现在的问题是我为每个面板上的所有按钮使用一个按钮。我需要能够知道按下了什么按钮,以便我可以在会话中将 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");
            }

}}
4

4 回答 4

1

尝试添加自定义 html 属性。

//后面的代码:

Button btnView = new Button();
btnView.ID = CarID;
//here
btnView.Attributes.Add("GCarID", 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);

//click
void btnView_Click(object sender, EventArgs e)
{
    Session["CarID"] = ((Button)sender).Attributes["GCarID"];
    Response.Redirect("View.aspx");
}
于 2013-08-14T18:55:22.850 回答
1

您需要了解ASP.NET 生命周期。您的动态控件需要在 Page_Init() 阶段而不是 Page_Load() 中创建。

发生的事情是您在 Page_Load 中创建控件,当您单击按钮并发生回发时,您的动态控件正在被破坏。控件的视图状态是在 Page_Init 之后、Page_Load 之前创建的。因此,您动态创建的控件不会保留它们的值(例如 ID)。

查看此页面此页面以基本了解我在说什么。

于 2013-08-14T19:33:31.803 回答
0

有什么问题?在您的事件处理程序 btnView_Click(object sender, EventArgs e) 中,发送者将是被点击的按钮。只需将 sender 转换为 Control 或 Button => (sender as Control) 或 (sender as Button) 并从 ID 属性中检索 carID .. (您在页面加载中设置) - btnView.ID = CarID;

这当然是假设您的页面视图状态工作正常:) - 这对于在 asp.net 中动态添加的控件可能会很棘手

于 2013-08-14T18:53:19.910 回答
0

您想将 Button 的 Command 事件与 CommandArgument 一起使用,而不是 Click 事件。

CommandArgument 用于存储参数,并在过去检索它。

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.Command += btnView_Command; // Note: Command event is used instead of Click.
btnView.CommandArgument = CarID; // Note: CarID is stored in CommandArgument.
Panel1.Controls.Add(btnView);

void btnView_Command(object sender, CommandEventArgs e)
{
    string carID = e.CommandArgument.ToString();
    Session["CarID"] = carID;
    Response.Redirect("View.aspx");
}
于 2013-08-14T19:14:42.570 回答