G
gishani ratnayake via .NET 247
I am trying to get the selected value of a drop down list which is a user control. Can someone please tell me where I am going wrong in this code??
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.
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.