This is a migrated thread and some comments may be shown as answers.

Populating a DropDownList from SQL server in an ascx file based on the live demo Grid/Examples/UserControlEditForm

1 Answer 151 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 2
Mike asked on 05 Aug 2008, 12:01 PM

 Hi guys,

Firstly, great work on the components you have to offer, I am very impressed with the demos. I will be purchasing these if they can do the job I am trying to do.

I am using an ascx User Control within a RadGrid that I have been trying to get to work using an SQL datasource to populate a dropdownlist when in edit/update mode. The edit form dropdownlists need to be primarily set to the existing values in the sql datatable. The normal gridview displays the correct index data however the user control edit form does not. I have followed the Live Demo model for Grid/Examples/UserControlEditForm using DefaultVB.aspx and EmployeeDetailsVB.ascx and tried to adapt it for this scenario.

My code is as follows:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
  <AjaxSettings>
    <telerik:AjaxSetting AjaxControlID="CampaignGrid">
      <UpdatedControls>
        <telerik:AjaxUpdatedControl ControlID="CampaignGrid" />
      </UpdatedControls>
    </telerik:AjaxSetting>
  </AjaxSettings>
</telerik:RadAjaxManager>

<telerik:RadGrid etc...
  <MasterTableView etc...
    <Columns>
      <telerik:GridBoundColumn UniqueName="Category" HeaderText="campaignsCategoryID" DataField="campaignsCategoryID">
        <HeaderStyle Width="60px"></HeaderStyle>
      </telerik:GridBoundColumn>
      etc...
    </Columns>

    <EditFormSettings UserControlName="CampaignDetailsVB.ascx" EditFormType="WebUserControl">
      <EditColumn UniqueName="EditCommandColumn1">
      </EditColumn>
    </EditFormSettings>

  </MasterTableView>
</telerik:RadGrid>

The RadGrid is populated via it's code behind and works fine as per your example in...

The .ascx file contains multiple <asp:textbox>'s and 2 <asp:DropDownList>'s

The asp:textbox's work just fine in the ascx fil as I have them set up this way:

  <asp:textbox id="Name" runat="server" Text='<%# DataBinder.Eval( Container, "DataItem.Name"  ) %>' tabIndex="1">
  </asp:textbox>

The <asp:DropDownList>'s are set up this way:

  <asp:DropDownList id="Category" runat="server" tabindex="1">
  <asp:DropDownList id="DefaultGreeting" runat="server" tabindex="2">

In the VB code behind for this ascx file I have the following:

Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports Telerik.Web.UI

Partial Class Admin_CampaignDetailsVB
    Inherits System.Web.UI.UserControl

    Private _dataItem As Object = Nothing

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

#Region "Web Form Designer generated code"

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        '
        ' CODEGEN: This call is required by the ASP.NET Web Form Designer.
        '
        InitializeComponent()
        MyBase.OnInit(e)
    End Sub 'OnInit

    '/ <summary>
    '/  Required method for Designer support - do not modify
    '/  the contents of this method with the code editor.
    '/ </summary>
    Private Sub InitializeComponent()
        AddHandler DataBinding, AddressOf Me.CampaignDetails_DataBinding
    End Sub 'InitializeComponent

#End Region

    Public Property DataItem() As Object
        Get
            Return Me._dataItem
        End Get
        Set(ByVal value As Object)
            Me._dataItem = value
        End Set
    End Property

    Protected Sub CampaignDetails_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim sqlCategory As String = "select campaignsCategoryID, Category from tblCampaignsCategory"
        Category.DataSource = GetDr(sqlCategory)
        Category.DataBind()

        Dim CategoryValue As Object = DataBinder.Eval(DataItem, "campaignsCategoryID")
        If CategoryValue.Equals(DBNull.Value) Then
            CategoryValue = 0
        End If

        Category.SelectedIndex = CategoryValue
        Category.DataSource = Nothing

        Dim sqlGreeting As String = "select campaignsDefaultGreetingsID, DefaultGreeting from tblCampaignsDefaultGreetings"
        DefaultGreetings.DataSource = GetDr(sqlGreeting)
        DefaultGreetings.DataBind()

        Dim GreetingValue As Object = DataBinder.Eval(DataItem, "campaignsDefaultGreetingsID")
        If GreetingValue.Equals(DBNull.Value) Then
            GreetingValue = 0
        End If
        DefaultGreetings.SelectedIndex = GreetingValue
        DefaultGreetings.DataSource = Nothing

    End Sub

    Private Function GetDr(ByVal sqlText As String) As DataTable

        Dim conn As SqlConnection = New SqlConnection(ConnectionString())
        Dim adapter As SqlDataAdapter = New SqlDataAdapter
        adapter.SelectCommand = New SqlCommand(sqlText, conn)
        Dim table1 As New DataTable
        conn.Open()
        Try
            adapter.Fill(table1)
        Finally
            conn.Close()
        End Try
        Return table1

    End Function

    Private Function ConnectionString() As String
        Dim ConnString As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
        Return ConnString
    End Function

End Class

The Protected Sub CampaignDetails_DataBinding is the area that doesnt seem to want to work properly. Like I said, I used your model for Grid/Examples/UserControlEditForm: DefaultVB.aspx and EmployeeDetailsVB.ascx and tried to adapt it for this scenario.

The edit form loads properly but the asp:dropdownlists both display "System.Data.DataRowView" for each item in the datatable??? The grid subsequently does not update at all, it appears to spend the time updating but justs stays in edit mode.

I would greatly appreciate any help you can offer for this case as I have spent countless days searching your forums and others externally and there is nowhere that shows how to work with this type of situation.

Many thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 07 Aug 2008, 11:40 AM
Hi Grant,

Thank you for contacting us.

Can you please try setting DataTextField and DataValueField of both DropDownList controls?  Thus these controls will be bound correctly and will not show you "System.Data.DataRowViewā€¯ as DropDownList items.

Best regards,
Nikolay
the Telerik team


Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Mike
Top achievements
Rank 2
Answers by
Nikolay Rusev
Telerik team
Share this question
or