locked
Custom DropDownList - Can't get SelectedValue RRS feed

  • Question

  • User-1235926091 posted

    I am trying to build a simple Custom DDL and am having a problem coming up with the Selected Value.

    My CC looks like this

     

    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    Namespace MyCC
    
        Public Class MyCC
    
            Inherits DropDownList
            Private ddl1 As New DropDownList()
    
            Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
    
                ddl1.Items.Add(New ListItem("Item 1", "Value 1"))
                ddl1.Items.Add(New ListItem("Item 2", "Value 2"))
                ddl1.Items.Add(New ListItem("Item 3", "Value 3"))
                ddl1.Items.Add(New ListItem("Item 4", "Value 4"))
    
                ddl1.RenderControl(writer)
            End Sub
    
        End Class
    
    End Namespace

     Then in the site

    ASPX

     
            <cc1:MyCC ID="MyCC1" runat="server" /><br />
            <asp:Button ID="Button1" runat="server" Text="Button" /><br />
            <asp:Label ID="Label1" runat="server" />
      

    ASPX.VB

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Label1.Text = MyCC1.SelectedValue
        End Sub
     
    Monday, August 11, 2008 1:06 AM

All replies

  • User1906575742 posted

    Try this

    MyCC1.SelectedItem.Value

    Or

    MyCC1.SelectedItem.Text

    Monday, August 11, 2008 2:19 AM
  • User-1235926091 posted

    No Dice, but I have found the answer... looks like I was rendering my control wrong

    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    Namespace MyCC
        <ToolboxData("<{0}:MyCC ID=""MyCC1"" runat=""server""></{0}:MyCC>")> _
        Public Class MyCC
    
            Inherits DropDownList
    
    
            Protected Overloads Overrides Sub CreateChildControls()
                If Not Page.IsPostBack Then
                    Items.Add(New ListItem("Item 1", "Value 1"))
                    Items.Add(New ListItem("Item 2", "Value 2"))
                    Items.Add(New ListItem("Item 3", "Value 3"))
                    MyBase.CreateChildControls()
                End If
            End Sub
    
        End Class
    
    End Namespace
     
    Monday, August 11, 2008 2:32 AM
  • User-16411453 posted

     This is for a couple of your post I saw.  Mark ddl1 public in declarations to access it from the outside

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls


    <DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> _
    Public Class WebCustomControl1
    Inherits WebControl

    Private ddl1 As DropDownList
    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

    If Not Me.Context Is Nothing Then
    RenderContents(writer)
    Else
    RenderDesignMode(writer)
    End If

    End Sub
    Protected Overrides Sub
    OnInit(ByVal e As System.EventArgs)
    MyBase.OnInit(e)

    Controls.Clear()

    ddl1 = New DropDownList
    With ddl1
    .ID = [ID] & "_ddl1"
    .Width = 80%
    .Style.Add(HtmlTextWriterStyle.Width, "80%")
    .AutoPostBack = True
    AddHandler
    .SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged

    End With
    Controls.Add(ddl1)

    End Sub
    Private Sub
    RenderDesignMode(ByVal writer As HtmlTextWriter)

    Controls.Clear()

    ddl1 = New DropDownList
    With ddl1
    .ID = [ID] & "_ddl1"
    .Width = 80%
    .Style.Add(HtmlTextWriterStyle.Width, "80%")
    .AutoPostBack = True
    AddHandler
    .SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged

    End With
    Controls.Add(ddl1)

    RenderControl(writer)

    End Sub
    Protected Overrides Sub
    OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)

    If Not Page.IsPostBack Then
    Load_ddl1()
    End If

    End Sub
    Private Sub
    Load_ddl1()

    ddl1.Items.Clear()
    ddl1.Items.Add(New ListItem("Item 1", "Value 1"))
    ddl1.Items.Add(New ListItem("Item 2", "Value 2"))
    ddl1.Items.Add(New ListItem("Item 3", "Value 3"))
    ddl1.Items.Add(New ListItem("Item 4", "Value 4"))

    End Sub
    Protected Sub
    ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    		myValue = ddl1.SelectedValue

    End Sub

    End Class



      
    Tuesday, August 12, 2008 7:07 PM