Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want "N" number of buttons to be created dynamically based on the count from SQL server database. In my code if there is count of 10 values in SQL Server then 10 times buttons should appear in page load.

What I have tried:

HtmlButton btns = new HtmlButton();
for (int i = 1; i <= Convert.ToInt32(hdnCountData.Value); i++)
{
    string id = "btn" + i;
    btns.ID = id;
    btns.InnerText = i.ToString();
    btns.Attributes.Add("class", "circularButton");
    btns.Attributes.Add("type", "button");
    divBodyButtons.Controls.Add(btns);
}

In this i am getting only the last button not getting from first like an array where i am doing can anyone tell me
Posted
Updated 18-Nov-18 23:06pm

1 solution

You've only created one button which you're changing the ID of and adding multiple times. You need to create a new button for each iteration of the loop.

for (int i = 1; i <= Convert.ToInt32(hdnCountData.Value); i++)
{
    HtmlButton btns = new HtmlButton();
    string id = "btn" + i;
    btns.ID = id;
    btns.InnerText = i.ToString();
    btns.Attributes.Add("class", "circularButton");
    btns.Attributes.Add("type", "button");
    divBodyButtons.Controls.Add(btns);
}
 
Share this answer
 
Comments
Member 8583441 19-Nov-18 5:12am    
I will check it and I will tell you if there is any error or the output comes.
Member 8583441 19-Nov-18 5:16am    
My vote 5
Member 8583441 19-Nov-18 5:31am    
Next question, these buttons will be displayed right i.e, float right in css. I require fixed position in float right but fixed position is done but displaying in left side. How to do this tell me sir

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