Thursday, October 11, 2018

Help adding click event programmatically for C# aspx page

I am writing a page that will allow the user to click on a calendar control and schedule a service request. I'm using the DayRender event to add a button control to the days that have something scheduled based on what's stored in a SQL table. I want it so when a user clicks one of these button controls, it does stuff. This is what I have when I'm creating the control in the DayRender event.

using (SqlConnection sqlCon = new SqlConnection(conn))

{

using (SqlCommand selCmd = new SqlCommand())

{

selCmd.CommandText = "SELECT ServiceDate, VehicleID, SvcDesc FROM RepairSchedule WHERE PMFlag = 1";

selCmd.Connection = sqlCon;

sqlCon.Open();

SqlDataReader sqlrdr = selCmd.ExecuteReader();

while (sqlrdr.Read())

{

strServiceDate = sqlrdr["ServiceDate"].ToString();

strVehicleID = sqlrdr["VehicleID"].ToString();

strSvcDesc = sqlrdr["SvcDesc"].ToString();

if (!d.IsOtherMonth && d.Date == Convert.ToDateTime(strServiceDate).Date)

{

Button btn = new Button();

btn.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;

btn.Text = strVehicleID;

btn.Font.Name = "Arial";

btn.Font.Bold = true;

btn.ForeColor = System.Drawing.Color.FromName("White");

btn.BackColor = System.Drawing.Color.FromName("Red");

btn.ToolTip = strSvcDesc;

btn.Click += Button_Click;

c.Controls.Add(btn);

}

}

sqlCon.Close();

selCmd.CommandText = "SELECT ServiceDate, VehicleID, SvcDesc FROM RepairSchedule WHERE PMFlag = 0";

selCmd.Connection = sqlCon;

sqlCon.Open();

SqlDataReader sqlrdr1 = selCmd.ExecuteReader();

while (sqlrdr1.Read())

{

strServiceDate = sqlrdr1["ServiceDate"].ToString();

strVehicleID = sqlrdr1["VehicleID"].ToString();

strSvcDesc = sqlrdr1["SvcDesc"].ToString();

if (!d.IsOtherMonth && d.Date == Convert.ToDateTime(strServiceDate).Date)

{

Button btn = new Button();

btn.BorderStyle = System.Web.UI.WebControls.BorderStyle.None;

btn.Text = strVehicleID;

btn.Font.Name = "Arial";

btn.Font.Bold = true;

btn.ForeColor = System.Drawing.Color.FromName("White");

btn.BackColor = System.Drawing.Color.FromName("Blue");

btn.ToolTip = strSvcDesc;

btn.Click += Button_Click;

c.Controls.Add(btn);

}

}

sqlCon.Close();

}

}

However, the Button_Click event never fires. It has to be something with how I'm adding the event to the button control, but danged if I can figure it out.

Any ideas?

EDIT: also, can't get the formatting to work properly on Reddit for my code. Apologies.

Help adding click event programmatically for C# aspx page Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team