Repeater - Always Binding Internal Controls

Last post 07-16-2009 5:33 AM by superguppie. 5 replies.

Sort Posts:

  • Repeater - Always Binding Internal Controls

    07-14-2009, 10:58 PM
    • Member
      409 point Member
    • unicorn21
    • Member since 03-29-2004, 6:41 PM
    • Brisbane
    • Posts 86

    Hi there.

    I have a question I have wanted to ask for a while now...

    I have a Repeater with 2 DropDownLists. 1 is for a List of Expressions. 1 is for a List of Rules.


    They are both bound to 2 different ObjectDataSource's. Basically as the Repeater iterates I want to display 1 or the other.

    The problem is both are always bound, even when I make them visible or not.

    Is there a good way to do this or is the Repeater the wrong Control to be using?


    Here is the code I am using:


    <table border="0" cellpadding="3">
       <asp:Repeater ID="rptExpressions" runat="server" DataSourceID="AddExpressionList" EnableViewState="true" >
          <ItemTemplate>
          <asp:Panel ID="panExpression" runat="server" Visible='<%# Eval("ExpressionType").ToString() == "Expression" %>'>
             <tr>
                <td>
                   <asp:LinkButton ID="butRemove" runat="server" Text="Remove" OnClick="RemoveExpression_Click" CommandArgument='<%# Container.ItemIndex %>' />
                </td>
                <td>
                   <asp:DropDownList ID="drpVariableList" runat="server" AutoPostBack="true"
                      DataSourceID="VariableList" DataTextField="Name" 
                      OnSelectedIndexChanged="VariableChanged_Selected" OnDataBound="Variable_Created" 
                      Text='<%# Eval("SelectedVariable") %>' Width="200" />
                </td>
                <td>
                   <asp:DropDownList ID="drpOperatorList" runat="server" Width="140" />
                </td>
                <td>
                   <asp:TextBox ID="txtValue" runat="server" Text='<%# Eval("SelectedValue") %>' />
                </td>
             </tr>
          </asp:Panel>
    
          <asp:Panel ID="panRules" runat="server" Visible='<%# Eval("ExpressionType").ToString() == "Rule" %>'>
             <tr>
                <td>
                   <asp:LinkButton ID="LinkButton1" runat="server" Text="Remove" OnClick="RemoveExpression_Click" CommandArgument='<%# Container.ItemIndex %>' />
                </td>
                <td>
                   <asp:DropDownList ID="drpRuleList" runat="server" OnDataBound="Rule_Created"
                      DataSourceID="RuleList" DataTextField="Name" 
                      Width="200" />
                </td>
                <td>
                   <asp:DropDownList ID="drpRuleValue" runat="server" Width="140">
                      <asp:ListItem Text="True" />
                      <asp:ListItem Text="False" />
                   </asp:DropDownList>
                </td>
                <td>
                </td>
             </tr>
          </asp:Panel>      
                                           
          </ItemTemplate>
       </asp:Repeater>
    </table>




  • Re: Repeater - Always Binding Internal Controls

    07-15-2009, 2:32 AM
    • Participant
      798 point Participant
    • kpyap
    • Member since 07-10-2009, 9:01 AM
    • Posts 205

    Hi,

    What I am understand from your post is that controls in Repeater are always doing databinding

    I think this behavior is apply to all Data Controls

    If you want to minize the databinding, you may do manual databinding
    E.g.

        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim ddl As DropDownList = e.Row.FindControl("DropDownList1")
    
                If e.Row.DataItem("FirstName").ToString.StartsWith("I") Then
                    If Not ddl Is Nothing Then
                        With ddl
                            .DataTextField = "DepartmentGroupName"
                            .DataValueField = "DepartmentGroupKey"
                            .DataSourceID = "departmentDS"
                        End With
                    End If
                Else
                    ddl.Visible = False
                End If
            End If
        End Sub


    And removed properties on HTML

    <asp:DropDownList ID="DropDownList1" runat="server"/>


    Hope it help

  • Re: Repeater - Always Binding Internal Controls

    07-15-2009, 2:46 AM
    • All-Star
      24,396 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,467

    IF the data were always either one or the other of "Expression" OR "Rule" then you could have thought of using dynamic templates (example: http://www.devx.com/vb2themax/Article/19908) ....but otherwise what you're doing is good enough.

    to reduce the overhead of binding you can choose to bind manually..in your case it will be the ItemDataBound event ...and don't forget to remove the DataSourceID assigned by default in the markup

    Regards,
    Peter
  • Re: Repeater - Always Binding Internal Controls

    07-15-2009, 10:44 AM
    • Contributor
      7,023 point Contributor
    • superguppie
    • Member since 05-19-2009, 7:42 AM
    • Posts 1,240
    Odd that Visible seems to bind after the contents of the Panel are bound.
    You might want to try handing the DataBinding event of the Panels. The NamingContainer of the sender object will be the Item that is being bound. That will have a DataItem that you can use to set Visible.
    However, I am not sure this will work, as it may be too late by then to stop the DataBinding process.
    Superguppie.

    Please remember to click “Mark as Answer” on the post that helps you.
    This can be beneficial to other community members reading the thread.

    When all you've got is a Hammer,
    Every Problem looks like a Nail.
    Michael Swain.
  • Re: Repeater - Always Binding Internal Controls

    07-15-2009, 7:43 PM
    • Member
      409 point Member
    • unicorn21
    • Member since 03-29-2004, 6:41 PM
    • Brisbane
    • Posts 86

    http://www.devx.com/vb2themax/Article/19908)

    Interesting idea but my data is a mixture of the Expressions and Rules so that wouldn't work.


    I guess though this has answered by question.

    Everything thats bindable in an ItemTemplate is always bound, regardless of if it is visible.


    Basically now I only bind to the 'value' control if it is an Expression or Rule by checking in the code behind after the DataBound event.

    <asp:DropDownList ID="drpExpressionList" runat="server" 
       DataSourceID="VariableList" DataTextField="Name" OnDataBound="Expression_Created"
    
    <asp:DropDownList ID="drpRuleList" runat="server" 
       DataSourceID="RuleList" DataTextField="Name" OnDataBound="Rule_Created"

     protected void Rule_Created(object sender, EventArgs e)
        {
        RuleManagement.State state = State.Applications<State.RuleManagement.State>.State;
        AddExpressionItem item = state.AddExpressionList[rptExpressions.Controls.Count - 1];
    
        if (item.ExpressionTypeEnum == ExpressionType.Rule)
    // Bind Value Control etc...




  • Re: Repeater - Always Binding Internal Controls

    07-16-2009, 5:33 AM
    Answer
    • Contributor
      7,023 point Contributor
    • superguppie
    • Member since 05-19-2009, 7:42 AM
    • Posts 1,240
    I have to disagree that everything is always DataBound. I have seen GridViews not DataBinding when in Panels that have Visible=false. However, that was for Panels that didn't DataBind Visible. I guess it is a matter of timing. If you set Visible=false in time, what is in it will not be DataBound.
    Superguppie.

    Please remember to click “Mark as Answer” on the post that helps you.
    This can be beneficial to other community members reading the thread.

    When all you've got is a Hammer,
    Every Problem looks like a Nail.
    Michael Swain.
Page 1 of 1 (6 items)