473,465 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HELP!! event handling with web user controls

I am getting an error when I try to get the selected text of the dropdownlist
which is a user control.

Here is the error.

The error message is:

Object reference not set to an instance of an object.
get
Line 20: {
Line 21: return ucFoodCategory.Items[ucFoodCategory.SelectedIndex].Text;
Line 22: }

Here's my code
foodCategory.ascx

<SELECT id="ctlFoodCat" style="WIDTH: 160px" name="Category">
<OPTION value="" selected>Any</OPTION>
<OPTION value="Appetizers">Appetizers</OPTION>
<OPTION value="Beverages">Beverages</OPTION>
<OPTION value="Breads">Breads</OPTION>
<OPTION value="Condiments & Dips">Condiments & Dips</OPTION>
</SELECT>
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class foodCategory : System.Web.UI.UserControl
{

protected System.Web.UI.HtmlControls.HtmlSelect ucFoodCategory;

public string SelectedText
{
get
{
return ucFoodCategory.Items [ucFoodCategory.SelectedIndex].Text;
}
}

}
..aspx code

<uc1:foodcategory id="ddlFoodCategory" runat="server"></uc1:foodcategory>

protected recipes.foodCategory ddlFoodCategory;
private void btnInsert_Click(object sender, System.EventArgs e)
{
lblfoodCategory.Text = ddlFoodCategory.SelectedText;
}

What am I missing ???

Thanks again.
Nov 16 '05 #1
1 1032
Your naming scheme is a little confusing or perhaps there are just
typographical errors.

In your HTML portion of your aspx file you call the SELECT element
ctlFoodCat and then in your codebehind you call it ucFoodCategory. Then you
name your user control ddlFoodCategory.

Besides not matching the prefix to the type of object, the real problem in
your code, as you present it, is that the id tag of the HTML SELECT element,
ctlFoodCat, does not match the id that you assign in your codebehind,
ucFoodCategory.

DalePres
MCAD, MCDBA, MCSE
"Buffy" <Bu***@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
I am getting an error when I try to get the selected text of the
dropdownlist
which is a user control.

Here is the error.

The error message is:

Object reference not set to an instance of an object.
get
Line 20: {
Line 21: return ucFoodCategory.Items[ucFoodCategory.SelectedIndex].Text;
Line 22: }

Here's my code
foodCategory.ascx

<SELECT id="ctlFoodCat" style="WIDTH: 160px" name="Category">
<OPTION value="" selected>Any</OPTION>
<OPTION value="Appetizers">Appetizers</OPTION>
<OPTION value="Beverages">Beverages</OPTION>
<OPTION value="Breads">Breads</OPTION>
<OPTION value="Condiments & Dips">Condiments & Dips</OPTION>
</SELECT>
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class foodCategory : System.Web.UI.UserControl
{

protected System.Web.UI.HtmlControls.HtmlSelect ucFoodCategory;

public string SelectedText
{
get
{
return ucFoodCategory.Items [ucFoodCategory.SelectedIndex].Text;
}
}

}
.aspx code

<uc1:foodcategory id="ddlFoodCategory" runat="server"></uc1:foodcategory>

protected recipes.foodCategory ddlFoodCategory;
private void btnInsert_Click(object sender, System.EventArgs e)
{
lblfoodCategory.Text = ddlFoodCategory.SelectedText;
}

What am I missing ???

Thanks again.

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: TheSpongeBob | last post by:
I have a .aspx that contains 2 .ascx. I need the first .ascx to fire off an event that the second .ascx handles. Is this possible ? and could anyone point me to an example. I can't seem to get the...
0
by: Wayne | last post by:
Hi! Is there a way to trigger events for the controls within the datagrid control? For instance, a check of a checkbox that would toggle all the checkboxes in the datagrid; or a dropdownlist...
0
by: manu_srinivasa | last post by:
:( Here is my design, we have user control placed in a aspx page. within user control, i am having datalist, within datalist i am placing radio button. On click of radio button, i need to...
5
by: serge calderara | last post by:
Dear all, I try to study Web user control .For that I have created a simple text box with button as a webuser control which has 2 properties. Then I drop my user control on a web page for...
8
by: Paul Bromley | last post by:
I am trying to get somewhere with user controls and have come up against an obstacle with using relative paths. I have a dataset in my user control, and have tried to enter the path with the...
2
by: WolfyUK | last post by:
Hello, I have a standard asp:DataGrid called CasesGrid that I wish to write my own paging controls for. The aim is to get something like the following rendered to screen: << First < Previous...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.