Posts
1
Comments
0
Trackbacks
0
| Home |
Drop Down Box in ListView InsertTemplate

Today started with a simple plan, create a quick entry form for the sales guys to start entering some client information, copy another page of similar format, change the SQL and bam, done.

Being the somewhat devious devils they are, Microsoft developers decided that was not a good plan for my first hour, instead it was an excellent plan for most the day.

The trouble starts when using a ListView with an InsertItemTemplate.  My old page had all Textboxes with Bind tying the data to the Datasource.  I planned on using a DropDownlist for one of the fields on this one. 

After much research and trial and error, i found that the best way (a.k.a. the first way I found) was to use an OnInt on the Dropdown list to add the parameter to the SQL.  Heres the snippets that made it work.

 

Here's the markup, you can see I added the OnInit to the ClientID drop down list.
asp:dropdownlist oninit="ddlClientID_Init" id="ClientID" runat="server" datatextfield="UserName" datavaluefield="ClientID" datasourceid="sqlClientID" /

And for the code behind:

    Sub ddlClientID_Init(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim list As DropDownList = DirectCast(sender, DropDownList)


        Dim parameter As ControlParameter = New ControlParameter
        parameter.Name = "ClientID"
        parameter.ControlID = list.UniqueID
        parameter.PropertyName = "SelectedValue"
        SqlDataSource1.InsertParameters.Add(parameter)
    End Sub

One message board suggested viewing the source and getting the full element name, which worked....once. Everytime you add another record to the ListView it increments ctl1$ to ctl2$ so that method would not work. In the method above, we capture the UniqueID directly from the Control. Since the SQLDataSource is outside of the ListView and free from all it's access annoyances, we are free to create and add the control.

posted on Thursday, February 05, 2009 9:02 AM Print
Comments have been closed on this topic.
News