Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Can access SelectedItem in Web Control after FindControl

Reply
Thread Tools

Can access SelectedItem in Web Control after FindControl

 
 
JB
Guest
Posts: n/a
 
      03-06-2009
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("lstCal lType")));
//if (gcar != null)
// MessageBox.Show("it found lst " + gcar.ID);
//DropDownList b = new DropDownList();
//b = (DropDownList)CallInformation1.FindControl("lstCal lType");
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");

}
}

--
JB
 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      03-08-2009
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)

JB wrote:
> 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("lstCal lType")));
> //if (gcar != null)
> // MessageBox.Show("it found lst " + gcar.ID);
> //DropDownList b = new DropDownList();
> //b = (DropDownList)CallInformation1.FindControl("lstCal lType");
> 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");
>
> }
> }
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedItem after DataBinding--get DataRow? Matt C. ASP .Net 0 06-23-2006 04:00 PM
Access To A Component.SelectedItem in Page_Init awalker1 ASP .Net 4 04-22-2005 02:30 PM
RadioButtonList and SelectedItem when control is disabled Amil ASP .Net 1 02-11-2005 09:06 PM
Selecteditem.value for a listbox in a user control Dave ASP .Net 2 12-07-2004 08:42 PM
dropdown list selecteditem changes after page.unload event Justin Bartels ASP .Net 2 10-21-2003 02:35 AM



Advertisments