Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

javascript - how to retain dynamic created button after post back in asp web forms

i have given task that i have to create three tabs ,morning & day & evening inside morning tab i have to display button from 10:00 am to 12 :pm similarly for day and evening i have to make three tabs on page load with click of there button .i have done but getting error like all button disappear on click of any button.. please answer me how to do it and also how to select/deselect clicked button with color .

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
           // Response.Write(((Button)pnlButtons.FindControl("10-0")).);
        }
    }

 protected void Tab3_Click(object sender, EventArgs e)
    {

        List<Button> buttons = new List<Button>();
        for (int i = 4; i < 6; i++)
        {
            for (int j = 0; j < 60; j += 15)
            {


                Button btn = new Button();
                btn.Text = i + "-" + j;
                btn.ID = i + "-" + j;
                //btn.Click += new System.EventHandler(btntime_Click);

                btn.Click += btn_Click;
                pnlButtons.Controls.Add(btn);
                Label1.Text = "your sheduled time between 4 PM to 6 PM";
            }
       }

at aspx page

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Dynamic controls are not maintained on postback, so you would have to recreate them on every postback (e.g. in Page_Load ). An alternative would be to put the buttons in a Panel in the markup. That panel would have the Visible property set to false initially. In Tab3_Click, you could set Visible="true".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...