如何使用for循环代码在C#ASP.NET中获取n个HTML按钮 [英] How to get n number of HTML buttons in C# ASP.NET using for loop code behind

查看:79
本文介绍了如何使用for循环代码在C#ASP.NET中获取n个HTML按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望根据SQL Server数据库的计数动态创建N个按钮。在我的代码中,如果SQL Server中有10个值,那么10次按钮应该出现在页面加载中。



我尝试过:



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

推荐答案

您只创建了一个按钮,您正在更改ID并添加多次。你需要为循环的每次迭代创建一个新按钮。



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);
}


这篇关于如何使用for循环代码在C#ASP.NET中获取n个HTML按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