Idea gathering, suggestions welcome

G

Guest

Hi all,

I'm running an asp.net website that allows its users to log events, say
social gathewrings (with event date, time, address, descrption, etc) and
allows people to post comments on each posting. I use a datagrid with 2 cells
to display all recent events chronologically and use nested databinding to
display all related comments underneeth the events. Works great.

Now, I'm trying to make it so that I can display the most recent event on
the homepage. I ended up trying to replciate the appearence of one instance
of a datagriditem by having a table with a bunch of labels that I set
individually. I want to reuse this logic now to also show the most recent
event posted by the logged in user. So, I'm thinking user control or server
control. I'm tempted to go for a server control just to gain experience
making one. If I do go for a server control, would I encorperate databinding
into it? Does that make sense when you're only pulling data (multiple fields)
from a single datarow? What's the alternative? Just manually setting label
values in a load event, right?

Any thoughts at all would be very welcome. Thanks...

-Ben
 
S

Steve C. Orr [MVP, MCSD]

User controls are simpler, although not very reusable across projects.
If the control is just for this one project and you don't forsee reusing it
in other projects then I'd suggest going with a user control. Otherwise the
additional complexity of a server control may be worthwhile.
Here's more info:
http://SteveOrr.net/faq/UserCustom.aspx
 
G

Guest

Hi Steve,

What about the databinding component? If I go for a server control, does my
scenario sound like a cantidate for databinding even without iteration? What
about in a user control?

-Ben
 
G

Guest

I guess a better way to phrase the question would be:

Could you suggest what kinds of controls I should use to display the data
and a method to populate them? Would your answer differ if it were a user or
server control?

-Ben
 
S

Steven Cheng[MSFT]

Hi Ben,

Regarding on the ASP.NET application, are you developing upon ASP.NET 1.X
or 2.0? Based on my understanding, ASP.NET 2.0 has provided some good
databound controls for displaying single datarow, like FormView or
DetailsView, you can use them to bound with a DataSource(DataTable ,
collection) with single item... Also, other template databound control
like repeater or datalist can also be used to bind with single row
datasource :).

And as for using UserControl, I think it a good approach, you can add those
control which you want to bound with data field into the usercontrol(ascx
template) and then apply databinding expression on their properties. At
runtime, you can just populate a certain page level property and call the
page or usercontrol's DataBind method to evaluate those databinding
expressions. e.g:

==========aspx page ===
<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"
OnClick="Button1_Click" />
</form>


========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();
}
}
========================


In the above code I just use a page variable as datasource and put those
controls at page level, you can encapsulate them into UserControl and just
call usercontrol.DataBind, that can make the databinding processing only
occur at the usercontrol scope.

Hope this also 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.
 
G

Guest

Hi Steven,

Your insights are very helpful. Indeed this is on .NET 2.0 so I do have
those options. I didn't actually consider using a repeater bound to only a
single row. Is that considered an ok programming practice?

I'm not sure I follow your comments about a user control. You seem to
suggest a usercontrol as an ascx template. Do you mean that I should use a
templated control and pull a template from an ascx file? Also:
At runtime, you can just populate a certain page level property and call the
page or usercontrol's DataBind method to evaluate those databinding
expressions.

Which "pagelevel property" in the example are you takling about?

Thanks...

-Ben

Steven Cheng said:
Hi Ben,

Regarding on the ASP.NET application, are you developing upon ASP.NET 1.X
or 2.0? Based on my understanding, ASP.NET 2.0 has provided some good
databound controls for displaying single datarow, like FormView or
DetailsView, you can use them to bound with a DataSource(DataTable ,
collection) with single item... Also, other template databound control
like repeater or datalist can also be used to bind with single row
datasource :).

And as for using UserControl, I think it a good approach, you can add those
control which you want to bound with data field into the usercontrol(ascx
template) and then apply databinding expression on their properties. At
runtime, you can just populate a certain page level property and call the
page or usercontrol's DataBind method to evaluate those databinding
expressions. e.g:

==========aspx page ===
<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"
OnClick="Button1_Click" />
</form>


========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();
}
}
========================


In the above code I just use a page variable as datasource and put those
controls at page level, you can encapsulate them into UserControl and just
call usercontrol.DataBind, that can make the databinding processing only
occur at the usercontrol scope.

Hope this also 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.
 
S

Steven Cheng[MSFT]

Thanks for your response Ben,

============================
I'm not sure I follow your comments about a user control.
=============================

I mean you can just put some simple control markup with databinding
expression(like the one I posted in former message) in a usercontrol and
use that usercontrol in your page. Also, you can directly put such markup
in main page as I did.

And in my markup snippet, you can see that I use databinding expression
(<%# .....%) to bind control properties with databound objects. e.g:

================
asp:TextBox ID="TextBox1" runat="server" Text='<%# category.CategoryID
%>'></asp:TextBox><br />
==============

And the "category" object is just a Page level variable which I defined in
page's codebehind class to act as the DataSource object for databinding.

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

In addition, as for use singleRow DataSource to bind with Repeater or other
List DataBound control, I think it ok and there is not violation against
any best practice.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top