Hello again,
I've been trying to get this to not work as you describe it but I can't. The only problem I can find with the Try It Out is getting the control to connect to the database correctly. Hence my query about the connection string.
Out of curiosity, try creating a new data connection for the sqldatasource you create in step 3 using the smart control panel that points to the wroxunited.mdf database. Select any fields from any table you like, test it and close. Then switch to source view and delete the selectcommand=" ... " section from the <asp:SqlDataSource> control tag and continue the example, ignoring step 6. It should work after that.
For reference, here is the working code I have for NewsUserControl.ascx.cs.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class NewsUserControl : System.Web.UI.UserControl
{
private int _itemsToShow = 5;
public int ItemsToShow
{
get { return _itemsToShow; }
set { _itemsToShow = value; }
}
private void Page_PreRender(object sender, System.EventArgs e)
{
string sel = string.Format("select top {0} * FROM [News] WHERE DateToShow <= '{1}' ORDER BY DateToShow DESC",
_itemsToShow, DateTime.Now.ToString("yyyy/MM/dd"));
SqlDataSource1.SelectCommand = sel;
}
}
The Page_PreRender should NOT be connected to the OnPreRender event of the Repeater control - it will be automatically attached to the Page object itself.
|