如何为动态添加按钮创建方法。 asp.net C#

时间:2011-12-15 13:37:23

标签: c# asp.net forms dynamic web

我创建了一个按钮,用于在updatepanel中查看消息。 我通过代码动态添加,因为按钮的数量与他们收到的消息数量有关。我需要按钮来显示标签。任何想法?

这是我的代码: 我觉得范围仅限于循环的问题。我打算更改id以增加“lblbody”= 1 + = 1

$ while (reader.Read())
    {

        string strrecipient, strsender, strsubject, strbody, strdate, strviewstate;

        strdate = "Date Sent: " + reader["date"].ToString();
        strsender = "From: " + reader["sender"].ToString();
        strsubject = "Subject: " + reader["subject"].ToString();
        strbody = reader["body"].ToString();
        strrecipient = "To: " + reader["recipient"].ToString();
        if (reader["viewstate"].ToString() == "notread")
        {
            strviewstate = "UnRead";

        }
        else
        {
            strviewstate = "read";

        }
        string strName;
        int intName;
        intName = 0;
        strName = intName.ToString();


        Panel pnlNewMess = new Panel();
        pnlMess.Controls.Add(pnlNewMess);

        pnlNewMess.BorderColor = System.Drawing.Color.LightGray;
        pnlNewMess.BorderStyle = BorderStyle.Solid;
        pnlNewMess.BorderWidth = 1;


        Label lbldate = new Label();
        Label lblsender = new Label();
        Label lblsubject = new Label();
        Label lblbody = new Label();
        Label lblrecipient = new Label();
        Label lblviewstate = new Label();
        Button btnView = new Button();



        lbldate.Text = strdate;
        lblsender.Text = strsender;
        lblsubject.Text = strsubject;
        lblbody.Text = strbody;
        lblrecipient.Text = strrecipient;
        lblviewstate.Text = strviewstate;
        btnView.Text = "View Message";
        btnView.ID = strsubject;
        lblbody.Visible = false;
        lblrecipient.Visible = false;
        lblviewstate.Visible = false;
        //lblbody.ID = "lblBody" + strName;


        pnlNewMess.Controls.Add(lblrecipient);
        pnlNewMess.Controls.Add(new LiteralControl("<br />"));
        if (lblviewstate.Text == "notread")
        {
            pnlNewMess.Controls.Add(new LiteralControl("<div class='clsmess' style='background-image:url('images/unread.png'); color:white;'>"));
        }
        else
        {
            pnlNewMess.Controls.Add(new LiteralControl("<div class='clsmess' style='background-image:url('images/read.png'); color:white;'>"));

        }
        pnlNewMess.Controls.Add(lbldate);
        pnlNewMess.Controls.Add(lblsubject);
        pnlNewMess.Controls.Add(lblsender);

        pnlNewMess.Controls.Add(btnView);
        pnlNewMess.Controls.Add(new LiteralControl("</div>"));
        pnlNewMess.Controls.Add(lblviewstate);

        pnlNewMess.Controls.Add(new LiteralControl("<br />"));
        pnlView.Controls.Add(lblbody);

        pnlMess.Controls.Add(pnlNewMess);





    }

我唯一尝试的是为按钮设置click事件,将主题lbl.text转换为全局变量,然后单击另一个按钮,将主题字段与数据库进行比较并显示lblbody。

 btnview.text = lblsubject.text;

SqlCommand CMretMess = new SqlCommand("SELECT body FROM [message] WHERE subject='" + clsGlobals.myGlobals.strSub + "'", connection);
    lblBody.Text = CMretMess.ExecuteScalar().ToString();
    connection.Close();

1 个答案:

答案 0 :(得分:1)

你能做一件这么简单的事吗?

btnView.Click += (sender, e) => {
    lblbody.Visible = true;
};