Sending dropdownlist items?

Discussion in 'ASP .Net Web Controls' started by agb, Aug 27, 2003.

  1. agb

    agb Guest

    How can I send dropdownlist items to an ascx file AND
    how can I get the dropdownlist items from outside of the ascx file :)

    thanksss...
    agb, Aug 27, 2003
    #1
    1. Advertising

  2. The trick is to look inside your page to get an instance of the user control.
    Then, look inside the user control to get the object (dropdownlist) that you
    want to change.

    In the code below, I create an instance of the usercontrol that I find in the
    page. I create an instance of the dropdownlist that I find in the usercontrol.
    After that, you can add the items. The full source code for both the .ascx and
    ..aspx files are below.

    Dim usrcntrl As UserControl
    Dim usrDDL As DropDownList
    usrcntrl = Page.FindControl("Usrddl1")
    usrDDL = usrcntrl.FindControl("dropdownlist1")
    usrDDL.Items.Add("Red")
    usrDDL.Items.Add("Blue")
    usrDDL.Items.Add("Green")

    Does this help?

    Ken
    MVP [ASP.NET]




    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="useuserddl.aspx.vb"
    Inherits="p733workev.useuserddl"%>
    <%@ Register TagPrefix="uc1" TagName="usrddl" Src="usrddl.ascx" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>useuserddl</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </head>
    <body ms_positioning="FlowLayout">
    <form id="Form1" method="post" runat="server">
    <p>
    <uc1:usrddl id="Usrddl1" runat="server"></uc1:usrddl></p>
    <p>
    <asp:button id="Button1" runat="server" text="Fill
    DDL"></asp:button></p>
    </form>
    </body>
    </html>



    Public Class useuserddl
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim usrcntrl As UserControl
    Dim usrDDL As DropDownList
    usrcntrl = Page.FindControl("Usrddl1")
    usrDDL = usrcntrl.FindControl("dropdownlist1")
    usrDDL.Items.Add("Red")
    usrDDL.Items.Add("Blue")
    usrDDL.Items.Add("Green")
    End Sub
    End Class




    <%@ Control Language="vb" AutoEventWireup="false" Codebehind="usrddl.ascx.vb"
    Inherits="p733workev.usrddl"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:dropdownlist id="DropDownList1" runat="server">
    <asp:listitem value="White">White</asp:listitem>
    <asp:listitem value="Black">Black</asp:listitem>
    </asp:dropdownlist>



    Public Class usrddl
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    End Class


    --
    Microsoft MVPs have a question for *you*: Are you patched against the Worm?
    http://www.microsoft.com/security/security_bulletins/ms03-026.asp



    "agb" <> wrote in message
    news:...
    How can I send dropdownlist items to an ascx file AND
    how can I get the dropdownlist items from outside of the ascx file :)

    thanksss...
    Ken Cox [Microsoft MVP], Aug 28, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gilles Kuhn
    Replies:
    0
    Views:
    426
    Gilles Kuhn
    Sep 15, 2003
  2. Bart Van Hemelen
    Replies:
    2
    Views:
    4,655
    Bart Van Hemelen
    Sep 4, 2006
  3. Larry Bud
    Replies:
    13
    Views:
    7,648
    Larry Bud
    Jan 7, 2008
  4. Ben
    Replies:
    2
    Views:
    867
  5. Anjan Bhowmik
    Replies:
    1
    Views:
    462
    Misbah Arefin
    Feb 14, 2008
Loading...

Share This Page