Me Myself & C#

Manoj Garg’s Tech Bytes – What I learned Today

Archive for July, 2008

A Day Out with AJAXToolKit’s CollapsiblePanelExtender

Posted by Manoj Garg on July 31, 2008

Last Week I was assigned a task to add Collapse/Expand animation to some portion of an ASP.NET User Control in my project. At that time I never thought, even remotely, that this simple stuff is going to be such a big nightmare for me. Things went bad in all possible way they can. 😦

Creating an Collapse/Expand effect in ASP.NET is quite simple You use AJAXToolKit’s CollapsiblePanelExtender, specify Header, Content panes and few properties and you are done. I did the same thing.

Following is the code I wrote for the initially, where I had a Header block named pnlWelcomeHeader for the header part of the Collapsible Panel containing just a single ASP Label control for displaying some header text, a Content block named pnlWelcomeContent containing another ASP Label for displaying a welcome message text to the user and finally an AJAXToolKit’s CollapsiblePanelExtender with all its properties defined.

   1: <%-- Welcome text Panel Header HTML  --%>
   2: <asp:Panel ID="pnlWelcomeHeader" runat="server" >
   3:      <table width = "100%">
   4:          <tr>
   5:               <td >
   6:                     <asp:Label ID="lblWelcomeHeader" Text="Welcome" runat="server"/>
   7:                      <asp:Image ID="_imageCollapse2" runat="server" ImageUrl = "~/images/icons/expand.gif" />        
   8:                </td>
   9:            </tr>
  10:      </table>
  11: </asp:Panel>
  12:  
  13: <%-- Collapsible Panel Extender--%>
  14: <asp:Panel ID="pnlWrapper" runat="server" SkinID="CollapsibleGenericContent">
  15:         <ajaxToolkit:CollapsiblePanelExtender ID="cpeWelcome" runat = "server" CollapseControlID="pnlWelcomeHeader" 
  16:              ExpandControlID ="pnlWelcomeHeader" TargetControlID = "pnlWelcomeContent" CollapsedSize="0" ImageControlID="_imageCollapse2" 
  17:             AutoCollapse = "false" AutoExpand="false" Collapsed="true" ExpandDirection = "Vertical" SuppressPostBack = "true" 
  18:             ExpandedImage ="~/images/icons/collapse.gif" CollapsedImage = "~/images/icons/expand.gif"> 
  19:         </ajaxToolkit:CollapsiblePanelExtender>
  20:  
  21:         <%-- Welcome text Content Panel HTML  --%>
  22:         
  23:         <asp:Panel ID="pnlWelcomeContent" runat="server">
  24:                   <asp:Label ID="welcome_msg" Text="Welcome" runat="server" />            
  25:         </asp:Panel>
  26: </asp:Panel>

But when I tried to run the website and test my code, I was amazed to see some weird behavior. Even when the Collapsed property of the extender was set to true, the panel was in expanded mode. On click of the Header block, the Extender is suppose to expand/Collapse the Content Panel but in my case it was not doing it. instead it appeared to do the expand/collapse animation i.e. on click of the header block the panel collapses but then suddenly it expands. The text in Header block was displayed correctly i.e. it shown “Show Details” when collapsed and “Hide Details ” when it was expanded. :-/

   After lots of head scratching and googling :), I found out the solution for this problem but not the reason why this weird thing is happening to me. Solution was to use “position:absolute” in the content block. So I wrapped a <DIV> tag around the welcome_msg ASP Label and applied this style to to that DIV and my Collapse/Expand not working problem was solved.

   1: <%-- Welcome text Content Panel HTML  --%>
   2: <asp:Panel ID="pnlWelcomeContent" runat="server">
   3:    <div id="_wrapperwelcome" style="border:solid 1px #ccc; position:absolute; width:100%;" runat="server">
   4:              <asp:Label ID="welcome_msg" Text="Welcome" runat="server" />            
   5:     </div>   
   6: </asp:Panel>

The DIV inside the panel in the abode code snippet did the trick. But this was just the beginning. Now the second thing I wanted to do with the collapsible panel was to show it in expanded mode first time when the page loads. but event after setting Collapsed property of the extender to false, it didn’t work out. 😦

Again after searching here and there got a solution for it but not the reason… Solution was to expand the panel in Page’s Code behind.

   1: cpeWelcome.ClientState = "false";
   2: cpeWelcome.Collapsed = false;

Now my collapsible panel was in expanded mode on page load.. Thank God…. But now I wanted to set the expand size for the collapsible panel so that when the content exceeds that length my panel starts scrolling.. Did I am asking too much from the collapsiblepanel 🙂

   1: ExpandedSize="250" // This is the property in extender that decides the maximum size

and here comes a much weird situation, now the collapsible panel didn’t expand at the page load but on the aspx page it occupies the blank space equal to its height i.e. in my case 250px and once it click on the header to collapse it the content of the panel gets displayed and then it collapses. that means it was just not working for the first time it loads and after that it works great.

After some hits and trials I wrote some dirty JavaScript code that fires the collapse/expand onclick event for the panel so that the Panel in shown in correct mode on page load.

   1: /* On ASPX page*/
   2:  
   3: function pageLoad(sender, args)
   4: {
   5:       //Following code is for expanding the Welcome Panel on Page Load
   6:      var objExtender = $find("<%=cpeWelcome.ClientID%>");
   7:      try{objExtender._doClose();}catch(e){}  // Collapse it
   8:      try{objExtender._doOpen();}catch(e){}  // expand it
   9: }
  10:  
  11: cpeWelcome.BehaviorID = cpeWelcome.ClientID; // In page code behind

