C# 将字符串值转换为占位符id
作为一名新手,我尝试了几次谷歌搜索,发现了一些令人困惑的答案。我努力实现的目标是:C# 将字符串值转换为占位符id,c#,string,asp.net-placeholder,C#,String,Asp.net Placeholder,作为一名新手,我尝试了几次谷歌搜索,发现了一些令人困惑的答案。我努力实现的目标是: 点击一个按钮(众多按钮中的一个) 提取该按钮的文本值,然后 使用该值可使相关占位符可见 到目前为止,我已经完成了前两个步骤,但如何完成第三个步骤?到目前为止,如果我点击Asia按钮,我的代码是: protected void btnArea_Click(object sender, EventArgs e) { string ar = (sender as Button).Text; //ar =
Asia
按钮,我的代码是:
protected void btnArea_Click(object sender, EventArgs e)
{
string ar = (sender as Button).Text;
//ar = "Asia";
phdasia.Visible = true;
}
简单地说,对于新手来说,我必须插入什么来代替phdasia?如果占位符控件共享相同的名称格式,您可以通过名称访问它们:
protected void btnArea_Click(object sender, EventArgs e)
{
string ar = (sender as Button).Text;
//ar = "Asia";
string name = "phd" + ar.ToLower(); // The naming format comes here
Control[] controls = this.Controls.Find(name, true); //find the control(s) by name
foreach(Control control in controls) // mow loop and make them visible
control.Visible = true;
//phdasia.Visible = true;
}
编辑:或者,您可以使用FindControl
方法在包含页面上查找ID属性为“phdasia”
的控件:
Control control = FindControl(name);
if(control!=null)
control.Visible = true;
非常感谢。它不仅有效,而且我理解它!