Reputation: 259
I have a textbox
control and a button
control inside a listview
, I want to hide those controls when required from code behind, I have tried using something like this
ListViewName.FindControl("TextBoxComment").Visible = false;
and
((TextBox)ListViewName.FindControl("TextBoxComment")).Visible = false
but when I run the code it gives NullReference Exception
Please help.
<ItemTemplate>
<table>
<tr>
<td>
<asp:TextBox ID="TextBoxComment" runat="server" >
</asp:TextBox>
</td>
<td>
<asp:Button ID="ButtonSubmit" runat="server"
CommandName="Comment"
CommandArgument='<%# Eval("FlowPostID") %>'/>
</td>
</tr>
</table>
</ItemTemplate>
Upvotes: 1
Views: 1748
Reputation: 259
I did this, it worked
TextBox Box = new TextBox()
Button Butt = new Button();
Box = (TextBox)e.Item.FindControl("TextBoxComment")
Butt = (Button)e.Item.FindControl("ButtonSubmit")
Box.Visible = false;
That worked perfectly fine :) Thank you all for your effort :)
Upvotes: 0
Reputation: 8628
You need to do this on the ListView's ItemDataBound Event Handle.
var item = (ListViewItem)e.DataItem;
var txtBox = (txtBox)item.FindControl("TextBoxComment");
if(txtBox != null)
{
txtBox.Visible = false;
}
And so forth...
Upvotes: 1
Reputation: 17614
You need to check for the null values
var textbox=ListViewName.FindControl("TextBoxComment");
if(textbox!=null)
ListViewName.FindControl("TextBoxComment").Visible = false;
Upvotes: 0