动态创建控件

时间:2013-08-14 18:11:54

标签: c# asp.net

我正在为一个课堂做一个项目,但无法弄清楚这个让我夜不能寐的小事。我正在动态创建一个页面,它为数据库中的每个条目创建一个面板。现在的问题是我使用一个按钮来显示每个面板上的所有按钮。我需要能够知道按下了什么按钮,这样我就可以将会话中的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 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:1)

您需要了解ASP.NET Life Cycle。您需要在Page_Init()阶段创建动态控件,而不是Page_Load()。

正在发生的事情是您在Page_Load中创建控件,当您单击按钮并发回帖子时,您的动态控件将被销毁。控件的视图状态是在Page_Init之后但在Page_Load之前创建的。因此,您动态创建的控件不会保留其值(例如ID)。

查看this pagethis page,了解我正在谈论的内容。

答案 2 :(得分:0)

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

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

答案 3 :(得分: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");
}