Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create method for a dynamically added button. asp.net C#
    primarykey
    data
    text
    <p>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?</p> <p>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</p> <pre><code>$ 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("&lt;br /&gt;")); if (lblviewstate.Text == "notread") { pnlNewMess.Controls.Add(new LiteralControl("&lt;div class='clsmess' style='background-image:url('images/unread.png'); color:white;'&gt;")); } else { pnlNewMess.Controls.Add(new LiteralControl("&lt;div class='clsmess' style='background-image:url('images/read.png'); color:white;'&gt;")); } pnlNewMess.Controls.Add(lbldate); pnlNewMess.Controls.Add(lblsubject); pnlNewMess.Controls.Add(lblsender); pnlNewMess.Controls.Add(btnView); pnlNewMess.Controls.Add(new LiteralControl("&lt;/div&gt;")); pnlNewMess.Controls.Add(lblviewstate); pnlNewMess.Controls.Add(new LiteralControl("&lt;br /&gt;")); pnlView.Controls.Add(lblbody); pnlMess.Controls.Add(pnlNewMess); } </code></pre> <p>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.</p> <pre><code> btnview.text = lblsubject.text; </code></pre> <p></p> <pre><code>SqlCommand CMretMess = new SqlCommand("SELECT body FROM [message] WHERE subject='" + clsGlobals.myGlobals.strSub + "'", connection); lblBody.Text = CMretMess.ExecuteScalar().ToString(); connection.Close(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload