2

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();
3
  • 1
    could you give a SSCCE (sscce.org) and please explain what you tried, what failed and what your expected result was Commented Dec 15, 2011 at 13:40
  • Are you trying to add an event handler to the button so that it does something when clicked? If so are you looking for server side or client side? Or are you just trying to change the text/identifier of the button based on which message it relates to? Commented Dec 15, 2011 at 14:24
  • Yes, It just make the lblbody correlating to the subject the user clicked to be visible. It is essentially a message system. Commented Dec 15, 2011 at 14:25

1 Answer 1

1

Could you do something as simple as this?

btnView.Click += (sender, e) => {
    lblbody.Visible = true;
};
Sign up to request clarification or add additional context in comments.

1 Comment

I will attempt tomorrow. Thanks. The only problem is assigning a specific value for each message. So when you click the button, it takes the subject it is related with and displays the body. I could use sql, I am not entirely sure yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.