Click here to Skip to main content
14,825,178 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using a dynamic buttons which are genertated on the click of a asp calender date and i want to write code on the click event of the dynamcic controle But now when i click on the button the button disapeares.

I also called this event on the page load

What I have tried:

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Doctor();
        Department();
        Bind();
        FillAppointmnetGrid();
        month.Visible = false;
        DateTime sDate = Calendar1.SelectedDate;
        string doc = ddlDoctor.SelectedValue.ToString();
        GenerateDocSlotLabels(doc, sDate);
    }
    //this.BindGrid();
}

protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
    try
    {
        DateTime sDate = Calendar1.SelectedDate;
        string doc = ddlDoctor.SelectedValue.ToString();
        GenerateDocSlotLabels(doc, sDate);
    }
    catch (Exception ex) { throw new Exception(ex.ToString()); }
}

protected void GenerateDocSlotLabels(string doc, DateTime sDate)
{
    doc = ddlDoctor.SelectedValue.ToString();
    sDate = Calendar1.SelectedDate;
    string sDay = sDate.ToString("MM/dd/yyyy");
    DataTable dt = tsbll.GetAvailableSlotsbyDocAndDate(doc, sDay);

    foreach (DataRow dr in dt.Rows)
    {
        Button label = new Button();
        label.Text = dr["SlotTime"].ToString();
        //label.Style["Left"] = "20px";
        label.CssClass = "btn btn-primary";
        //label.CssClass = "background - color: #19bc49;";
        label.Click += new EventHandler(labelClick);
        pnlLabel.Controls.Add(label);
    }
}
Posted
Updated 11-Dec-17 2:42am
v2
Comments
F-ES Sitecore 11-Dec-17 8:58am
   
Controls you create yourself are not automatically persisted so they disappear on post-back, you need to re-add them on every page load making sure to keep their ID the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900