Det
Det

Reputation: 39

How to create method for a dynamically added button. asp.net C#

I created a button that is supposed to view a message in a updatepanel. I dynamically added through code since the ammount of buttons are relative to how many messages they recieve. I need the button to display a label. Any Ideas?

Here is my code: I feel like the problem that the scope is limited to the loop. I was going to change the id to increase "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);





    }

The only thing I have tried was to set a click event for the button taking the subject lbl.text to a global variabe and then with the click of another button, would compare the subject field with the database and display the 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();

Upvotes: 2

Views: 929

Answers (1)

Andy Christianson
Andy Christianson

Reputation: 371

Could you do something as simple as this?

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

Upvotes: 1

Related Questions