I am in need of validati开发者_StackOverflow社区ng a TreeView to make sure that users have selected something. What I need is something that mimics a RequiredFieldValidator and stops a page from posting back with any data until something is selected.
I am using C# and ASP.NET 3.5
I have tried this and it did not work for me:
if (TreeView.SelectedNode.Value == null)
{
lblError.Text = "Required";
lblError.ForeColor = Color.Red;
}
I am at a loss, and any help would be very appreciated.
As far as I can say, TreeView is basically used for navigation and in navigation you cannot force a user to choose node. But if its requirement in your app. If your are navigating to other page, then you can do one thing, add some flag or value to your querystring so that the app will know whether its coming from treeview or directly by copy pasting the url. Catch it all in your Page_Load event. or Create a Session variable in the treeview selected event Session["TreeviewChecked"]= true;
In your code check if Session is null or not.
I hope this will help.
精彩评论