Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refresh ddl when inserting a new record

Status
Not open for further replies.

stinkybee

Programmer
May 15, 2001
218
GB
I have two listviews, ListView1 and ListView2. After insterting (and deleting / updating) a record in ListView1 I need to refresh a DropDownList in ListView2 with the new database values.


I have tried to do a DataBind() on the InsertedItem event but get the following error:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Here's the code for the databind if it helps

Code:
    Sub Update_ddl(ByVal sender As Object, ByVal e As ListViewInsertedEventArgs)
    	Dim ddl As new DropDownList
    	ddl = ListView2.InsertItem.FindControl("DropDownList1")
    	ddl.DataBind()
    end sub

Thanks in advance for any help




Web Development Manager
 
I need to see code. I don't use SqlDataSources or the ListView.
Most likely you need to use the OnItemInsered event if one exits.
 
Sorry, I meant the OnItemInserted event in my previous post. Anyway, here is more code minus the irrelevant template content

Code:
<asp:View ID="View1" runat="server">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:freemoneytrialsConnectionString %>" 
                DeleteCommand="DELETE FROM [merchants] WHERE [merchants_id] = @merchants_id" 
                InsertCommand="INSERT INTO [merchants] ([merchants_affiliate_id], [merchants_name], [merchants_logo], [merchants_link], [merchants_active], [merchants_description], [merchants_short_description]) VALUES (@merchants_affiliate_id, @merchants_name, @merchants_logo, @merchants_link, @merchants_active, @merchants_description, @merchants_short_description)" 
                SelectCommand="SELECT * FROM [merchants]" 
                UpdateCommand="UPDATE [merchants] SET [merchants_affiliate_id] = @merchants_affiliate_id, [merchants_name] = @merchants_name, [merchants_logo] = @merchants_logo, [merchants_link] = @merchants_link, [merchants_active] = @merchants_active, [merchants_description] = @merchants_description, [merchants_short_description] = @merchants_short_description WHERE [merchants_id] = @merchants_id"
                

OnInserted="Update_ddl"
                >
                <DeleteParameters>
                    <asp:Parameter Name="merchants_id" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="merchants_affiliate_id" Type="String" />
                    <asp:Parameter Name="merchants_name" Type="String" />
                    <asp:Parameter Name="merchants_logo" Type="String" />
                    <asp:Parameter Name="merchants_link" Type="String" />
                    <asp:Parameter Name="merchants_active" Type="Boolean" />
                    <asp:Parameter Name="merchants_description" Type="String" />
                    <asp:Parameter Name="merchants_short_description" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="merchants_affiliate_id" Type="String" />
                    <asp:Parameter Name="merchants_name" Type="String" />
                    <asp:Parameter Name="merchants_logo" Type="String" />
                    <asp:Parameter Name="merchants_link" Type="String" />
                    <asp:Parameter Name="merchants_active" Type="Boolean" />
                    <asp:Parameter Name="merchants_description" Type="String" />
                    <asp:Parameter Name="merchants_short_description" Type="String" />
                    <asp:Parameter Name="merchants_id" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>

            <asp:ListView ID="ListView1" runat="server" DataKeyNames="merchants_id" 
                DataSourceID="SqlDataSource1" 
                InsertItemPosition="LastItem"
                OnItemInserted="Update_ddl"
                >
                <!-- templates here -->
            </asp:ListView>
        </asp:View>
        
        
        <asp:View ID="View2" runat="server">      
            <asp:ListView ID="ListView2" runat="server" DataKeyNames="offers_id" 
                DataSourceID="SqlDataSource2" InsertItemPosition="LastItem"
                OnItemUpdating="test"
                OnItemUpdated="updateCategoryIds"
                OnItemInserted="insertCategoryIds">
                <InsertItemTemplate>
                    <tr style="">
                        <td>

			<!-- the DropDownList that needs to be refreshed -->

                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceId="SqlDataSource1" DataTextField="merchants_name" DataValueField="merchants_id" enableviewstate="true" SelectedValue='<%# Bind("merchants_id")%>'>
                                </asp:DropDownList>
                        </td>
                    </tr>
                </InsertItemTemplate>
               

            </asp:ListView>
</asp:view>


And the Update_ddl function

Code:
    Sub Update_ddl(ByVal sender As Object, ByVal e As ListViewInsertedEventArgs)
        Dim ddl As new DropDownList
    	ddl = ListView2.InsertItem.FindControl("DropDownList1")
    	ddl.DataSource = "SqlDataSource1"
    	ddl.DataBind()

    End Sub


Web Development Manager
 
I believe this is your problem:
SelectedValue='<%# Bind("merchants_id")%>


I am assuming you want to select the newly inserted value from ListView1? You will have to find a way to the get ID after the row is inserted. I have no idea how to do this with DataSource controls. I never use them because they become useless once you need to do something slightly complicated. What you are trying to do is not complicated, however the datasource controls make it so.
You are best off writing your own classes(DAL) to retrive, update, insert data. Using stored procedures, you can easily get the id(best off being an identity column) of the last inserted row. Then you can set that value in the ddl.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top