We help IT Professionals succeed at work.

Vb.net: Datalist - Change objectdatasource select method depending on control

As shown in the code below, I have a datalist which gets its data from the objectDatasource - ObjectDataSource.

I also have tow link buttons and one repeater which is populated with Alpahets having the following click events it he code behind.

At the moment the datalist displayes data is linked to selectmethod SelectMethod="BindData".  I want to now change the behaviour of the datalist and datasource so that depending on the selection(click) of the linkbuttons or the repeater alpahets, i want to load the datalist with different data.

I have 3 different stored procs which get the required data. My problem is how do i link the click event of the link buttons or the alphbet repeater so that the relevant data is shown by the datalist.
<table cellpadding="0" cellspacing="0" border="0" align="center">
                                                <tr>
                                                    <td align="center">
                                                        <asp:LinkButton ID="lnkLastestEntries" runat="server" Text="50 Latest Entries" OnClick="LastestEntries_Click"
                                                            Enabled="true" />
                                                        |
                                                        <asp:LinkButton ID="lnkMostViewed" runat="server" Text="50 Most Viewed" OnClick="MostViewed_Click"
                                                            Enabled="true" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="center">
                                                        <asp:Repeater ID="rptAlphabets" runat="server">
                                                            <ItemTemplate>
                                                                <asp:LinkButton ID="lnkAlphabet" runat="server" Text='<%#Eval("Value")%>' OnClick="Alphabet_Click"
                                                                    Enabled='<%# Eval("isNotSelected")%>' />
                                                            </ItemTemplate>
                                                        </asp:Repeater>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <div style="position: relative; height: 545px; width: 420px; overflow: auto;">
                                                            <asp:DataList ID="dlContacts" runat="server" RepeatLayout="Table" RepeatColumns="1" dataSourceID="SDataSource" 

                                                                CellPadding="2" CellSpacing="2">
                                                                <ItemTemplate>
                                                                    <table cellpadding="2" cellspacing="0" border="0" style="width: 390px; border: solid 1px #D2E4FC;">
                                                                        <tr>
                                                                            <td rowspan="4" width="25%">
                                                                                <a href="?id=<%#Eval("pid")%>">
                                                                                    
                                                                        <tr>
                                                                            <td>
                                                                                
                                                                                    <%#Eval("eline")%></a></b>
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <td>
                                                                                Viewer:
                                                                                <%#Eval("likerdata")%>
                                                                                                   </td>
                                                                        </tr>
                                                                    </table>
                                                                </ItemTemplate>
                                                            </asp:DataList>
                                                               <div id="paging" align="center">
                    <asp:Label ID="lblPagerLocation" runat="server" />  
                    <asp:PlaceHolder ID="ViewAll" runat="server"></asp:PlaceHolder>
                    <a id="PrevPage" runat="server">
                        <asp:Image runat="Server" id="PrevImage"  src="http://myserver.com/static/images/previous.gif"
                              AlternateText="Previous page" /></a>
                    <asp:PlaceHolder ID="PageLinks" runat="server"></asp:PlaceHolder>
                    <a id="NextPage" runat="server">
                       <tz:Img runat="Server" id="NextImage" src="http://myserver.com/static/images/next.gif"
                           AlternateText="Next page" skinid="nextpage"/></a>
                </div>            
        
    </div>    
    <asp:ObjectDataSource ID="SDataSource"  TypeName="entries.Index" 
          Runat="server"      SelectMethod="BindData">
        <SelectParameters>
               <asp:QueryStringParameter Name="PageIndex" 
                 QueryStringField="PageIndex" DefaultValue="0" />
            <asp:QueryStringParameter Name="NumRows" 
                 QueryStringField="NumRows" DefaultValue="3" />
            <asp:Parameter Name="Count" 
                 Direction="Output" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>

Open in new window

Comment
Watch Question

Commented:
Hi,

Here is one way to achieve it...

1. Add a new hidden-field which we will use to store selected alphabet

2. Add a new parameter  to your SelectMethod (i.e. BindData) say SelectedAlphabet and modify your select query in that method accordingly.

3. add new SelectParameter of type ControlParameter to your ObjectDataSource 'SDataSource' for new parameter SelectedAlphabet we added in step#2, parameter will get its value from hidden field control we created in step#1.

4. In click event of lnkAlphabet link button of repeater control do following...
        a. set selected alphabet in hidden-field
        b. Re-bind DataList control

Author

Commented:
Hi,

Thanks your reply.

I am not able to do the 4th step because the datalist is already bound to "SDataSource" as below.

<asp:DataList ID="dlContacts" runat="server" RepeatLayout="Table" RepeatColumns="1" dataSourceID="SDataSource"

How do i remove the binding and rebind?

Thanks

Commented:
Hi,


Call DataBind() method of DataList control in click event.

Author

Commented:
Thanks