gpt4 book ai didi

c# - ASP.NET 如何有效地在下拉列表中添加整数

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:32 26 4
gpt4 key购买 nike

我正在为一个婚姻网站制作个人资料页面。我在 make profile page 中使用这些功能。有什么方法可以优化这些功能吗?它们是在 ASP.NET 页面的页面加载事件中在非 IsPostBack 条件下调用的,并且迭代次数过多。

private void LoadTime()
{
string text = string.Empty;
//hour
for (int i = 0; i < 12; i++)
{
text = (i + 1 < 10) ? "0" + (i + 1).ToString() : (i + 1).ToString();
ListItem li = new ListItem(text, i.ToString());
ddlHour.Items.Add(li);
}
//min
for (int i = 0; i < 60; i++)
{
text = (i < 10) ? "0" + i.ToString() : i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlMin.Items.Add(li);
}
}

private int loadfromage()
{
int from = (rbtnMale.Checked && !rbtnFemale.Checked) ? 18 : 21;
//from age
for (int i = from; i <= 49; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlFromAge.Items.Add(li);
}
return from;
}

private void loadtoage(int fromage)
{
//to age
for (int i = fromage; i <= 50; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlToAge.Items.Add(li);
}
}

private void loadfromheight()
{
//from height
for (int i = 4; i <= 7; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlppFromHeightFeet.Items.Add(li);
}
for (int i = 0; i <= 11; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlppFromHeightInches.Items.Add(li);
}

}

private int feetToInches(int feet, int inches)
{
return (feet * 12) + inches;
}


private void loadtoheight(int inches)
{
int feet = inches / 12;
inches %= 12;
//to height
for (int i = feet; i <= 8; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlppToHeightFeet.Items.Add(li);
}
for (int i = inches; i <= 11; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlppToHeightInches.Items.Add(li);
}

}

最佳答案

  private void loadtoage(int fromage)
{
//to age
for (int i = fromage; i <= 50; i++)
{
string text = i.ToString();// convert i to string just once. you are converting it again in next line. boxing and unboxing is expensive when it comes to performance.
ListItem li = new ListItem(text, i.ToString());//Rather than doing i.ToString() again, you can directly pass text. Its all same.
ddlToAge.Items.Add(li);
}
}

我看到您在很多地方都这样做了。

关于c# - ASP.NET 如何有效地在下拉列表中添加整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35726965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com