2013-11-09 72 views
2

我想隱藏已經添加到表格中的repeater元素,但現在我面臨一些問題,即使我爲比較條件做了一些操作,中繼器也會根據表格顯示出整體內容。這是我的編碼部分。希望會知道我犯的錯誤。隱藏帶條件的Repeater元素

設計師代碼:

<script language="C#" runat="server"> 
protected void ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     String personID = ""; 
     try 
     { 
      MySql.Data.MySqlClient.MySqlConnection connStr = new MySql.Data.MySqlClient.MySqlConnection(); 
      connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;"; 
      String searchPerson = "SELECT PersonIDB from contactFriend WHERE PersonID = @id"; 
      MySql.Data.MySqlClient.MySqlCommand cmdSearch = new MySql.Data.MySqlClient.MySqlCommand(searchPerson, connStr); 
      connStr.Open(); 
      cmdSearch.Parameters.AddWithValue("@id", "M000001"); 


      MySql.Data.MySqlClient.MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader(); 

      if (dtrRead2.Read()) 
      { 
       personID = (String)dtrRead2["PersonIDB"]; 
      } 

      dtrRead2.Close(); 
      connStr.Close();     
     } 
     catch (Exception ex) 
     { 
      System.Windows.Forms.MessageBox.Show(ex.ToString()); 
     } 

     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      Label lblID = (Label)e.Item.FindControl("lblID"); 
      string s = lblID.Text; 

      if (s == personID) 
      { 
       lblID.Visible = false; 
       e.Item.FindControl("photoImage").Visible = false; 
       e.Item.FindControl("lblName").Visible = false; 
       e.Item.FindControl("deleter").Visible = false; 
       e.Item.FindControl("lblEmail").Visible = false; 
      } 
      else 
      { 
       lblID.Visible = true; 
       e.Item.FindControl("photoImage").Visible = true; 
       e.Item.FindControl("lblName").Visible = true; 
       e.Item.FindControl("deleter").Visible = true; 
       e.Item.FindControl("lblEmail").Visible = true; 
      } 
     } 

    } 
    </script> 

<asp:Repeater ID="friendRepeater" runat="server" OnItemDataBound = "ItemDataBound"> 
    <HeaderTemplate> 
    <table cellpadding="0" cellspacing="0"> 
    </HeaderTemplate> 


    <ItemTemplate> 
    <tr> 
     <td> 
     <div style=" padding-left:180px;"> 
      <div id="leftHandPost" style="float:left; width:120px; height:120px; border: medium solid #cdaf95; padding-top:5px;"> 
       <div id="childLeft" style=" padding-left:5px;"> 
       <div id="photo" style=" border: thin solid black; width:100px;height:100px;"> 
        <asp:Image id="photoImage" runat="server" ImageUrl='<%# String.Concat("Images/", Eval("Picture")) %>' Width="100px" Height="100px" /> 
       </div> 
       </div><!--childLeft--> 
      </div><!--leftHandPost--> 
      </div> 
     </td> 

         <td> 
          <div id="rightHandPost" style=" float:right; padding-right:260px;"> 
           <div id="childRight" style="width:400px; height:120px; border: medium solid #cdaf95; padding-top:5px; padding-left:10px;"> 
            <strong><asp:Label id="lblName" runat="server"><%# Eval("PersonName") %></asp:Label></strong><br /> 
            <div style=" float:right; padding-right:10px;"> 

            <asp:ImageButton id="deleter" ImageUrl="App_Themes/Health/images/blue/Add.png" CausesValidation="False" runat="server" 
         AlternateText="Add" ImageAlign="absmiddle" BorderStyle="None" Width="21" Height="21" 
         OnCommand="add" OnClientClick="return confirm('Are you sure you want to add this?');" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "PersonID") %>' /> 
         </div><br /> 
            <asp:Label id="lblID" runat="server"><%# Eval("PersonID") %></asp:Label><br /> 
            <asp:Label id="lblEmail" runat="server"><%# Eval("Email") %></asp:Label> 
           </div><!--childRight--> 
          </div><!--rightHandPost--> 
         </td> 
        </tr> 

    </ItemTemplate> 

    <AlternatingItemTemplate> 
    <tr> 
     <td> 
     <div style=" padding-left:180px;"> 
      <div id="Div1" style="float:left; width:120px; height:120px; border: medium solid #cdaf95; padding-top:5px;"> 
       <div id="Div2" style="padding-left:5px;"> 
       <div id="Div3" style=" border: thin solid black; width:100px;height:100px;"> 
        <asp:Image id="photoImage" runat="server" ImageUrl='<%# String.Concat("Images/", Eval("Picture")) %>' Width="100px" Height="100px" /> 
       </div> 
       </div><!--childLeft--> 
      </div><!--leftHandPost--> 
     </div> 
     </td> 

         <td> 
          <div id="Div4" style=" float:right; padding-right:260px;"> 
           <div id="Div5" style="width:400px; height:120px; border: medium solid #cdaf95; padding-top:5px; padding-left:10px;"> 
            <strong><asp:Label id="lblName" runat="server"><%# Eval("PersonName")%></asp:Label></strong> 
            <div style=" float:right; padding-right:10px;"> 
            <asp:ImageButton id="deleter" ImageUrl="App_Themes/Health/images/blue/Add.png" CausesValidation="False" runat="server" 
         AlternateText="Add" ImageAlign="absmiddle" BorderStyle="None" Width="21" Height="21" 
         OnCommand="add" OnClientClick="return confirm('Are you sure you want to add this?');" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "PersonID") %>' /> 
         </div><br /> 
            <br /> 
            <asp:Label id="lblID" runat="server"><%# Eval("PersonID") %></asp:Label><br /> 
            <asp:Label id="lblEmail" runat="server"><%# Eval("Email") %></asp:Label> 
           </div><!--childRight--> 
          </div><!--rightHandPost--> 
         </td> 
        </tr> 
    </AlternatingItemTemplate> 

    <FooterTemplate> 
    </table> 
    </FooterTemplate> 

