2016-07-31 181 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; 
       } 
      } 
     } 
    }