2016-07-31 185 views
-1

我有項模板中繼器:asp.net中繼器FindControl不工作?

<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource1"> 
      <HeaderTemplate> 
       <table> 
      </HeaderTemplate> 
      <ItemTemplate> 
       <tr> 
        <td style="text-align:center;width:15%;vertical-align:top"> 
         <strong id="author" runat="server" style="color:brown;font-size:20px"><%# Eval("Username") %></strong> 
         <img src="<%# Eval("Avatar") %>" alt="Avatar" /> 
         <p>Create At: <%# Eval("createAt") %></p> 
        </td> 
        <td style="margin-left:5%;width:70%;vertical-align:top"> 
         <div id="contentTopic" runat="server"> 
         <p><%# Eval("TopicContent") %></p> 
         </div> 
        </td> 
        <td style="width:5%;vertical-align:bottom;margin-left:5%"> 
         <asp:Button id="btnSua" runat="server" Text="Update" CssClass="button" Height="30px" Visible="false"/> 
         <br /> 
         <br /> 
         <asp:Button id="btnXoa" runat="server" Text="Delete" CssClass="button" Height="30px" Visible="false"/> 
        </td> 
       </tr> 
      </ItemTemplate> 
      <FooterTemplate> 
       </table> 
      </FooterTemplate> 
     </asp:Repeater> 

我想使這些按鈕可見當會話[「帳戶」]的值等於<%#的eval(‘用戶名’)%>值但它似乎不起作用,按鈕仍然不可見。

我後面的代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["account"] != null) 
     { 
      Userzone.Text = Session["account"].ToString(); 
      Info.Visible = true; 
      Logout.Visible = true; 
      foreach (RepeaterItem item in Repeater4.Items) 
      { 
       HtmlGenericControl author = (HtmlGenericControl)item.FindControl("author"); 
       string Username = author.InnerText; 
       Button btnsua = item.FindControl("btnSua") as Button; 
       Button btnxoa = item.FindControl("btnXoa") as Button; 

       string account = Session["account"].ToString(); 
       if (Username == account) 
       { 
        btnsua.Visible = true; 
        btnxoa.Visible = true; 
       } 
       else 
       { 
        btnsua.Visible = false; 
        btnxoa.Visible = false; 
       } 
      } 
     } 
     else 
     { 
      Userzone.Text = "Login"; 
      Info.Visible = false; 
      Info.Visible = false; 
     } 
    } 

任何解決方案來處理呢?

+0

你試過調試呢?你是否獲得了作者變量的價值? – Tushar

+0

它正常運行,作者變量中的值也顯示。但不是顯示作者變量= Session [「account」]項中的按鈕,它不顯示。 –

+0

如果這段代碼無法正常工作,並且找不到按鈕,那麼您將得到一個空的異常(查看該代碼) - 所以在其他地方是您的問題! – Aristos

回答

1

與@Aristos同意,這將不檢查pageLoad的,所以我使用的ItemDataBound直放站運行:

protected void Repeater4_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) 
     { 
      HtmlGenericControl user =(HtmlGenericControl) e.Item.FindControl("author"); 
      string Username = user.InnerText.ToString(); 
      Button btnsua = e.Item.FindControl("btnSua") as Button; 
      Button btnxoa = e.Item.FindControl("btnXoa") as Button; 
      if (Session["account"] != null) 
      { 
       string account = Session["account"].ToString(); 
       if (Username == account) 
       { 
        btnsua.Visible = true; 
        btnxoa.Visible = true; 
       } 
       else 
       { 
        btnsua.Visible = false; 
        btnxoa.Visible = false; 
       } 
      } 
     } 
    }