2015-10-21 59 views
1

我想創建一個Tab平板與鏈接按鈕,我的代碼是:如何更改可見面板?

// 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

不要使用控制的ID與數字而已,這是不可能的,他們將是獨一無二的,它也沒有多大意義。但是,在NamingContainer上使用FindControl

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
string placeHolderId = "..."; 
string panelId = "p" + linkbutton.ID; 

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