Basically, I am trying to put validation control in of Listview. But, I am not able to specify ControlToValidate = “grpNameTextBox”.
I tried to put
((RequiredFieldValidator)ListView1.FindControl("RequiredFieldValidator1")).ControlToValidate = ((TextBox)ListView1.FindControl("grpNameTextBox")).ID;
in different Events, but not able to do it.
Afterwards, I deleted Validation Control, and put simple Label. Then in ‘ItemInserting’ event I put this code :
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
TextBox t1 = (TextBox)ListView1.FindControl("grpNameTextBox"); // Getting Null Exception here
if (t1.Text.Trim() == null)
{
throw new System.Exception("Field cannot be empty");
}
}
But getting “Object reference not set to an instance of an object.” error.
Can any one tell me, where I am wrong?
.aspx part is given below :
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</td>
<td>
</td>
<td>
<asp:TextBox ID="grpNameTextBox" runat="server" Text='<%# Bind("grpName") %>' />
<asp:Label ID="lblError" runat="server" Text=""></asp:Label>
</td>
</tr>
</InsertItemTemplate>
Thanks.
TextBox txt_btn = (TextBox)e.Item.FindControl(“grpNameTextBox”);