Mike Eghtebas
asked on
add event for dynamically generated buttons... asp.net c#
I have the following event:
protected void medBtn_Click(object sender, EventArgs e)
{
// do stuff
}
I want to include with the following code which generates number of buttons as required.
Question: Could you possibly show me how to add this click event to all the buttons the code generates?
Thank you.
protected void medBtn_Click(object sender, EventArgs e)
{
// do stuff
}
I want to include with the following code which generates number of buttons as required.
Question: Could you possibly show me how to add this click event to all the buttons the code generates?
Thank you.
static Panel myPanel2 = new Panel();
static string r_name = "Region 1";
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Region"] == null)
Session["Region"] = "Region 1";
r_name = (string)Session["Region"];//.ToString();
//String rName = rn;
Panel myPanel = new Panel();
myPanel = this.MedCenterPanel;
myPanel2 = myPanel;
using (SqlConnection connection = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True"))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT .. ORDER BY ...";
command.CommandType = CommandType.Text;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
int id = reader.GetInt32(0);
string btnCaption = reader.GetString(1);
makeButtonFor(id, btnCaption);
}
}
else
{
// so something else
}
reader.Close();
}
}
static private void makeButtonFor(int id, string caption)
{
Button b = new Button();
{
b.ID = "medButton" + id.ToString();
b.Text = caption;
var captionButton = (caption.Substring(0, 6) == "Region") ? "RegionButton" : "medCtrButtonStyle";
b.ControlStyle.CssClass = captionButton;
myPanel2.Controls.Add(b);
};
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for eghtebas's comment #a40256286
for the following reason:
Thank you.
Accepted answer: 0 points for eghtebas's comment #a40256286
for the following reason:
Thank you.
ASKER
BTW,
The answer for my last question was:
b.Click += new System.EventHandler(medCtr Submit_cli ck);
The answer for my last question was:
b.Click += new System.EventHandler(medCtr
The answer was correct - how to add an event handler to a dynamically created button. The author used the solution. Exact syntax could be found in textbooks.
ASKER
re: Exact syntax could be found in textbooks.
This fine but it didn't work, but the following worked.
b.Click += new System.EventHandler(medCtr Submit_cli ck);
perhaps because the solution provide was for generic events requiring no parameters.
now, all is good and I appreciated for your input.
Regards,
Mike
This fine but it didn't work, but the following worked.
b.Click += new System.EventHandler(medCtr
perhaps because the solution provide was for generic events requiring no parameters.
now, all is good and I appreciated for your input.
Regards,
Mike
ASKER
I guess by mistake, I had accepted my own solution. My apology if this was the case.
Mike
Mike
ASKER
I added your solution as line 10, but at the end of the line at "medCtrSubmit_click()", I got an error. The error complains about the missing arguments in "medCtrSubmit_click()":
'No verload for method 'medCtrSubmit_Click() takes 0 argument.'
I tired the following but it didn't work:
b.Click += new System.EventHandler(medCtr
Question: What the syntax for the argument ought to be?
Open in new window