简体   繁体   中英

Onclick of dynamic Linkbutton not firing

I've got a calendar control where I click on a day to give me a javascript popup to add a training class. On each day there might be multiple training classes. To add the classes is no problem. Then on another page I've got this exact calendar control, but with the training classes populated in each respective day with dynamic linkbuttons. On the click of the linkbutton I want to retrieve the ID of that training class which will be used to populate labels and use this ID as a reference when the manager request training for his/her employees (when inserting into sql).

I have tried adding an EventHandler, but this is not firing. I have tried using an Click attribute to this linkbutton (but cannot call a method from an attribute)... I'm not sure if I have missed something that the eventhandler is not firing, could anybody please help me on this?

This is what I have done: DayRendering of calendar control:

    protected void calSchedule_DayRender(object sender, DayRenderEventArgs e)
    {
        bool result = false;

        string filterstring = "SELECT tos.[ID], sc.ServiceCenter, tos.Description " +
                              "FROM [SOS].[dbo].[TrainingOfficerSchedule] tos " +
                              "INNER JOIN [dbo].[TrainingOfficerServiceCenters] sc " +
                              "on tos.ServiceCenterRef = sc.ID " +
                              "WHERE TrainingDate = @TrainingDate";

        cn = new SqlConnection(GetConnectionString());
        SqlCommand myCmd = new SqlCommand(filterstring, cn);
        myCmd.Parameters.AddWithValue("@TrainingDate", e.Day.Date.ToShortDateString());

        cn.Open();
        SqlDataReader myReader = myCmd.ExecuteReader();

        if (myReader.HasRows)
        {
            while (myReader.Read())
            {
                string training = myReader["ServiceCenter"].ToString() + " - " + myReader["Description"].ToString();

                LinkButton lnk = new LinkButton();
                lnk.ID = myReader["ID"].ToString();
                lnk.ForeColor = Color.FromArgb(64, 64, 64);
                lnk.BackColor = Color.FromArgb(192, 192, 192);

                lnk.Attributes.Add("onclick", "return getItems();");
                lnk.Click += new EventHandler(lnk_Click);
                  //lnk.Attributes.Add("onClick", "return getItems();");
                  //lnk.Attributes.Add("OnClientClick", "return getItems();");
                  //lnk.Attributes.Add("onclick", jsMethod(Convert.ToInt32(myReader["ID"])));
                  //lnk.Click += new EventHandler(this.lnk_Click);
                  //lnk.Atributes.Add("runat", "server");
                  //lnk.Attributes.Add("onclick", jsMethod(myReader["ID"].ToString()));
                lnk.Text = training + "<br/><br/>";
                lnk.Style.Add("cursor", "pointer");
                e.Cell.Controls.Add(lnk);
            }
        }
        cn.Close();
    } 

Event Handler to fire when clicking on Linkbutton:

    public void lnk_Click(object sender, EventArgs e)
    {
        string ctrlId = ((Control)sender).ID;
        txtMManagerName.Text = ctrlId;
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "popSchedular", "showSchedular();", true);
    }

Is there maybe something I am doing wrong?

Put down PlaceHolder where you want to display link button and then try.

<asp:PlaceHolder ID="ph" runat="server" />


LinkButton lnk = new LinkButton();
            lnk.ID = myReader["ID"].ToString();
            lnk.ForeColor = Color.FromArgb(64, 64, 64);
            lnk.BackColor = Color.FromArgb(192, 192, 192);
            lnk.Attributes.Add("onclick", "return getItems();");
            lnk.Click += new EventHandler(lnk_Click);
            lnk.Text = training + "<br/><br/>";
            lnk.Style.Add("cursor", "pointer");
            ph.Controls.Add(lnk);

protected void lnk_Click(object sender, EventArgs e)
{
    string ctrlId = ((Control)sender).ID;
    txtMManagerName.Text = ctrlId;
    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "popSchedular", "showSchedular();", true);
}

I've had this before with dynamic controls. Creating multiple items with the same object reference seems to mess things up. Try creating a class that inherits from LinkButton and use that in your read loop.

public class myButton : LinkButton
{
    public myButton(SqlCommand sqlCmd)
    {
        var myReader = (SqlDataReader) sqlCmd.ExecuteReader();
        var training = myReader["ServiceCenter"].ToString() + " - " + myReader["Description"].ToString();
        ID =myReader["ID"].ToString();
        ForeColor = Color.FromArgb(64, 64, 64);
        BackColor = Color.FromArgb(192, 192, 192);

        Attributes.Add("onclick", "return getItems();");
        Click += new EventHandler(lnk_Click);
        Text = training + "<br/><br/>";
        Style.Add("cursor", "pointer");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM