Archive for July, 2006
Error : A potentially dangerous Request.Form value was detected from the client
This occurs when we try to post html code. The server will validate the postdata, if it find that its an HTML code, error something similar to this is shown. This actually helps to prevent running malicious script. If you really want to post the html content add this to the page directive validateRequest=”false”.
You can also do this from the web.config like this
<pages validateRequest=”false” />
10 comments July 26, 2006
Nested Gridview to show Master/Details relationship in ASP.NET 2.0
This will help you to nest grdiviews inside ASP.NET. There may be many ways to do this, but this is my way( May not be the best, let me know if you have any other
).
1. First drop a gridview in to the page, also set its DataKeyNames to your primary key
2. Add a new template column to this gridview.
3. Place another gridview inside this template column.
the code for the termplate column may look like this
<asp:TemplateField HeaderText=”Heading”>
<EditItemTemplate>
<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:GridView ID=”GridView1″ runat=”server” DataSource=’<%# GetTrasnl(Convert.ToInt32(Eval(“pId”))) %>’ AutoGenerateColumns=”False” CellPadding=”4″ ForeColor=”Black” GridLines=”Vertical” BackColor=”White” BorderColor=”#DEDFDE” BorderStyle=”None” BorderWidth=”1px”>
<FooterStyle BackColor=”#CCCC99″ />
<Columns>
<asp:BoundField DataField=”language” HeaderText=”Language” />
<asp:BoundField DataField=”uName” HeaderText=”Translator” />
</Columns>
<RowStyle BackColor=”#F7F7DE” />
<SelectedRowStyle BackColor=”#CE5D5A” Font-Bold=”True” ForeColor=”White” />
<PagerStyle BackColor=”#F7F7DE” ForeColor=”Black” HorizontalAlign=”Right” />
<HeaderStyle BackColor=”#6B696B” Font-Bold=”True” ForeColor=”White” />
<AlternatingRowStyle BackColor=”White” />
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
This child gridview has to show the content based on the primary of the row thats binded to main gridview.
Here in my case “pId” is the primary key. The nesting happens here
<asp:GridView ID=”GridView1″ runat=”server” DataSource=’<%#GetTrasnl(Convert.ToInt32(Eval(“pId”))) %>’
what I have done is that, I have called function “GetTrasnl” and passed the primary key to that function. This function will return a datatable. So each time a row is bound to the main gridview, the child gridview is also bound with the corresponding values.
The functions is like this
Public Function GetTrasnl(ByVal prgId As Integer) As DataTable
………………
……………….
End Function
Please post your comments if you have any
22 comments July 26, 2006
Page post back after JavaScript code executed on server control click
I created a ASP.NET page with two text boxes and a button. I want this button to reset the form(Only to reset the page). There are two ways you can do this, one is going to the click event of the button and add the code to clear the text boxes. Like the one given below
C#
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
but this technique is very heavy, bcz it need another round trip to the server.
The other way is using the JavaScript. You need to add an attribute to the button in the page load event. Like this
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add(“onclick”, “JavaScript:document.forms[0].reset(); return false;“);
}
what this code does is that, The button is rendered like this
<input type=”submit” name=”Button1″ value=”Button” onclick=”JavaScript:document.forms[0].reset(); return false;” id=”Button1″ />
so that the page wiill be automatically reset when the user clicks the button (Completely handled by the browser, no round trip to the server).
The code that I bolded (return false;), its very important, other wise the page will be posted back.
11 comments July 21, 2006
Maintaining the Scroll Position after post back
Some of us may come across this situation, ie maintaining the scroll position after the page is post back. This is very useful when we have a gridview or data repeater or some thing like that, which shows a large amount of data and a button controls is place below page, so that user has to scroll a bit to reach there. After clicking on the button, the page is posted back, but the page is scrolled to top.
If you want to maintain the scroll position, add this attribute on the @page directive
<%@ Page Language=”VB” MaintainScrollPositionOnPostback=”true”
Hope this will help you
9 comments July 18, 2006
Sending Emails from ASP.NET 2.0
Sending Emails from ASP.NET is very easy. In .NET 1.1 and 1.0, the class was System.Web.Mail but in .NET 2.0, they changed to System.Net.Mail.
The code for sending an email is given below
VB.NET
Dim email As MailMessage
Dim mailClient As SmtpClient
email = New MailMessage(fromAdd, toAdd, subject, message)
‘if you want to send HTML message set this property to true
email.IsBodyHtml = True
mailClient = New SmtpClient(“Your mail server name”) ‘like mail.xxx.com
‘ if your server is using different port then set this
mailClient.Port = [PortNumber]
mailClient.Send(email)
Some times your outgoing SMTP server may need username and password. Then you need to modify the code little bit.
First create an object of NetworkCredential (This is available in the System.Net Namespace) with the username and password
Dim authInfo As New NetworkCredential(smtpUsername, smtpPassword)
Then set the UseDefaultCredentials property of the smtp client object to false and set the Credentials property of the smtp client object to authInfo. Then send the mail.
mailClient.UseDefaultCredentials = False
mailClient.Credentials = authInfo
1 comment July 18, 2006
Debugger Fails to Hit the Breakpoint When You Debug an ASP.NET Web Application
Today morning I was trying to debugg an WebApp, this application was running without any problem in laptop. But when I copied this to my PC and tried to give a break point, I was not able to bebug application. After googling I found many solutions. They are
1) This is from Microsoft
To resolve this issue, you need to force a reload of the pertinent .dll file into aspnet_wp.exe (or w3wp.exe for applications run on IIS 6.0). To do this, recompile the project before you debug again. However, the project cannot be recompiled unless you make changes to the code. Therefore, restart Microsoft Visual Studio .NET, and then click Rebuild All. (Alternatively, you can run the iisreset command from the command prompt.) Now, when you run the debugger again, it hits the breakpoint.
2) Second one is from a forum(I don’t remember the link).
If your application is having same file names(in my case Default.aspx, one inside the root folder and other inside the admin folder), this will issue will occur. So rename any one of these file and rebuild.
Actually I tried the first solution but no hope.
The second solution worked for me. Sometimes the first solution works for you. Try both.
These are bugs in the VS.NET. So don’t think this as your fault.
1 comment July 15, 2006
How to check whether a file exist or not
There is method which makes it easy to check whether a file exist or not,
Code is given below
C#
if(System.IO.File.Exists(@”c:\test.xml”))
{
//True statements comes here
}
This code check whether a file named “test.xml” exist in the C:\ drive.
3 comments July 13, 2006
An open-source ASP.NET menu server control
skmMenu is a simple and free ASP.NET menu server control. It uses XML file as back end. Really cool control.
Visit skmMenu website
2 comments July 11, 2006
ASP.NET: Connection Strings
In ASP.NET there are several options for storing connection strings. These include the Web.config file, Application variables, custom text files, and hardcoded into pages or components. In Classic ASP, it was common practice to use the Application object to store the connection string information for an application, so it was common to find code like this in the global.asa file:
Sub Application_OnStart
Application("Conn1_ConnectionString") = _
"Provider=SQLOLEDB.1;UID=sa;PWD=p;Initial
Catalog=src;Data Source=localhost;"
End Sub
This was the “best practice” approach for many websites. Another option that some sites, such as ASPAlliance.com, used was to have these settings in a common text file that was then called with Server.Execute, such as this:
Sub Application_OnStart Server.Execute "/db/config.asp" End Sub
This would allow many different applications (like all of the individual columnists on ASPAlliance.com) to have their connection information stored in one place. ASPAlliance also used a COM object’s property for this same purpose for a while. The question is, what’s the best way to store this information in ASP.NET?Well, there is no single right answer for this question, but depending on your situation, there is usually a method that is best. Before we consider the different possibilities, let’s consider one option that should probably be eliminated right away: Application variables.
1 comment July 11, 2006