Can access SelectedItem in Web Control after FindControl

Discussion in 'ASP .Net' started by JB, Mar 6, 2009.

  1. JB

    JB Guest

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

    }
    }

    --
    JB
     
    JB, Mar 6, 2009
    #1
    1. Advertising

  2. JB

    bruce barker Guest

    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>
    >
    > <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");
    >
    > }
    > }
    >
     
    bruce barker, Mar 8, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin Bartels
    Replies:
    2
    Views:
    994
    Jacob Yang [MSFT]
    Oct 21, 2003
  2. Dave
    Replies:
    2
    Views:
    1,664
  3. Amil
    Replies:
    1
    Views:
    437
  4. awalker1
    Replies:
    4
    Views:
    499
    awalker1
    Apr 22, 2005
  5. Matt C.
    Replies:
    0
    Views:
    1,108
    Matt C.
    Jun 23, 2006
Loading...

Share This Page