</asp:Repeater> 

對於pageLoad的:

​​
+0

+1 MessageBox中的錯誤處理和內聯CSS – CodeZombie

+0

Erhmm ~~所以,對不起,我不明白..能進一步解釋? – user2769165

回答

1

嘗試使整個項目不可見的,而不是一塊一塊的,就像這樣:

if (s == personID) 
{ 
    e.Item.Visible = false; 
} 

另外,您可以在綁定到中繼器控件之前通過條件過濾結果。

+0

我試圖讓整個東西看不見,但沒有功能。這意味着整個ItemDataBound是不是功能?正如你在綁定中繼器控制過濾器之前所說的那樣,結果可以簡要地解釋它嗎?真的很感謝你:D – user2769165

+0

我假設你正在做某種搜索功能?無論哪種控件(即按鈕)調用搜索,然後在該點擊處理程序中更改SQL SELECT以在文本框中鍵入值包括where子句,而不是在綁定時嘗試匹配文本框的值。 –

+0

嗨,我解決了我的問題。順便說一句,我只會隱藏第一行數據庫內容。如果我想隱藏不止一個,我該怎麼辦?謝謝 – user2769165

0

我認爲你應該過濾你的SQL查詢中的數據,只將需要的數據綁定到中繼器。

我認爲你要填寫PersonIDB日期值要隱藏已添加到contactFriend表中的行,不是嗎?

如果是這樣,你應該改變SQL查詢字符串是如

SELECT p.PersonID, p.PersonName, p.Email, p.Picture 
FROM Person AS p JOIN contactFriend AS cf ON p.PersonID = cf.PersonID 
WHERE cf.PersonIDB IS NOT NULL AND cf.[date] IS NOT NULL 

這是其他過濾查詢,如果你從來沒有在contactFriend表PERSONID數據,直到你加入PersonIDB值使用(我意味着你插入是PersonID togather與PersonIDB當時創造新行)

SELECT PersonID, PersonName, Email, Picture 
FROM Person 
WHERE PersonID NOT IN (SELECT PersonID FROM contactFriend) 
+0

這意味着我不需要再使用ItemDataBound了嗎? – user2769165

+0

不,你仍然需要通過數據轉發器,但你可以刪除檢查點了,我的意思是這個範圍** if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType。 AlternatingItem){...} ** – Alice

+0

BUt我已經將它綁定到頁面載入中,它工作。 SELECT PersonID,PersonName,Email,Picture FROM Person Where PersonID Not in(SELECT PersonIDB FROM contactFriend WHERE PersonID = @id) 我將此更改爲我的頁面加載並且是工作。 – user2769165