So finally after all this my task was done…. 🙂

If anyone of you gets into this situation and finds a solution for this please do ping me pack..

Posted in AJAX, ASP.Net | Tagged: | 12 Comments »

Freezing a row in Asp.NET GridView

Posted by Manoj Garg on July 30, 2008

In ASP.NET Grid view we don’t have a scroll feature so that the content rows will scroll with mouse not the Header/Footer Rows. Recently in my project I got a requirement where I had to fix the Header and the Top Pager row of a grid view and keep the content of the gridview scrollable once the content height gets more then the container height.

After some hits & trials  and some googling, I got through.

Following is the style you need to use for making a row fix in the Grid.

/*below style is used for freezing grid header so that it doesn’t scrolls with grid rows*/
.GridHeaderFreezing

   position:relative; 
   top:expression(this.offsetParent.scrollTop);
   padding:0px;
   margin:0px;    
   z-index: 10;
}

and apply this CSS Class to the row you want to make static. for example:

<HeaderStyle CssClass=”GridHeaderFreezing“></HeaderStyle>

like wise you can apply this style to any of the Rows of a GridView.

PS: It works great with Internet Explorer but not on FF

Hope it helps 🙂

Posted in ASP.Net, CSS | Tagged: , , | 7 Comments »

How to repair a SQL Server 2005 Suspect database

Posted by Manoj Garg on July 17, 2008

Sometimes when you connect to your database server, you may find it in suspect mode. Your database server won’t allow you to perform any operation on that database until the database is repaired.

A database can go in suspect mode for many reasons like improper shutdown of the database server, corruption of the database files etc.

To get the exact reason of a database going into suspect mode can be found using the following query,

DBCC CHECKDB (‘YourDBname’) WITH NO_INFOMSGS, ALL_ERRORMSGS

Output of the above query will give the errors in the database.

To repair the database, run the following queries in Query Analyzer,

EXEC sp_resetstatus ‘yourDBname’;

ALTER DATABASE yourDBname SET EMERGENCY

DBCC checkdb(‘yourDBname’)

ALTER DATABASE yourDBname SET SINGLE_USER WITH ROLLBACK IMMEDIATE

DBCC CheckDB (‘yourDBname’, REPAIR_ALLOW_DATA_LOSS)

ALTER DATABASE yourDBname SET MULTI_USER

and you are done. 🙂

lightbulbYou should keep one thing in mind while using the above queries that the repair mode used here , REPAIR_ALLOW_DATA_LOSS, is a one way operation i.e. once the database is repaired all the actions performed by these queries can’t be undone. There is no way to go back to the previous state of the database. So as a precautionary step you should take backup of your database before executing above mentioned queries.

Ha-P Querying…

Posted in Database, SQL Server 2005 | Tagged: , , | 121 Comments »

Writing a MemoryStream to Response Object for opening a Open / Save / Cancel Dialogue in ASP.NET 2.0

Posted by Manoj Garg on July 17, 2008

Following code block can be used to show user on Open/Save/Cancel dialogue box in browser for a memory stream.

             if (ms != null)
               {
                    Byte[] byteArray = ms.ToArray();
                    ms.Flush();
                    ms.Close();
                    Response.BufferOutput = true;
                    // Clear all content output from the buffer stream
                    Response.Clear();
                    //to fix the “file not found” error when opening excel file
                    //See
http://www.aspose.com/Community/forums/ShowThread.aspx?PostID=61444
                    Response.ClearHeaders();
                    // Add a HTTP header to the output stream that specifies the default filename
                    // for the browser’s download dialog
                    string timeStamp = Convert.ToString(DateTime.Now.ToString(“MMddyyyy_HHmmss”));
                    Response.AddHeader(“Content-Disposition”,
                                       “attachment; filename=testFileName_” + timeStamp + “.fileextention”);
                    // Set the HTTP MIME type of the output stream
                    Response.ContentType = “application/octet-stream”;
                    // Write the data
                    Response.BinaryWrite(byteArray);
                    Response.End();
                }

Ha-P Coding lightbulb

Posted in ASP.Net, C# | Tagged: , , | 8 Comments »

XML documentation parse error: Whitespace is not allowed at this location. XML comment will be ignored.

Posted by Manoj Garg on July 4, 2008

Today while writing comment in a code file I got following error

XML documentation parse error: Whitespace is not allowed at this location. XML comment will be ignored.

After some searching on Google found the following link describing the reason behind the error..thumbs_up

Actually the problem was xml comments don’t allow special characters like &, >, < etc in them.. so either don’t use them or Use & < etc.

Ha-P Coding .. smile_regular

Posted in ASP.Net, C# | Tagged: | 6 Comments »

Fixing Sys.WebForms.PageRequest ManagerParserErrorException in AJAX

Posted by Manoj Garg on July 3, 2008

Recently I came across a problem where my aspx page was throwing an exception

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near’

after some googling lightbulb found following article by Eilon Lipton

http://weblogs.asp.net/leftslipper/archive/2007/02/26/sys-webforms-pagerequestmanagerparsererrorexception-what-it-is-and-how-to-avoid-it.aspx

While goin through this article the reason why I was getting that exception.

Actually I has a page which had an UpdatePanel and a ASP Button control on clicking of which I wanted to load another aspx page in the same masterpage. for this what I was using Server.Transfer, which was actually causing the problem. After I replaced the Server.Transfer with Response.Redirect. I got through and it saved lots of my time. thumbs_up

Ha-P Coding.. smile_teeth

Posted in ASP.Net | Tagged: , , | 2 Comments »