0

I have the standard search scenario: user enters search parameters and clicks the search button - results show up in the panel below. What I want to do is to control the visibility of the panel based on whether search results are available or not. I have tried to code my panel as follows, but I am getting a parse error at run time.

<asp:Panel ID="ResponsePanel" Visible="<%= Model != null %>" runat="server">
    ...
</asp:Panel>

The parse error I am getting is this:

Cannot create an object of type 'System.Boolean' from its string representation
'<%= Model != null %>' for the 'Visible' property.

How do I toggle the panel depending on the availability of the model?

Naresh
  • 23,937
  • 33
  • 132
  • 204
  • 2
    Well, for starters you should not **I REPEAT NOT** use `` in an MVC application – Chase Florell Nov 26 '10 at 03:23
  • asp:panel? Seriously..dude! Why don't you add `EnableViewState=true` on there too - see what happens, lol – RPM1984 Nov 26 '10 at 03:40
  • 1
    haha, @RPM ... good to see you. – Chase Florell Nov 26 '10 at 03:42
  • Good to see you too (in the virtual sense). BTW, +1 to your answer. – RPM1984 Nov 26 '10 at 03:44
  • Thanks! Yeah I always see you on around this time of night (GMT -7:00). And some of the crap you say really makes me laugh. – Chase Florell Nov 26 '10 at 03:46
  • Thanks for all the help guys! I have literally started learning ASP.NET since last 2 days. Just catching on to the fact that I should be focusing on MVC2/3 etc. and not WebForms! It is difficult to figure out best practices since the net is full of old stuff and new stuff! I don't yet know what partials are, I will read up. Solved my immediate issue by replacing the panel with <% if (Model.HasValue) { %> - thanks rockinthesixstring. Can you suggest a good book or reference on current best practices? – Naresh Nov 26 '10 at 04:19
  • see the comment in my answer. – Chase Florell Nov 26 '10 at 04:40

1 Answers1

3

As I said in my comment, you should not be using a <asp:panel> in your asp.net application.

Instead, build a partial view (ascx)

<% if(! Model.HasValue){ %>
    <%: Html.Partial("WhatWouldGoInYourPanel") %>
<% } %>

Then in your Partial view, you can put all the "stuff" that you want to show if the Model is empty.

You can put the partial in one of two places. If it's shared, you put it in the Views/Shared folder. If it's specific to the Controller, you put it in the Views/[ControllerName] folder.

note: please forgive my C#... I'm not all that good.

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • Follow up question - I assume HasValue is a new attribute on the model and you are also suggesting that Model should never be null. Correct? Also why should not be used in a MVC application? – Naresh Nov 26 '10 at 04:23
  • MVC is not webforms. Most of what you will find on the net currently deals with webforms, so you will find a lot of stuff with `` You do not want to use ANY "controls" on your MVC Views (they are not webforms). – Chase Florell Nov 26 '10 at 04:33
  • [HasValue](http://stackoverflow.com/questions/676078/which-is-preferred-nullable-hasvalue-or-nullable-null) simply check to see if there is "anything" in the object. `! null` calls the `HasValue` anyways. – Chase Florell Nov 26 '10 at 04:35
  • If you want to learn MVC... some great starts will be to check out [NerdDinner](http://nerddinner.codeplex.com/) along with [Pro MVC Framework](http://www.amazon.com/dp/1430210079/?tag=stackoverfl08-20) and [Professional ASP.NET MVC2](http://www.amazon.com/Professional-ASP-NET-MVC-Wrox-Programmer/dp/0470643188/ref=sr_1_2?s=books&ie=UTF8&qid=1290746253&sr=1-2). I bought both, and they're great. Also read these blogs [scottgu](http://bit.ly/hSemEr), [haacked](http://bit.ly/ewZoKx) and [Hanselman](http://bit.ly/fQ3evN). – Chase Florell Nov 26 '10 at 04:39
  • also. If you're just learning asp.net then you "might" want to go straight to MVC 3 and razor (currently in RC, but definitely the direction of MVC), or at very least, be sure to use MVC 2. – Chase Florell Nov 26 '10 at 04:41