DropDownList

E

Eugene Anthony

ModifyUserRegistration.aspx
---------------------------


<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="ModifyUserRegistration.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<br /><br />
<form id="form1" runat="server">
<asp:Repeater ID="rptItems" runat="server"
OnItemCommand="rptItems_ItemCommand">
<HeaderTemplate>
<table border="0" width="304">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="294" colspan="2" bgcolor="#FFCC99">
<p align="center"><b>Modify User Registration</b></p>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label1" runat="server"
Text="Email"></asp:Label></td>
<td style="width: 205px"><%# Eval("Email") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label2" runat="server"
Text="Password"></asp:Label></td>
<td style="width: 205px"><%# Eval("Password") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label3" runat="server"
Text="FirstName"></asp:Label></td>
<td style="width: 205px"><%# Eval("FirstName") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label4" runat="server"
Text="LastName"></asp:Label></td>
<td style="width: 205px"><%# Eval("LastName") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label5" runat="server"
Text="Country"></asp:Label></td>
<td style="width: 205px"><%# Eval("Country") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label6" runat="server"
Text="PostalCode"></asp:Label></td>
<td style="width: 205px"><%# Eval("PostalCode") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label7" runat="server"
Text="Gender"></asp:Label></td>
<td style="width: 205px"><%# Eval("Gender") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label8" runat="server"
Text="DOB"></asp:Label></td>
<td style="width: 205px"><%# Eval("DOB") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label9" runat="server"
Text="Status"></asp:Label></td>
<td style="width: 205px"><aspropDownList ID="DropDownList1"
runat="server"></aspropDownList></td>
</tr>
<tr>
<td style="width: 100px"></td>
<td style="width: 205px">
</td>
</tr>
<tr>
<td style="width: 100px"></td>
<td style="width: 205px">
<asp:Button ID="Button1" runat="server" Text="Modify"
OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 205px; text-align: right;">
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>

</html>






ModifyUserRegistration.aspx.cs
------------------------------


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String[] status = { "Enabled", "Disabled" };
DropDownList1.DataSource = status;
DropDownList1.DataBind();


if (!Page.IsPostBack)
LoadData();
}

private void LoadData()
{
String videoID = Request.QueryString["VideoID"];
SqlConnection cnn = new
SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].Con
nectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = cnn;
myCommand.CommandText = "SELECT * FROM Authentication WHERE
AuthenticationID=1";
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "Authentication");

rptItems.DataSource = ds;
rptItems.DataBind();
}
protected void rptItems_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
LoadData();
}

protected void Button1_Click(object sender, EventArgs e)
{

}
}



I attempted to specify the code bellow:


String[] status = { "Enabled", "Disabled" };
DropDownList1.DataSource = status;
DropDownList1.DataBind();


In my Page_Load function and I got an error saying:


Error 1 The name 'DropDownList1' does not exist in the current context
C:\Documents and Settings\solomon\My Documents\Visual Studio
2005\WebSites\WebSite4\ModifyUserRegistration.aspx.cs 17 9
C:\...\WebSite4\


How do I solve the problem?.


Your help is kindly appreciated.


Eugene Anthony
 
G

Guest

Hi Eugene,

Repeater is a list type control, with templated items. You are trying to
access drop down list, which is created as many times as number of rows in
the underlying data source. In other words, drop down list is instantiated
for every row in Repeater control, after data binding (if your result set
contains 3 records, 3 rows will be created, each one with combo box).
Therefore you cannot reference drop down list using DropDownList1 direclty.
Instead, use one of the method below:

1. (Programatic) Handle Repeater's ItemDataBound event:

protected void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList dropDownList =
(DropDownList)e.Item.FindControl("DropDownList1");

dropDownList.Items.Add("Item1");
dropDownList.Items.Add("Item2");
}

2. You can add items to a drop down list declaratively:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Item1"/>
<asp:ListItem Text="Item2"/>
</asp:DropDownList>

Hope this helps
--
Milosz


Eugene Anthony said:
ModifyUserRegistration.aspx
---------------------------


<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="ModifyUserRegistration.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<br /><br />
<form id="form1" runat="server">
<asp:Repeater ID="rptItems" runat="server"
OnItemCommand="rptItems_ItemCommand">
<HeaderTemplate>
<table border="0" width="304">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="294" colspan="2" bgcolor="#FFCC99">
<p align="center"><b>Modify User Registration</b></p>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label1" runat="server"
Text="Email"></asp:Label></td>
<td style="width: 205px"><%# Eval("Email") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label2" runat="server"
Text="Password"></asp:Label></td>
<td style="width: 205px"><%# Eval("Password") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label3" runat="server"
Text="FirstName"></asp:Label></td>
<td style="width: 205px"><%# Eval("FirstName") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label4" runat="server"
Text="LastName"></asp:Label></td>
<td style="width: 205px"><%# Eval("LastName") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label5" runat="server"
Text="Country"></asp:Label></td>
<td style="width: 205px"><%# Eval("Country") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label6" runat="server"
Text="PostalCode"></asp:Label></td>
<td style="width: 205px"><%# Eval("PostalCode") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label7" runat="server"
Text="Gender"></asp:Label></td>
<td style="width: 205px"><%# Eval("Gender") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label8" runat="server"
Text="DOB"></asp:Label></td>
<td style="width: 205px"><%# Eval("DOB") %></td>
</tr>
<tr>
<td style="width: 100px"><asp:Label ID="Label9" runat="server"
Text="Status"></asp:Label></td>
<td style="width: 205px"><aspropDownList ID="DropDownList1"
runat="server"></aspropDownList></td>
</tr>
<tr>
<td style="width: 100px"></td>
<td style="width: 205px">
</td>
</tr>
<tr>
<td style="width: 100px"></td>
<td style="width: 205px">
<asp:Button ID="Button1" runat="server" Text="Modify"
OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 205px; text-align: right;">
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>

</html>






ModifyUserRegistration.aspx.cs
------------------------------


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String[] status = { "Enabled", "Disabled" };
DropDownList1.DataSource = status;
DropDownList1.DataBind();


if (!Page.IsPostBack)
LoadData();
}

private void LoadData()
{
String videoID = Request.QueryString["VideoID"];
SqlConnection cnn = new
SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].Con
nectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = cnn;
myCommand.CommandText = "SELECT * FROM Authentication WHERE
AuthenticationID=1";
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "Authentication");

rptItems.DataSource = ds;
rptItems.DataBind();
}
protected void rptItems_ItemCommand(object source,
RepeaterCommandEventArgs e)
{
LoadData();
}

protected void Button1_Click(object sender, EventArgs e)
{

}
}



I attempted to specify the code bellow:


String[] status = { "Enabled", "Disabled" };
DropDownList1.DataSource = status;
DropDownList1.DataBind();


In my Page_Load function and I got an error saying:


Error 1 The name 'DropDownList1' does not exist in the current context
C:\Documents and Settings\solomon\My Documents\Visual Studio
2005\WebSites\WebSite4\ModifyUserRegistration.aspx.cs 17 9
C:\...\WebSite4\


How do I solve the problem?.


Your help is kindly appreciated.


Eugene Anthony
 

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

Top