DataBind CSLADataSource Textbox in Webform

DataBind CSLADataSource Textbox in Webform

Old forum URL: forums.lhotka.net/forums/t/4255.aspx


jstevens posted on Tuesday, January 29, 2008

Can anyone tell me how to databind a textbox in an aspx webform without using controls such as detailsview or formview with a CSLADataSource?

Thanks!

RockfordLhotka replied on Wednesday, January 30, 2008

I don't think that is possible. My understanding of the ASP.NET 2.0 data binding model is that datasource controls provide data only to the gridview/detailsview/formview controls. The CslaDataSource control is just like any other datasource control.

jstevens replied on Wednesday, January 30, 2008

thanx Rocky - to further elaborate, I was wondering if we could do something similar to this example with the CSLADataSource:

ASPX 

First Product:<br />
<asp:textbox id="txtProductName" runat="server" />
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
	selectcommand="SELECT TOP (1) * FROM [Products]">
</asp:sqldatasource>

CODE-BEHIND 

protected void Page_Load(object sender, EventArgs e)
{
	if (!this.IsPostBack)
	{
		this.GetData();
	}
}

private void GetData()
{
	DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
	DataRowView drv = dv[0];

	txtProductName.Text = drv["ProductName"].ToString();
}
 

RockfordLhotka replied on Wednesday, January 30, 2008

That example doesn’t use data binding – it just uses the datasource control as a way of executing the query, and then manually copies the value(s) into the UI control(s).

 

Why wouldn’t you take the simpler route (with CSLA anyway) and do this in the code behind:

 

protected void Page_Load(…)
{

  if (!this.IsPostBack)

  {

    GetData();

  }

}

 

private void GetData()

{

  Product product = Product.GetProduct();

  txtProductName.Text = product.Name;

}

 

This avoids all the reflection overhead of interacting with a datasource control and gets the same result.

 

Rocky

 

 

From: jstevens [mailto:cslanet@lhotka.net]
Sent: Wednesday, January 30, 2008 10:29 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] DataBind CSLADataSource Textbox in Webform

 

thanx Rocky - to further elaborate, I was wondering if we could do something similar to this example with the CSLADataSource:

ASPX 

First Product:<br />
<asp:textbox id="txtProductName" runat="server" />
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
        selectcommand="SELECT TOP (1) * FROM [Products]">
</asp:sqldatasource>

CODE-BEHIND 

protected void Page_Load(object sender, EventArgs e)
{
        if (!this.IsPostBack)
        {
               this.GetData();
        }
}
 
private void GetData()
{
        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        DataRowView drv = dv[0];
 
        txtProductName.Text = drv["ProductName"].ToString();
}
 



jstevens replied on Wednesday, January 30, 2008

Rocky you are right...  Been programming for 16 straight hours and didn't think of that approach at all.  That's why you get paid the big bucks!

By the way - excellent work on the new framework 3.0.3.  Very stable and I have been using WCF for all my new projects.  It has been a learing curve to understand CSLA over the last several months, but now utilizing code generators, I can just fly on my new projects.  Just wanted to say thanks for all your hard work!

Julian

Copyright (c) Marimer LLC