动态创建控件

本文关键字:控件 创建 动态 | 更新日期: 2023-09-27 18:27:54

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

动态创建控件

尝试添加自定义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");
}

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

实际情况是,您正在Page_Load中创建控件,当您单击某个按钮并返回时,您的动态控件将被销毁。控件的视图状态是在Page_Init之后但在Page_Load之前创建的。因此,您动态创建的控件没有保留它们的值(例如ID)。

查看本页和本页,了解我所说的内容。

您希望将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");
}

问题出在哪里?在事件处理程序btnView_Click(对象发送方,EventArgs e)中,发送方将是被单击的按钮。只需将sender强制转换为Control或Button=>(sender作为Control)或(sender为Button),然后从ID属性中检索carID。。(在页面加载中设置)-btnView.ID=CarID;

当然,这是假设您的页面视图状态运行良好:)-在asp.net 中动态添加控件可能会很棘手