我在webform上有10个文本框,我想使用C#为每个文本分配单独的guid。
答案 0 :(得分:2)
像...一样的东西。
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control MyControl in Form1.Controls)
{
TextBox MyTextBox = MyControl as TextBox;
if (MyTextBox != null)
{
MyTextBox.ID = Guid.NewGuid().ToString();
}
}
答案 1 :(得分:1)
你可以拥有
static class ControlExtension
{
public static IEnumerable<Control> GetAllControls(this Control parent)
{
foreach (Control control in parent.Controls)
{
yield return control;
foreach (Control descendant in control.GetAllControls())
{
yield return descendant;
}
}
}
}
并致电
this.GetAllControls().OfType<TextBox>().ToList().ForEach(t => t.ID = Guid.NewGuid().ToString());