Posts Tagged ‘ASP.NET’

ASP.NET 3.5 ListView and Databinding a DropdownList inside InsertTemplate

What I was finding for binding a dropdown list in an insertemplate for a 3.5 Listview has been either erroneous or clunky. First the issue:

When triggering an Insert Template to appear, there is no event that occurs as the form is rendered, like the the Update form. So any dropdown list, pretty much needs to be filled before fact. Or not…..

I created a simple web control inherited from the dropdown, and have it load from a datasource on it’s load. With a couple of checks, it loads once, and doesn’t repeat the databind. While the code I  put here probably won’t work exactly as is outside of my app’s context (custom object collections are retrieved a bound) You’ll get the idea. Once you create the control, simply put it in the template. No additional work needed!


Imports System
Imports System.Collections.Generic
ImportsSystem.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
ImportsSystem.Web.UI.WebControls  <DefaultProperty("Text"), ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")> _
Public ClassTypeDropDownList
InheritsDropDownList<Bindable(True), Category("Data"), DefaultValue(""), Localizable(True)> PropertyTypeName() As String
   Get
      Dim s As String = CStr(ViewState("TypeName"))
      If s Is Nothing Then
        Return "[" + Me.ID + "]"
      Else
        Return s
      End If
  End Get
  Set(ByVal Value As String)ViewState("TypeName") = Value
  End Set
End Property
<Bindable(True), Category("Data"), DefaultValue("False"), Localizable(True)> PropertyIsLoaded() As Boolean
Get
   IfViewState("IsLoaded") IsNot Nothing Then
     Return CBool(ViewState("IsLoaded"))
   Else
     Return False
   End If
End Get
Set(ByVal Value As Boolean)ViewState("IsLoaded") = Value
End Set
End Property

Private Sub ServerControl1_Load(ByVal sender As Object, ByVal e AsSystem.EventArgs) Handles Me.Load
	DimopRes AsdbhJRBusiness.OperationResult
	If Not Me.IsLoaded Then
		DimlItems As List(OfdbhJRBusiness.DataContracts.TypeContract)
		Dimotyp As NewdbhJRBusiness.Entities.TypeEntity(Me.TypeName) opRes = otyp.GetListItems
 		If opRes.Success = True ThenlItems =CType(opRes.data, List(OfdbhJRBusiness.DataContracts.TypeContract))
			Me.DataSource = lItems
			Me.DataBind()
			Me.IsLoaded = True

		End If
	otyp =Nothing


	End If
End Sub
End  Class
And here is what I've put in the aspx. Note the simple way to find to the value.  

 

<isd:TypeDropDownList runat="server" DataTextField="LongDesc"  

SelectedValue='<%# DataBinder.Eval(Container.DataItem, "AddressTypeGUID") %>' 

DataValueField="Id" ID="ddlAddresstype" TypeName="AddressType" /> 

 Hope this helps! I'll respond to any questions, but please be patient!
November 2018
M T W T F S S
« Feb    
 1234
567891011
12131415161718
19202122232425
2627282930