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

Advertisements

July 26, 2006 at 5:02 am 32 comments

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.

July 21, 2006 at 7:50 am 12 comments

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

July 18, 2006 at 12:09 pm 9 comments

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

July 18, 2006 at 5:54 am 1 comment

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.

July 15, 2006 at 5:56 am 1 comment

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.

July 13, 2006 at 8:00 am 3 comments

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

July 11, 2006 at 6:36 am 3 comments

Older Posts Newer Posts


Top Clicks

  • None

Calendar

November 2017
M T W T F S S
« Jun    
 12345
6789101112
13141516171819
20212223242526
27282930  

Recent Posts

Feeds

Blog Stats

  • 93,195 hits