GridView RenderControl
When trying to render a gridview as html,
you may encounter the following errors:
1. Control of type ‘GridView’ must be placed inside a form tag with runat=server.
This error occurs because the GridView is already placed in a form (usually the one from the master page)
To fix this, either override the function VerifyRenderingInServerForm like this:
{
// Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time
return;
}
or remove the grid control before rendering it, and add them back after render
int GridIndex = 0;
if (parent != null)
{
GridIndex = parent.Controls.IndexOf(grid);
parent.Controls.Remove(grid);
}
grid.RenderControl(hw);
if (parent != null)
{
parent.Controls.AddAt(GridIndex, grid);
}
Another alternative to avoid the override is to do this:
grid.HeaderRow.RenderControl(hw);
foreach (GridViewRow row in grid.Rows)
{
row.RenderControl(hw);
}
grid.FooterRow.RenderControl(hw);
grid.RenderEndTag(hw);
2. asp.net RegisterForEventValidation can only be called during Render()
in this case, add in the aspx page, in the page tag, the directive: EnableEventValidation =”false”
Like this:
In this case, if you run on a framework >= 3.5, you also need in web.config, the entry: httpRuntime requestValidationMode=”2.0″, in the system.web tag, like this:
............
<system.web>
............
<httpRuntime requestValidationMode="2.0"/>
</system.web>
</configuration>
align 3 divs, 2 fixed sides and the middle div auto size
How to align 3 divs using only css, from which 2 have fixed sides and the middle one will auto fit.
<html>
<head>
<style type="text/css">
body { min-width: 630px; }
#header { background-color:brown; }
#mainContent { width:1000px; margin:0px auto; }
#container { padding-left: 200px; padding-right: 190px; }
#container .column { position: relative; float: left; }
#center { padding: 10px 20px; width: 100%; background-color:green; }
#left { width: 180px; padding: 0 10px; right: 240px; margin-left: -100%; background-color:blue; }
#right { width: 130px; padding: 0 10px; margin-right: -190px; background-color:red; }
#footer { clear: both; background-color:brown; }
/*** IE Fix ***/
* html #left { left: 150px; }
</style>
</head>
<body>
<div id="mainContent">
<div id="header">header</div>
<div id="container">
<div id="center" class="column">mid container</div>
<div id="left" class="column">left side</div>
<div id="right" class="column">right side</div>
</div>
<div id="footer">footer</div>
</div>
</body>
</html>
PHP – Create a hierarchical menu based on the category level
Having an array of categories as a tree (parent->childs relations), retrieved in the desired order, i.e.:
category1
|—subcategory 1_1
|——subcategory 1_1_1
|——subcategory 1_1_2
category2
|—subcategory2
category 3
To display them in the above order, using ul and li tags:
foreach($objCategoriesList->Records as $recordItem)
{
if ($recordItem->Level > $prevLevel) echo '<ul><li>';
else if ($recordItem->Level == $prevLevel) echo '</li><li>';
else if ($recordItem->Level < $prevLevel)
{
for ($levIndex = $recordItem->Level; $levIndex < $prevLevel; $levIndex++)
{
echo '</li></ul>';
}
echo '</li><li>';
}
echo '<a href="categories.php?cat=' .$recordItem->UrlKey.'">' .$recordItem->Name .'</a>';
$prevLevel = $recordItem->Level;
}
echo '</li></ul>';
If there are also articles to be added:
$selectedClass = '';
if ($currentCategory != null)
{
$arrTreeList = $objCategoriesMap->GetTreeList($currentCategory->CategoryId);
if ($arrTreeList != null)
{
$currentCategoryStart = false;
foreach($arrTreeList as $recordItem)
{
if ($currentCategory->CategoryId == $recordItem->CategoryId) $selectedClass = 'class="selected"';
else $selectedClass = '';
if ($recordItem->Level <= $currentCategory->Level && $currentCategoryStart)
{
include("blocks/articles_list_nav.php");
$currentCategoryStart = false;
}
if ($recordItem->Level > $prevLevel) echo '<ul><li>';
else if ($recordItem->Level == $prevLevel) echo '</li><li>';
else if ($recordItem->Level < $prevLevel)
{
for ($levIndex = $recordItem->Level; $levIndex < $prevLevel; $levIndex++)
{
echo '</li></ul>';
}
echo '</li><li>';
}
$prevLevel = $recordItem->Level;
if ($currentCategory->CategoryId == $recordItem->CategoryId)
$currentCategoryStart = true;
echo '<a '.$selectedClass. ' href="link_' .$recordItem->UrlKey .'.html">' .$recordItem->Name .'</a>';
}
echo '</li></ul>';
}
}
Htaccess file for redirect
– some servers will require this option enabled
#Options +FollowSymlinks
– if the apache server has mod_rewrite enabled
<IfModule mod_rewrite.c>
– redirect 301 all files that are found in the folder : /home/viewcity/ , to the specified url (using regular expressions)
RedirectMatch 301 /home/viewcity/(.+)$ http://www.meteovremea.com/meteo-vremea-in-$1.html
RewriteEngine On
– if the REQUEST_URI does not start with /google91849e903c5ce132.html , then will apply the rules
RewriteCond %{REQUEST_URI} !^/google91849e903c5ce132.html
– all files that starts with getforecast will be processed trough the file forecast.php
RewriteRule ^getforecast(.+)$ forecast.php?str=$1 [L]
– all files that starts with getpartforecast/ will be processed trough the file forecast_partners.php
RewriteRule ^getpartforecast/(.+)$ forecast_partners.php?str=$1 [L]
– all files that starts with city_details_ and ends in .html will be processed trough the file city_details.php
RewriteRule ^city_details_(.+)\.html$ /city_details.php?pl=$1 [L]
</IfModule>
WordPress 404 error headers for custom pages
If you create a custom page which uses the wordpress engine,
you might get a 404 error header even if the page displays correctly (being redirected trough .htaccess).
In this case, a direct work around … unless one creates a plugin.
I had in .htacess a redirect to get all html files which starts with hotel- and pass them to a file which handles that by the query.
So, in the file: wp_includes\classes.php, in the function handle_404,
above this condition:
add a new condition
{
status_header( 200 );
return;
}
else
Then, in the file: wp_includes\query.php, in the function get_queried_object,
where you find this condition:
$this->queried_object = $this->post;
$this->queried_object_id = (int) $this->post->ID;
}
add a new condition, so that the code will look like this:
$this->queried_object = $this->post;
if (isset($this->post->ID)
$this->queried_object_id = (int) $this->post->ID;
}
WordPress error: You do not have sufficient permissions to access this page
recently, after installed a wordpress plugin (more exactly “si-captcha-for-wordpress”), when i hit the settings links to configure that plugin, i received this error:
“You do not have sufficient permissions to access this page error“.
After a little research, realized that this happened because i renamed the plugin folder to “captcha-for-wordpress”.
Renamed it back to “si-captcha-for-wordpress” and then i could access the setting page for that plugin.
Seems that by renaming the folder from it’s original name will trigger this error.
When received the error, the url from the browserwas like this:
mysite/wp-admin/plugins.php?page=si-captcha-for-wordpress/si-captcha.php
So this means that wordpress searches the file “si-captcha.php” in the folder “si-captcha-for-wordpress”, therefor go check on your ftp if the name of the folder corresponds with the one that wordpress requires, and if not, change it to the one displayed in the browser url.
si-captcha-for-wordpress
MSSQL – select all parents details from a hierarchical tale
Having 2 tables, table 1 named ‘categories’ with the fields : ‘category_id’, ‘parent_id’, ‘category_name’
and table 2 named category_xref, with the fields ‘ category_id’,'parent_id’ (which is the references for the categories),
to select detail for all the parents for a given category, use this code:
(
SELECT parent_id AS category_id FROM category_xref WHERE category_id = @category_id
UNION
SELECT @category_id AS category_id
) t1 JOIN categories c ON c.category_id = t1.category_id
MSSQL – Update a table column incremental
SET @Id = 0
UPDATE TABLE_NAME SET @Id = column_name = @Id + 1
Port 80 is being used on Windows, by SYSTEM (PID 4)
Problem: trying to run apache or other application server on port 80, but port 80 is being in use.
If you run in a command window the command: “netstat -aon” and you get: TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 4
In process task manager, PID 4 is SYSTEM, which is not really helpful, because there are more windows application which can use that port.
In this case, you should verify for the following things:
1. check if Skype is started, in this case close it
2. check if IIS (Internet Information Services) is started, if so, then stop the it from Control Panel -> Administrative Tools -> Internet Information Services (IIS) Manager,
or from command line : NET STOP IISADMIN /Y
3. check if SQL Server Reporting Services (MSSQLSERVER) is started, in the windows services, if so stop it
Alternative to Image.FromFile which doesn’t lock the file
If you get the error that file is in use when using Image.FromFile function,
you can use this simple alternative:
{
FileStream stream = new FileStream(filePath, FileMode.Open);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
stream.Close();
return image;
}