| |
|
posted by Tihomir Ivanov on 16 January 2010 19:17
You may often use the Response's Redirect method to open new pages, ex:
Response.Redirect("http://www.devtheweb.net");
It will open the url in the same window.
If you want to open url by server side in a new window, it cannot be done using the Response's Redirect method.
But it can be done using JavaScript, here is an example:
ClientScript.RegisterStartupScript(typeof(Page), "key", "<script>window.open('http://www.devtheweb.net','_blank')</script>");
|
|
posted by Tihomir Ivanov on 08 December 2009 16:57
I'm writing ability user of my site http://www.devtheweb.net to have ability to change their avatar photos by uploading their own image files.
In this article you can find:
- how to check on server-side if file is selected by FileUpload
- how to check on server-side if Image file is selected by FileUpload
- how to check Image dimensions from file selected by FileUpload
In .aspx file we have a FileUpload Control, Button to submit file and a Label to output result in it:
<div>
<asp:FileUpload ID="_customImageFU" runat="server" />
</div>
<div>
<asp:Label ID="_resultLbl" runat="server" ForeColor="Red"></asp:Label>
</div>
<div>
<asp:Button ID="_uploadImageBtn" runat="server" onclick="_uploadImageBtn_Click" Text="Upload" />
</div>
in code-behind file, we handle the Submit Button Click and check for valid image:
protected void _uploadImageBtn_Click(object sender, EventArgs e)
{
string extension, filename;
try
{
//t checks if file exists
if (!_customImageFU.HasFile)
{
_resultLbl.Text = "Please, Select a File!";
return;
}
//t checks file extension
extension = System.IO.Path.GetExtension(_customImageFU.FileName).ToLower();
if (!extension.Equals(".jpg") && !extension.Equals(".jpeg")
&& !extension.Equals(".gif") && !extension.Equals(".png"))
{
_resultLbl.Text = "Only image files (.JPGs, .GIFs and .PNGs) are allowed.";
return;
}
//t checks if image dimensions are valid
if (!ValidateFileDimensions(100, 100))
{
_resultLbl.Text = "Maximum allowed dimensions are: width <= 100px and height <= 100px.";
return;
}
filename = Server.MapPath("~/some_folder/") + "some-file-name" + extension;
_customAvatarFU.SaveAs(filename);
}
catch (Exception)
{
//t handle the exception
}
}
public bool ValidateFileDimensions(int aHeight, int aWidth)
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(_customImageFU.PostedFile.InputStream))
{
return (image.Height <= aHeight && image.Width <= aWidth);
}
}
That's all :)
|
|
posted by Tihomir Ivanov on 17 November 2009 13:22
It's very easy to display RSS Feeds using XmlDataSource and DataList:
Just copy the code below and replace the DataFile value with the RSS Feed url you want
<asp:XmlDataSource ID="RSSDataSource" Runat="server" DataFile="http://www.dev-the-web.com/blog/feed/"
XPath="rss/channel/item" EnableCaching="true" CacheDuration="300" CacheExpirationPolicy="Sliding" />
<asp:DataList ID="dlRSSItems" Runat="server" DataSourceID="RSSDataSource">
<ItemTemplate>
<li><a href='<%# XPath("link") %>'><%# XPath("title") %></a></li>
</ItemTemplate>
</asp:DataList>
That's all :)
|
|
posted by Tihomir Ivanov on 12 November 2009 16:40
Sometimes it's necessary to get the current Theme Name and it's physical folder path (ex. to check if some file exists in the current theme folder).
We usually define the corrent theme in the WebConfig File:
...
<pages theme="Default" ... >
...
Now, how we could get the current theme name programmatically (C# example):
using System.Web.Configuration;
...
PagesSection pageSection = ConfigurationManager.GetSection("system.web/pages") as PagesSection;
//t pageSection contains property Theme with value - the name of the current Theme
string currentTheme = pageSection.Theme;
//t now we could get the current theme folder physical path
string themeDirPhysicalPath = HttpContext.Current.Server.MapPath("~/App_Themes/" + pageSection.Theme);
...
well, that seems to be All :)
|
|
posted by Tihomir Ivanov on 10 November 2009 13:19
Sometimes it's usefult to add default ListItem to a DropDownList Control (ex. '[none]' or '[empty]'),
You can do it in two ways:
1st Way -Programmatically:
_yourDropDownList.Items.Insert(0, new ListItem("[none]", "some default value");
The first parameter of the Insert Method is the index where to insert the item, it's very useful because you can insert the default item after loading the other items.
2nd Way Using theDropDownList's AppendDataBoundItems Property:
You can add it into your aspx page
<asp:DropDownList ID="_yourDropDownList" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="[none]" Value="some default value" />
</asp:DropDownList>
That's all.
|
|
posted by Tihomir Ivanov on 20 October 2009 17:22
Instead of using LinqDataSource to laod data for GridView you can load data for it programatically:
in the aspx file we have simple GridView:
<asp:GridView ID="_someGV" runat="server" AutoGenerateColumns="false"
<Columns>
<asp:BoundField DataField="..." />
</Columns>
</asp:GridView>
in the code-behind class we add all necessary functionality to load the data using LINQ:
...
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
DBDataContext dbContext;
dbContext = new DBDataContext();
var dataItems = from s in dbContext.someTable
select s;
_someGV.DataSource = dataItems;
_someGV.DataBind();
}
}
catch (Exception)
{}
}
Now, we'll add the paging functionality:
<asp:GridView ID="_someGV" runat="server" AutoGenerateColumns="false"
AllowPaging="true" PageSize="100"
onpageindexchanging="_someGV_PageIndexChanging"
<Columns>
<asp:BoundField DataField="..." />
</Columns>
</asp:GridView>
in the code-behind class handle the PageIndexChaging Event:
protected void _somesGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
DBDataContext dbContext;
//t we need to reload all data gaian
dbContext = new DBDataContext();
var dataItems = from s in dbContext.someTable
select s;
_someGV.DataSource = dataItems;
_someGV.PageIndex = e.NewPageIndex;
_someGV.DataBind();
}
catch (Exception)
{}
}
|
|
posted by Tihomir Ivanov on 28 September 2009 07:37
You may wonder what's the difference between Server.HtmlEncode and HttpUtility.HtmlEncode.
Actually, Server is an instance of HttpServerUtility:
Public ReadOnly Property Server As HttpServerUtility
Get
If (Me._server Is Nothing) Then
Me._server = New HttpServerUtility(Me)
End If
Return Me._server
End Get
End Property
And as noted HttpServerUtility.HtmlEncode makes a straight call to the shared method HttpUtility.HtmlEncode
Public Sub HtmlEncode(ByVal s As String, ByVal output As TextWriter)
HttpUtility.HtmlEncode(s, output)
End Sub
So for best performance, you could call directly to HttpUtility.HtmlEncode yourself.
|
|