Databind a Label (or Literal), proper way?

B

Brian Simmons

I'll try to keep this short & sweet and hopefully get the direction I'm
looking for.

I'll drop a SqlDatasource onto a webform. Configure it to use a stored
proc, perhaps have the parameters be QueryString parms or Session variables,
whatever the situation calls for.

The Stored Proc will return one row of data, let's say FirstName & LastName,
John Smith.

I drop a Label (or Literal) onto the webform.

I would like the Label's Text property to be set to FirstName.

What/where is the proper way to hook this up?

In the past, I've had a Page_Load event and if not Postback, I setup a
datareader and read the values from the datareader and populate the
label/literal/textbox controls.

But I was thinking there must be a way to utilize the SqlDatasource object
and do the same thing (thus saving having to write all that SP calling code
in the Page_Load).

Thanks,
Brian
 
M

Manish

Hi Brian,

I think you can try the following code to bind your Label control to a
SqlDataSource.

Dim dv As DataView =
CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
Dim dr As DataRowView = dv.Item(1)
Me.Label1.Text = dr.Item(0)

Regards,
Manish
www.ComponentOne.com
 
S

Steven Cheng

Hi Brian,

As for the SqlDataSource control, it does be a good component for supply
databinding value on ASP.NET 2.0 pages, however, it by default only work
with those databound controls(such as GridView, DataList,
DropDownList....). Label is not a databound control, therefore, it only
support simple databound and can not work directly with SqlDataSource
control.

As Manish has suggested, you can use code to programmatically get the data
result from SqlDataSource control and assign the certain datavalue to your
label control. Another possible approach is putting the Label or Literal
control(that need to bind to value from SqlDatasource) into an additional
databound control(e.g. a FormView control) so that you can use databinding
expression on the Label(within the FormView) and the FormView will connect
to the SqlDataSource. How do you think?

If there is any other particular concerns in your scenario, welcome to post
here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top