user2769165
user2769165

Reputation: 69

Hide Repeater Element with condition

I want to hide the repeater element that already added in table but now I faced some problem which is even I done for the comparison condition the repeater will also show out the overall content based on the table. Here is my coding part. Hope will know the mistake that i made.

Designer code :

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

For the PageLoad :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                MySqlConnection connStr = new MySqlConnection();
                connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
                String searchOverall = "SELECT PersonID, PersonName, Email, Picture FROM Person";
                MySqlCommand cmdSearch = new MySqlCommand(searchOverall, connStr);
                connStr.Open();

                MySqlDataReader dtrRead2 = cmdSearch.ExecuteReader();
                friendRepeater.DataSource = dtrRead2;
                friendRepeater.DataBind();
                dtrRead2.Close();
                dtrRead2 = null;

                connStr.Close();

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }            
        }             
    }

Upvotes: 2

Views: 7177

Answers (2)

Alice
Alice

Reputation: 1265

I think you should filter data in your SQL query to bind only wanted data to the repeater.

I assume that you want to hide the rows which already added to contactFriend table by filling PersonIDB and date value, don't you?

If so, you should change the SQL query string to be such as

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

And this is other filtered query to use if you never have PersonID data in contactFriend table until you added the PersonIDB value (I mean you insert PersonID togather with PersonIDB to create new row at that time)

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

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34846

Try making the entire item invisible instead of piece by piece, like this:

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

Alternatively, you can filter the results by the condition before you bind to the repeater control.

Upvotes: 2

Related Questions