1

I have been given a task in asp.net web form to dynamically generate Buttons / Labels with values of time intervals in 15 minute steps (10:00, 10:15, 10:30 ... 12:00) by using a loop.

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        CreateControl();
    }

    void CreateControl()
    {
        Button btn1 = new Button();
        btn1.ID = "btn1";
        btn1.Text = "click me";
        btn1.Click += new EventHandler(btn1_Click);
        div1.Controls.Add(btn1);
    }

    void btn1_Click(object sender, EventArgs e)
    {
        div1.InnerHtml += "btn1 was clicked";
    }
}
2
  • Check my answer below. It should work for you. Commented May 18, 2016 at 19:36
  • If any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the one who gave the answer and yourself. There is no obligation to do this. Commented May 19, 2016 at 17:42

3 Answers 3

2
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TimeSpan startTime = new TimeSpan(0, 0, 0);
        TimeSpan endTime = new TimeSpan(1, 0, 0);
        int i = 0; 

        While(startTime <= endTime)
        {
            i++;
            CreateControl(startTime.ToString(@"hh\:mm\:ss"), i);
            startTime += TimeSpan.FromMinutes(15); //Add 15 minutes
        }
    }


    void CreateControl(string printTime, increment)
    {
        Button btn1 = new Button();
        btn1.ID = "btn_" + increment;
        btn1.Text = printTime;
        btn1.Click += new EventHandler(btn_Click);
        div1.Controls.Add(btn1);
    }

    void btn_Click(object sender, EventArgs e)
    {
        string ID = (sender as Button).ID;
        div1.InnerHtml += "btn " + ID + " was clicked";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer, always more than one way to accomplish the same thing
@MattRowland, Thanks.
2
 void CreateControl()
    {
        TimeSpan FifteenMinutes = new TimeSpan(0, 15, 0);
        TimeSpan Time = new TimeSpan(0,0,0);
        int i = 1;
        while (Time.Hours != 24)
        {
            Button btn1 = new Button();
            btn1.ID = "btn"+i;
            btn1.CommandArgument = i.ToString();
            btn1.Text = "click me";
            btn1.Click += new EventHandler(btn1_Click);
            div1.Controls.Add(btn1);

            Label lbl = new Label();
            lbl.Text = Time.ToString();
            div1.Controls.Add(lbl);

            Time = Time.Add(FifteenMinutes);

            i++;
        }
    }

Comments

2

You can use a TimeSpan and increment the time by 15 minutes with each iteration.

int j = 0;
for (TimeSpan i = new TimeSpan(10, 0, 0); i.ToString(@"hh\:mm") != "12:00"; i = i.Add(new TimeSpan(0, 15, 0)), j++)
{
    Button btn1 = new Button();
    btn1.ID = "btn" + j;
    btn1.Text = i.ToString(@"hh\:mm");
    btn1.Click += new EventHandler(btn1_Click);
    div1.Controls.Add(btn1);

    Label lbl = new Label();
    lbl.Text = i.ToString(@"hh\:mm");
    div1.Controls.Add(lbl);

    Console.WriteLine(i.ToString(@"hh\:mm"));
}

void btn1_Click(object sender, EventArgs e)
{
    string buttonId = ((Button)sender).ID;
    div1.InnerHtml += "Button: " + buttonId + " was clicked";
}

3 Comments

You have missed the button click event. He would need to display which button was clicked. Correct answer though.
@vohrahul Good catch, I added that logic into the answer now.
And here you get a +1 for your answer sir. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.