【问题标题】:Convert a string value into PlaceHolder id将字符串值转换为 PlaceHolder id
【发布时间】:2019-04-07 15:49:33
【问题描述】:

作为一个新手,我尝试了几次 Google 搜索,并找到了一些令人困惑的答案。我想要实现的是:

  1. 点击一个按钮(其中一个),

  2. 提取该按钮的文本值,然后

  3. 使用该值使相关占位符可见。

到目前为止,我已经完成了前 2 步,但如何完成第 3 步?到目前为止,如果我点击 Asia 按钮,我的代码是:

protected void btnArea_Click(object sender, EventArgs e)
{
    string ar = (sender as Button).Text;
    //ar = "Asia";
    phdasia.Visible = true;
}

简单来说,对新手友好,我必须插入什么来代替phdasia

【问题讨论】:

    标签: c# string asp.net-placeholder


    【解决方案1】:

    如果您的占位符控件共享相同的名称格式,您可以通过名称访问它们:

    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;
    

    【讨论】:

    • 谢谢。不仅有效而且我理解!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2023-03-10
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    相关资源
    最近更新 更多