Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Find asp.net listview control in Item Template

Find asp.net listview control in Item Template

Scheduled Pinned Locked Moved C#
csharpasp-netdatabasesysadmin
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akosidandan
    wrote on last edited by
    #1

    Hello Experts, I would like to ask how will I find the listview control or use the correct event. My goal is that I want to check the listview control text value inside of listview item template after it load the data, then I have a condition to trigger some code depends on what my control text result. Here is my Code I am not sure why it does not work and it always give me

    Object reference not set to an instance of an object.

    The aspx page Code

    <ItemTemplate>
    <tr style="">
    <td>
    <asp:Label ID="AnnouncementMessageLabel" runat="server"
    Text='<%# Eval("AnnouncementMessage") %>' />
    </td>
    <td>
    <asp:Literal ID="projectType" runat="server"
    Text='<%# Eval("AnnouncementType") %>'>
    </asp:Literal>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </td>
    </tr>
    </ItemTemplate>

    My behind file code.

    protected void ListView1_DataBound(object sender, EventArgs e)
    {
    foreach (ListViewDataItem item in ListView1.Items)
    {
    Literal project = new Literal();
    Button b = new Button();
    b = (Button)item.FindControl("Button1");
    project =(Literal)item.FindControl("projectType");
    if (project.Text == "Project")
    {
    b.Text = "This is a project POST";
    }

            }
    
        }
    

    Any comments or suggestions are kindly appreciated.

    A 1 Reply Last reply
    0
    • A akosidandan

      Hello Experts, I would like to ask how will I find the listview control or use the correct event. My goal is that I want to check the listview control text value inside of listview item template after it load the data, then I have a condition to trigger some code depends on what my control text result. Here is my Code I am not sure why it does not work and it always give me

      Object reference not set to an instance of an object.

      The aspx page Code

      <ItemTemplate>
      <tr style="">
      <td>
      <asp:Label ID="AnnouncementMessageLabel" runat="server"
      Text='<%# Eval("AnnouncementMessage") %>' />
      </td>
      <td>
      <asp:Literal ID="projectType" runat="server"
      Text='<%# Eval("AnnouncementType") %>'>
      </asp:Literal>
      <asp:Button ID="Button1" runat="server" Text="Button" />
      </td>
      </tr>
      </ItemTemplate>

      My behind file code.

      protected void ListView1_DataBound(object sender, EventArgs e)
      {
      foreach (ListViewDataItem item in ListView1.Items)
      {
      Literal project = new Literal();
      Button b = new Button();
      b = (Button)item.FindControl("Button1");
      project =(Literal)item.FindControl("projectType");
      if (project.Text == "Project")
      {
      b.Text = "This is a project POST";
      }

              }
      
          }
      

      Any comments or suggestions are kindly appreciated.

      A Offline
      A Offline
      akosidandan
      wrote on last edited by
      #2

      Fix it when I almost lost hope trying :D when I suddenly copy paste my design of item template to alternating item template of my listview. It seems that the listview databound event is affected by both alternating item template and item template.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups