如何更改可见面板?

时间:2015-10-21 07:05:36

标签: c# asp.net

我想创建一个带有链接按钮的标签面板,我的代码是:

// create title panel 
for (int i = 0; i < 5; i++)
{
    Ctrl.Controls.Add(new LiteralControl(string.Concat("<li role=\"presentation\" class=\"rightcolomn\">")));
    LinkButton lb = new LinkButton();
    lb.ID = i.ToString();
    lb.Text =i.ToString();
    lb.Click += new EventHandler(this.lbnTitle_Click); 
    Ctrl.Controls.Add(lb);
    Ctrl.Controls.Add(new LiteralControl("</li>"));
}

// this code we create panels
for (int i = 0; i < 5; i++)
{
    Panel pn = new Panel();
    pn.ID = "p" + i.ToString();
    for (int j = 0; j < 3; j++)
    {
        //some code add to panel                    
    }
    pn.Visible = false;
    Ctrl.Controls.Add(pn);
}

// code for link button click is:
protected void lbnTitle_Click(object sender, EventArgs e)
{
    LinkButton ClickedLink = (LinkButton)sender;            
}

当我点击链接按钮时,我希望相应的面板设置为可见,如下所示:panel("p"+linkbutton.ID).Visible=true

如何访问面板并执行此操作?

Ctrlasp:PlaceHolder

2 个答案:

答案 0 :(得分:2)

不要仅将控制ID与数字一起使用,它们不太可能是唯一的,也不是很有意义。但是,请在FindControl上使用NamingContainer

protected void lbnTitle_Click(object sender, EventArgs e)
{
    LinkButton ClickedLink = (LinkButton) sender;
    Control container = ClickedLink.NamingContainer;
    Panel panel = (Panel) container.FindControl("p" + ClickedLink.ID);
    panel.Visible = true;
}

答案 1 :(得分:-1)

string placeHolderId = "...";
string panelId = "p" + linkbutton.ID; 

(this.Form.FindControl("placeHolderId").FindControl("panelId") as Panel).Visible=true;