|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis code drop is part of a smash and grab series. If you're in a rush, you can just grab this code and insert it into your application, no understanding is required. When you have some time (ya, right) you may want to review the source code. BackgroundI was presented with a project to convert an Access single-user application into a web based multi-user SQL Server application. The Access application had about 10 tables that needed to be managed through an Add/Edit/Delete interface. This table maintenance was to be done by a single individual, so concurrency issues would not be a problem. I decided that with some tweaking, the This article will show you how to code inline detail records just below a selected master record - just like Access does. For this article, I have used tables in the Northwind database. Using the codeDownload the project, unzip it to somewhere on your hard disk, and then create a new web project from an existing folder. Build the project, and test drive it so that you can see what it does. There are a few key files:
Please download the source files and follow along as I explain what you need to insert. Master.aspxInsert this code in the <script src="GridViewHelper.js" type="text/javascript"></script>
<link href="GridViewHelper.css" rel="STYLESHEET" type="text/css" />
<link href="AppStyle.css" rel="STYLESHEET" type="text/css" />
Insert this code following the <script type="text/javascript">
GridViewHelper.Init(document.all.GridView1, 0, 0);
var ToolTips = new Array("", "??", "??", "??");
GridViewHelper.AddToolTips(document.all.GridView1, ToolTips);
</script>
Master.aspx.csMost of the code in here is just boiler plate, just cut and paste. Two lines of code are kind of interesting: string RowID = Convert.ToString(
System.Web.UI.DataBinder.Eval(e.Row.DataItem, "CategoryID"));
string Url = "Details.aspx?ID=" + RowID;
When you call Details.aspx, you must tell it which category the details are for. This is how you do it. Details.aspxNotice that the <body leftmargin="40" topmargin="0" scroll="no">
Insert this code in the <script src="GridViewHelper.js" type="text/javascript"></script>
<link href="GridViewHelper.css" rel="STYLESHEET" type="text/css" />
<link href="AppStyle.css" rel="STYLESHEET" type="text/css" />
Notice that the <form id="form1" runat="server" style="overflow: hidden;">
Notice that the <div style="overflow: hidden;">
And now the magic. Insert this code following the <script type="text/javascript">
GridViewHelper.Init(document.all.GridView1,
ExtraWidth=100, ExtraHeight=0);
var ToolTips = new Array("", "Product name",
"Supplier", "Quantity per unit",
"Unit price", "Units on order",
"Reorder level", "Discontinued");
GridViewHelper.AddToolTips(document.all.GridView1, ToolTips);
</script>
This causes the Take a look at the SelectCommand="SELECT '0' as [ProductID], '' as [ProductName],
'' as [CompanyName], '0' as SupplierID, '0' as [QuantityPerUnit],
'0.00' as [UnitPrice], '0' as [UnitsInStock], '0' as [UnitsOnOrder],
'0' as [ReorderLevel], '0' as [Discontinued]
UNION SELECT [ProductID], [ProductName], Suppliers.CompanyName,
Suppliers.SupplierID, [QuantityPerUnit],
convert(varchar(20), [UnitPrice], 0) as [UnitPrice],
[UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]
FROM [Products]
left join Suppliers on Suppliers.Supplierid=Products.SupplierID
WHERE ([CategoryID] = @CategoryID)
ORDER BY [ProductName]
Notice that the Take a look at the InsertCommand="INSERT INTO [Products] ([ProductName],
[SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice],
[UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued])
VALUES (@ProductName, @SupplierID, @CategoryID,
@QuantityPerUnit, convert(money,@UnitPrice),
@UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued)
I had to convert the field back to money so the insertion would not fail with a "conversion from varchar to money error". Details.aspx.csMost of the code in here is just boiler plate, just cut and paste. There is really only one trick in here and that has to do with inserting new records: p = new System.Data.SqlClient.SqlParameter(
"@CategoryID", Request.QueryString["ID"]);
c.Parameters.Add(p);
The insert requires a Points of interestAn interesting side effect of this technique is that detail records can have their own detail records ad infinitum. When you expand a detail block the first time, a request to the server is made to populate it. If you collapse the details block, the block is just hidden, so that the next time you request for expansion, there is no server hit, the block is just made visible again. Notice: No AJAX was maimed or killed in the creation of this article. In fact, the newest kid on the block wasn't even required. I've got nothing against AJAX (in fact some of my friends use AJAX), but it's got its time and place (probably in my next project ;-). Smash and Grab / Redux seriesI have recently started two series of articles here at CodeProject. Smash and Grab is intended as a series of short articles on one specific code technique. Redux is intended as a series of longer articles which attempt to reduce a complicated topic (like ConclusionsPretty easy, eh! (I'm Canadian). If you have the time, take a look at the source, there is some cool DHTML going on.
|
||||||||||||||||||||||