Archive for the ‘C#’ Category
DataBinder.Eval function
I am creating a blog and I need to preserve white space for user comment. So I need to put <pre> tag around the comment content. In ASP.NET, I could do it in the ItemDataBound function and then use the FindControl function and append <pre> tags to the text property of the label. But there is a much better and easier way. Use the DataBinder.Eval function like below
<asp:Label ID=”CommentLabel” runat=”server” Text=’<%# DataBinder.Eval(Container.DataItem,”comment_content”,”<pre>{0}</pre>”) %>’></asp:Label>
In my case, the “comment_content” is the name of the field in my table that contains the user htmlencoded content.
Escape double quote in C#
If you want to represent a double quote in a string literal, the escape sequence is two double quotes with “@” system in the front.
string myString = @”<xml attribute=“”my attribute“”/>”;
Ways to pass data for asp.net website
- ViewState – Maintain data for postback for a page
- Context items collection – Simple data pass from 1 page to another. (common use for server farm)
- QueryString – put value in the URL
- Session – Server store value per user (example: user id)
- Application – For the whole web application
- Cache – Server storing data among all users (example: the product table)
- Cookie – Store information on the user’s pc for a long time (example: track first time visit)
- PreviousPage object
…and others
Redirect if there is no PreviousPage
Problem:
An online form has been broken down into several steps. Each step is a single aspx page. Sometimes user goes directly into a step in the middle.
Solution:
To prevent visitor to go to a page directly, below is the code to check to see if there is no previouspage and redirect it to the default.aspx
In VB:
If IsNothing(PreviousPage) Then
Response.Redirect("Default.aspx" )
End If
In C#:
if (PreviousPage == null)
{
Response.Redirect("Default.aspx");
}
Leave a Comment
Leave a Comment
Leave a Comment