Can access SelectedItem in Web Control after FindControl

  • Thread starter Thread starter JB
  • Start date Start date
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>

<asp:DropDownList ID="DropDownList1" runat="server" Height="16px"
Width="67px">
</asp:DropDownList>
-----------------------------------------------

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");

}
}
 
so many coding errors, so little time:

there is no reason to expect the usercontrol onload to fire before the
page onload.

building the list in onload will replace any postback values

MessageBox will throw an error as there is no window.

you never check if things exist before using them.

poor design, a page should not know about internal control in a control.
the user control should have properties to access internal objects.

-- bruce (sqlwork.com)
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>

<asp:DropDownList ID="DropDownList1" runat="server" Height="16px"
Width="67px">
</asp:DropDownList>
-----------------------------------------------

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");

}
}
 
Back
Top