Groups | Blog | Home
all groups > asp.net webcontrols > april 2006 >

asp.net webcontrols : no FormView for create/insert page


David Thielen
4/11/2006 7:17:02 PM
Hi;

As an experiment for one page I created just a bunch of controls on the page
- no FormView, etc. I use this page for both create & edit. This way I only
have every control once (no Item/EditItem) and all controls are accessable as
objects in the code behind.

It seems to work pretty nice. But the ASP API is definitely not designed
with this in mind. For example, there is no way to tie an ObjectDataSource to
the controls.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
stcheng NO[at]SPAM online.microsoft.com
4/12/2006 12:00:00 AM
Hi Dave,

As for the ASP.NET webcontrols, they all support simple databinding without
using those template databinding control (such as FormView, GridView...).
So for your scenario, you can just declare a page variable(non-private) in
the page's code behind class which is of the dataobject type(contains the
data propertes you want to bound them to the controls on the page). Then,
in the page's aspx template, we can just databinding expression to bind the
data properties on the data object onto the controls in the page. e.g:

==========aspx=============
............................
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"></asp:SqlDataSource>

</div>
<asp:Label ID="Label1" runat="server"
Text="CategoryID:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
category.CategoryID %>'></asp:TextBox><br />
<asp:Label ID="Label2" runat="server"
Text="CategoryName:"></asp:Label><asp:TextBox
ID="TextBox2" runat="server" Text='<%# category.CategoryName
%>'></asp:TextBox><br />
<asp:Label ID="Label3" runat="server"
Text="Description:"></asp:Label><asp:TextBox
ID="TextBox3" runat="server" Text='<%# category.Description
%>'></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
===================

==========code behind========
public partial class _Default : System.Web.UI.Page
{
protected CategoryDS.CategoriesDataTable dt;
protected CategoryDS.CategoriesRow category;


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CategoryDSTableAdapters.CategoriesTableAdapter ta = new
CategoryDSTableAdapters.CategoriesTableAdapter();

dt = ta.GetData();
category = dt.Rows[0] as CategoryDS.CategoriesRow;

Page.DataBind();
}
}
}
========================

# I call Page.DataBind so as to make all the databinding expression in the
page get evaluated (after I've populated the page member variable which
hold the dataobject for databinding).

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
David Thielen
4/12/2006 6:14:02 AM
This is great - thanks.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



[quoted text, click to view]
stcheng NO[at]SPAM online.microsoft.com
4/13/2006 1:51:26 AM
You're welcome Dave,

Good luck!

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
AddThis Social Bookmark Button