ASP.NET Listview with a DropDown List having own datasource in Insert Item Template

Here is the procees for ASP.NET Listview with a DropDown List having own datasource in Insert Item Template.

I have a ListView1 with SqlDataSource1
I wanna place DropDownList in InsertItem Template with SqlDataSource2


               <td>
                        
                        <asp:DropDownList ID="ddlProject" runat="server" datatextfield="v" DataValueField="v"  DataSourceID="SqlDataSource2">
                     
                         </asp:DropDownList>
                    </td>
                    <td>
                        <asp:TextBox ID="unitTextBox" runat="server" Text='<%# Bind("unit") %>' />
                    </td>

You can have other controls databind to list view if required accept DropDown

Now you need to assign the dropdownvalue to insert parameter . It can be done at ListView1_ItemInserting Event


Protected Sub ListView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles ListView1.ItemInserting
        Dim ddl As DropDownList = TryCast(e.Item.FindControl("ddlProject"), DropDownList)
        If ddl IsNot Nothing Then
            e.Values("project") = ddl.SelectedValue
        End If
    End Sub


May be this event dosent trigger so call it from listview tag as OnItemInserting event:


<asp:ListView ID="ListView1" runat="server" DataKeyNames="uid" 
            DataSourceID="SqlDataSource1" InsertItemPosition="FirstItem" OnItemInserting="ListView1_ItemInserting">


###########
In Same Manner we can hav an uneditable label or textbox whose value cant be edited by user...
declare ur label in insertitem template without binding to datasource parameter


<td>
                        
                        <asp:Label ID="lblProject" runat="server" Text=""></asp:Label>
                    </td>

Now use listview databound event



Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
        Dim lbl As Label = TryCast(ListView1.InsertItem.FindControl("lblProject"), Label)
        If lbl IsNot Nothing Then
            lbl.Text = ddlProject.SelectedValue
        End If
    End Sub

envoke this event from listview


<asp:ListView ID="ListView1" runat="server" DataKeyNames="uid" 
            DataSourceID="SqlDataSource1" InsertItemPosition="FirstItem"  OnItemDataBound = "ListView1_ItemDataBound">

Update the insert parameter value programmatic-ally at page load or whenever required:

SqlDataSource1.InsertParameters("project").DefaultValue = ddlProject.SelectedValue




- Vinod Kotiya
www.vinodkotiya.com


Comments

Most Viewed Post

solution for facebook static.ak.fbcdn.net waiting

Connect Sony XPLOD MEX-BT2500 Bluetooth CD Receiver with ur mobile

Crystal Report : Show data horizontally (Left to right) i.e. columns as rows

Google+ Followers