开发者

How professional ASP.NET Web Form developers code database view pages?

开发者 https://www.devze.com 2023-03-24 13:41 出处:网络
Just wondering what method professionals choose for a page for viewing data from a database table in ASP.NET Web Forms, for example a single product details page on an ecommerce website?

Just wondering what method professionals choose for a page for viewing data from a database table in ASP.NET Web Forms, for example a single product details page on an ecommerce website?

Some of the methods I consider are:

  1. Data bound DetailsView;
  2. Data bound FormView;
  3. Lots of label controls assigned values in the code-behind;
  4. Lots of Response.Writes on the markup page (eg. <%= object.field %>); or,
  5. Any other methods???

  6. The code excerpt below is the method I am using at present. Is it bad practice?

Markup...

<asp:Panel ID="panOrder" runat="server">
&l开发者_如何转开发t;table>
    <tr>
        <td>Order Number</td>
        <td><asp:Label ID="lblOrderId" runat="server" Text='<%# Order.orderid %>' /></td>
    </tr>
   <tr>
        <td>Order Date</td>
        <td><asp:Label ID="lblCreationDate" runat="server" Text='<%# Order.creationdate.ToString("dd/MM/yyyy") %>' /></td>
   </tr>
</table>

Code behind...

protected order Order { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
    int orderId = Request.QueryString["orderid"] == null ? 0 : Utilities.IntParse(Request.QueryString["orderid"]);
    Order = order.SelectByOrderId(orderId);
    panOrder.DataBind();
}


If you want to show the product details on the page, you should use the Repeater Control instead. This way you will have more control of the layout of the page. In addition this control is light weight and will provide you with good performance.


It all depends on what you need in specific situation:

  1. use DetailsView if you need simple operations on your data (basic select, update, insert) and your data will be presented in single view in a list (top to bottom)

  2. the same as for DetailsView but keep in mind that with FormView you are more free of how to present your data out of the box (DetailsView need some tweaks to behave the same). You won't be limited to top to bottom presentation, you can freely choose where on your page some info is presented.

  3. when you have only 2 to 3 databound elements maybe, but when you have more fields I would rather go for one of the above methods. It is easier.

  4. this method in webforms I personally use very rarely. Maybe if you have to redesign some old ASP (any non webforms) page where is a lot of HTML to handle, and is already structured nicely?

  5. using other Data controls too, like Gridview when presenting tabular data for example. Repeater is also another nice control like Muhammad mentioned (I agree it is the most customizable).

Sorry for so long answer. I was wondering the same question couple of years back too, so I thought it is in place to share this with you. The above helped me a lot when identifying methods to show this or that in webforms.

To answer your given example: Single products details page will be easiest to achieve with DetailsView or FormView. I would go for the second one.

0

精彩评论

暂无评论...
验证码 换一张
取 消