J
JB
Hello
My C# ASP.NET application only fails when I try to access the Web
Controls objects properties of SelectedItem, SelectedIndex and
SelectedValue. As you can see from the code below;
-The Web Control contains a DropDownList populated from a dataset.
-The Web Control is place on the Default.aspx page.
-The FindControl finds the DropDownList control on the Web Control.
The problem is that I get an error stating:
"NullReferenceException was unhandled by user code" and
"Object reference not set to an instance of an object"
The web control is defined here:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="CallInformation.ascx.cs" Inherits="CallInformation" %>
<asp:Label ID="Label5" runat="server" Text="Call Information"
style="font-weight: 700"></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<aspropDownList ID="DropDownList1" runat="server" Height="16px"
Width="67px">
</aspropDownList>
-----------------------------------------------
Here is where the Web Control is populated:
protected void Page_Load(object sender, EventArgs e)
{
DataSet rtcar = new DataSet();
DataSet dscar = new DataSet();
cardata allcars = new cardata();
dscar = allcars.getcar(rtcar);
lstCallType.DataSource = dscar.Tables[0];
lstCallType.DataTextField =
dscar.Tables[0].Columns["CarID"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["Make"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["Color"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["PetName"].ColumnName.ToString();
lstCallType.DataBind();
lstCallType.SelectedIndex = 0;
carndx = Convert.ToString(lstCallType.SelectedIndex);
caritm = lstCallType.SelectedItem.Text;
carval = lstCallType.SelectedItem.Value.ToString();
}
}
-------------------------------------------------
Here is where I try to access the Web Control
SelectedItem, SelectedValue and SelectedIndex and fails:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebControl gcar =
((WebControl)(CallInformation1.FindControl("lstCallType")));
//if (gcar != null)
// MessageBox.Show("it found lst " + gcar.ID);
//DropDownList b = new DropDownList();
//b = (DropDownList)CallInformation1.FindControl("lstCallType");
DropDownList b = ((DropDownList)(gcar.FindControl("lstCallType")));
string caritm = b.SelectedItem.Text.ToString();
string cartext = b.SelectedValue.ToString();
if (b != null)
{
MessageBox.Show("this is the item " +
b.SelectedItem.Text.ToString());
MessageBox.Show("this is the value " + cartext.ToString());
}
else
MessageBox.Show("lstCallType not found");
}
}
My C# ASP.NET application only fails when I try to access the Web
Controls objects properties of SelectedItem, SelectedIndex and
SelectedValue. As you can see from the code below;
-The Web Control contains a DropDownList populated from a dataset.
-The Web Control is place on the Default.aspx page.
-The FindControl finds the DropDownList control on the Web Control.
The problem is that I get an error stating:
"NullReferenceException was unhandled by user code" and
"Object reference not set to an instance of an object"
The web control is defined here:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="CallInformation.ascx.cs" Inherits="CallInformation" %>
<asp:Label ID="Label5" runat="server" Text="Call Information"
style="font-weight: 700"></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<aspropDownList ID="DropDownList1" runat="server" Height="16px"
Width="67px">
</aspropDownList>
-----------------------------------------------
Here is where the Web Control is populated:
protected void Page_Load(object sender, EventArgs e)
{
DataSet rtcar = new DataSet();
DataSet dscar = new DataSet();
cardata allcars = new cardata();
dscar = allcars.getcar(rtcar);
lstCallType.DataSource = dscar.Tables[0];
lstCallType.DataTextField =
dscar.Tables[0].Columns["CarID"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["Make"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["Color"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["PetName"].ColumnName.ToString();
lstCallType.DataBind();
lstCallType.SelectedIndex = 0;
carndx = Convert.ToString(lstCallType.SelectedIndex);
caritm = lstCallType.SelectedItem.Text;
carval = lstCallType.SelectedItem.Value.ToString();
}
}
-------------------------------------------------
Here is where I try to access the Web Control
SelectedItem, SelectedValue and SelectedIndex and fails:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebControl gcar =
((WebControl)(CallInformation1.FindControl("lstCallType")));
//if (gcar != null)
// MessageBox.Show("it found lst " + gcar.ID);
//DropDownList b = new DropDownList();
//b = (DropDownList)CallInformation1.FindControl("lstCallType");
DropDownList b = ((DropDownList)(gcar.FindControl("lstCallType")));
string caritm = b.SelectedItem.Text.ToString();
string cartext = b.SelectedValue.ToString();
if (b != null)
{
MessageBox.Show("this is the item " +
b.SelectedItem.Text.ToString());
MessageBox.Show("this is the value " + cartext.ToString());
}
else
MessageBox.Show("lstCallType not found");
}
}