SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If Statements and Repeaters

    Hi guys,

    Very new to asp.net still in classic asp mode but I was wondering if anyone can help me.

    I have a repeater pulling out data from a database.

    Code:
    		Dim txt_businesses_id
            txt_businesses_id = Request.QueryString("id")
    		
    		
    		'Create the connection and DataAdapter for the biz_categories table. 
            Dim cnn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("db/db_boston_dat.mdb"))
    
    
            ' Text Business Listings
            Dim cmd1 As New OleDbDataAdapter("select * from tbl_businesses WHERE biz_sub_categories_id = " & txt_businesses_id & "", cnn)
    
            'Create and fill the DataSet. 
            Dim ds As New DataSet()
            cmd1.Fill(ds, "tbl_businesses")
    
            'Bind the tbl_biz_categories table to the parent Repeater control, and call DataBind. 
            businesses.DataSource = ds.Tables("tbl_businesses")
    		
            Page.DataBind()


    Within the database I have a TRUE or FALSE field named ... business_webcheck

    basically IF this field equals TRUE i would like it to display the businesses email and website address details if FALSE it should hide them to the user.

    So I thought a classic way of doing this would be to use the .Visible property to its best and do something like this,


    Code:
    email.visible = True
    web.visible = True

    and within the actual repeater declare the web details ID like so,

    Code:
    <asp:Repeater id="businesses" runat="server">
    <ItemTemplate>
    <ul class="biz_item">
    <li><a href=""><%#DataBinder.Eval(Container.DataItem, "(businesses_name)")%></a></li>
    <li><%#DataBinder.Eval(Container.DataItem, "(businesses_building)")%> <%#DataBinder.Eval(Container.DataItem, "(businesses_address1)")%> <%#DataBinder.Eval(Container.DataItem, "(businesses_address2)")%> <%#DataBinder.Eval(Container.DataItem, "(businesses_town)")%> <%#DataBinder.Eval(Container.DataItem, "(businesses_county)")%> <%#DataBinder.Eval(Container.DataItem, "(businesses_postcode)")%></li>
    <li class="float_left">Telephone: <%#DataBinder.Eval(Container.DataItem, "(businesses_telephone)")%></li>
    <li>Fax: <%#DataBinder.Eval(Container.DataItem, "(businesses_fax)")%></li>
    
    
    <li id="email" runat="server" class="float_left">E-mail: <%#DataBinder.Eval(Container.DataItem, "(businesses_email)")%></li>
    
    <li id="web" runat="server">Website: <%#DataBinder.Eval(Container.DataItem, "(businesses_website)")%></li>
    
    
    
    </ul>
    </itemtemplate> 
    </asp:repeater>

    Notice that I have ensured the runat="server" bit within the list tag element.


    But even I have even attempted to get the IF statement within there I have a problem as the page returns a error saying the ID email has not been found on the page.

    I have to actually take it OUT of the repeater to be abel to get that list element to hide and show. Which defeats the purpose of the whole process.


    If anyone can shed any light on way my ID is not being picked up within my repeater can you let me know.

    I have even tried down the lines of the list element is a child of the parent object of the repeater but didnt seem to get me anywhere.


    Am I totally barking up the wrong tree?


    Thanks,


    Tim

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ASP.NET plays some clever tricks with ID in INamingContainer controls, which includes the repeater. It does so to avoid the ambigous IDs (illegal DOM) which would otherwise be generated by having multiple instantiations by the template.

    What ASP.NET does is prefixing the ID with the ID of the naming container and possibly suffixing with an index. But you really should not rely on how the IDs are generated. Instead you can use the FindControl member method of any naming containers.

    You can do this by installing an ItemDataBound handler. Through the sender you can find the naming container (cast it to a Control). Use the FindControl to find the control.

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,623
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Another option would be to catch this in the databinding phase and use the visible property to hide or show things as appropriate.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    Another option would be to catch this in the databinding phase and use the visible property to hide or show things as appropriate.


    could you give me an example on how to do this please.

    thanks,

    tim

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <li runat="server" class="float_left" Visible='<&#37;#Eval("business_webcheck")%>'>E-mail: <%#Eval("businesses_email")%></li>
    Explanation: The Visible property of server side elements controls whether the element (and children) renders or not.

  6. #6
    SitePoint Member xela220's Avatar
    Join Date
    Oct 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem that first jumps at me is the first line. Remember, that ASP.Net does NOT allow variants as classic asp does. You need to declare the type.

    Dim txt_businesses_id
    txt_businesses_id = Request.QueryString("id")


    Will give you an error. Try this instead.

    Dim txt_businesses_id As String
    txt_businesses_id = Request.Params("id")

    The other problem that you will have is your inline sql is open for a sql injection. I recommen using a stored procedure and using the sqlParams collection. At the very least, use a parameter for your inline SQL.

    Regards


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •