HELP!! event handling with web user controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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