Find asp.net listview control in Item Template
-
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.
-
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.
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.