unable to cast type System.Web.UI.Control to System.Web.UI.WebControls.Label and object set to null reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

    unable to cast type System.Web.UI.Control to System.Web.UI.WebControls.Label and object set to null reference

    I have the following listView control on a page:

    <asp:ListView ID="ListView1" runat="server"
    ItemPlaceholder ID="PlaceHolder 1">

    <ItemTemplate >

    <dl>

    <dt>

    <asp:Label ID="Label1" runat="server" Text='Eval("Key s")'></asp:Label>

    </dt>

    <dd>

    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

    </dd>

    </dl>

    </ItemTemplate>

    <LayoutTemplate >

    <asp:PlaceHolde r ID="PlaceHolder 1" runat="server"> </asp:PlaceHolder >

    </LayoutTemplate>

    </asp:ListView>

    The code behind for the page that it is on is below. I am trying to get the
    text for the key and value pair into the labels above. I get 2 different
    errors depending on what I use in the code behind. There will be comments
    where required.

    namespace Contracts {

    public partial class _Default : System.Web.UI.P age {

    //create a contract for use in the page.

    Contract StockContract = new Contract();

    protected void Page_Load(objec t sender, EventArgs e) {

    //create a ContractDiction ary and assign it to the Dictionary property.

    StockContract.D ictionary = new ContractDiction ary<string, string>();



    //add an entry to the dictionary

    StockContract.D ictionary.Add(" Word", "Just testing here...");



    //dataBind the listview control where the entries from the dictionary will
    be seen to the Dictionary

    ListView1.DataS ource = StockContract.D ictionary;

    ListView1.DataB ind();

    //these next 2 lines cause a 'unable to cast type System.Web.UI.C ontrol to
    System.Web.UI.W ebControls.Labe l error.

    //Label WordLabel = (Label)ListView 1.Controls[0];

    //Label DefinitionLabel = (Label)ListView 1.Controls[1];



    //these next 2 lines cause an 'Object set to null reference' error.

    //Label WordLabel = (Label)ListView 1.FindControl(" Label1");

    //Label DefinitionLabel = (Label)ListView 1.FindControl(" Label2");



    //cycle through the key/value pair and give the values to the Labels inside
    the ListView1.

    foreach(KeyValu ePair<string, stringvalues in StockContract.D ictionary) {

    WordLabel.Text = values.Key;

    DefinitionLabel .Text = values.Value;

    }

    }

    }

    }



    Any ideas how to fix this?






  • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

    #2
    RE: unable to cast type System.Web.UI.C ontrol to System.Web.UI.W ebCont

    you labels are not direct children of the listview. they are children of the
    template that is a child of the listview. you need to do a recursive search
    for the labels.

    -- bruce (sqlwork.com)


    "Andy B" wrote:
    I have the following listView control on a page:
    >
    <asp:ListView ID="ListView1" runat="server"
    ItemPlaceholder ID="PlaceHolder 1">
    >
    <ItemTemplate >
    >
    <dl>
    >
    <dt>
    >
    <asp:Label ID="Label1" runat="server" Text='Eval("Key s")'></asp:Label>
    >
    </dt>
    >
    <dd>
    >
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    >
    </dd>
    >
    </dl>
    >
    </ItemTemplate>
    >
    <LayoutTemplate >
    >
    <asp:PlaceHolde r ID="PlaceHolder 1" runat="server"> </asp:PlaceHolder >
    >
    </LayoutTemplate>
    >
    </asp:ListView>
    >
    The code behind for the page that it is on is below. I am trying to get the
    text for the key and value pair into the labels above. I get 2 different
    errors depending on what I use in the code behind. There will be comments
    where required.
    >
    namespace Contracts {
    >
    public partial class _Default : System.Web.UI.P age {
    >
    //create a contract for use in the page.
    >
    Contract StockContract = new Contract();
    >
    protected void Page_Load(objec t sender, EventArgs e) {
    >
    //create a ContractDiction ary and assign it to the Dictionary property.
    >
    StockContract.D ictionary = new ContractDiction ary<string, string>();
    >
    >
    >
    //add an entry to the dictionary
    >
    StockContract.D ictionary.Add(" Word", "Just testing here...");
    >
    >
    >
    //dataBind the listview control where the entries from the dictionary will
    be seen to the Dictionary
    >
    ListView1.DataS ource = StockContract.D ictionary;
    >
    ListView1.DataB ind();
    >
    //these next 2 lines cause a 'unable to cast type System.Web.UI.C ontrol to
    System.Web.UI.W ebControls.Labe l error.
    >
    //Label WordLabel = (Label)ListView 1.Controls[0];
    >
    //Label DefinitionLabel = (Label)ListView 1.Controls[1];
    >
    >
    >
    //these next 2 lines cause an 'Object set to null reference' error.
    >
    //Label WordLabel = (Label)ListView 1.FindControl(" Label1");
    >
    //Label DefinitionLabel = (Label)ListView 1.FindControl(" Label2");
    >
    >
    >
    //cycle through the key/value pair and give the values to the Labels inside
    the ListView1.
    >
    foreach(KeyValu ePair<string, stringvalues in StockContract.D ictionary) {
    >
    WordLabel.Text = values.Key;
    >
    DefinitionLabel .Text = values.Value;
    >
    }
    >
    }
    >
    }
    >
    }
    >
    >
    >
    Any ideas how to fix this?
    >
    >
    >
    >
    >
    >
    >

    Comment

    • Andy B

      #3
      Re: unable to cast type System.Web.UI.C ontrol to System.Web.UI.W ebCont

      How do you do this?


      "bruce barker" <brucebarker@di scussions.micro soft.comwrote in message
      news:EFC3C4FB-F431-43BB-A0EA-33C6F2688A99@mi crosoft.com...
      you labels are not direct children of the listview. they are children of
      the
      template that is a child of the listview. you need to do a recursive
      search
      for the labels.
      >
      -- bruce (sqlwork.com)
      >
      >
      "Andy B" wrote:
      >
      >I have the following listView control on a page:
      >>
      ><asp:ListVie w ID="ListView1" runat="server"
      >ItemPlaceholde rID="PlaceHolde r1">
      >>
      ><ItemTemplat e>
      >>
      ><dl>
      >>
      ><dt>
      >>
      ><asp:Label ID="Label1" runat="server" Text='Eval("Key s")'></asp:Label>
      >>
      ></dt>
      >>
      ><dd>
      >>
      ><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
      >>
      ></dd>
      >>
      ></dl>
      >>
      ></ItemTemplate>
      >>
      ><LayoutTemplat e>
      >>
      ><asp:PlaceHold er ID="PlaceHolder 1" runat="server"> </asp:PlaceHolder >
      >>
      ></LayoutTemplate>
      >>
      ></asp:ListView>
      >>
      >The code behind for the page that it is on is below. I am trying to get
      >the
      >text for the key and value pair into the labels above. I get 2 different
      >errors depending on what I use in the code behind. There will be comments
      >where required.
      >>
      >namespace Contracts {
      >>
      >public partial class _Default : System.Web.UI.P age {
      >>
      >//create a contract for use in the page.
      >>
      >Contract StockContract = new Contract();
      >>
      >protected void Page_Load(objec t sender, EventArgs e) {
      >>
      >//create a ContractDiction ary and assign it to the Dictionary property.
      >>
      >StockContract. Dictionary = new ContractDiction ary<string, string>();
      >>
      >>
      >>
      >//add an entry to the dictionary
      >>
      >StockContract. Dictionary.Add( "Word", "Just testing here...");
      >>
      >>
      >>
      >//dataBind the listview control where the entries from the dictionary
      >will
      >be seen to the Dictionary
      >>
      >ListView1.Data Source = StockContract.D ictionary;
      >>
      >ListView1.Data Bind();
      >>
      >//these next 2 lines cause a 'unable to cast type System.Web.UI.C ontrol
      >to
      >System.Web.UI. WebControls.Lab el error.
      >>
      >//Label WordLabel = (Label)ListView 1.Controls[0];
      >>
      >//Label DefinitionLabel = (Label)ListView 1.Controls[1];
      >>
      >>
      >>
      >//these next 2 lines cause an 'Object set to null reference' error.
      >>
      >//Label WordLabel = (Label)ListView 1.FindControl(" Label1");
      >>
      >//Label DefinitionLabel = (Label)ListView 1.FindControl(" Label2");
      >>
      >>
      >>
      >//cycle through the key/value pair and give the values to the Labels
      >inside
      >the ListView1.
      >>
      >foreach(KeyVal uePair<string, stringvalues in StockContract.D ictionary)
      >{
      >>
      >WordLabel.Te xt = values.Key;
      >>
      >DefinitionLabe l.Text = values.Value;
      >>
      >}
      >>
      >}
      >>
      >}
      >>
      >}
      >>
      >>
      >>
      >Any ideas how to fix this?
      >>
      >>
      >>
      >>
      >>
      >>
      >>

      Comment

      Working...