Wednesday, July 26, 2006

 

Asp.NET : 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” />


Tuesday, July 25, 2006

 

Asp.NET : 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


Friday, July 21, 2006

 

Asp.NET : 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.


Tuesday, July 18, 2006

 

Asp.NET : 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


Saturday, July 15, 2006

 

Asp.NET : 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.


Thursday, July 13, 2006

 

Asp.NET : 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.


Tuesday, July 11, 2006

 

Asp.NET : 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


This page is powered by Blogger. Isn't yours?