Sending dropdownlist items?

A

agb

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...
 
K

Ken Cox [Microsoft MVP]

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



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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top