看起来您将一直拥有相同的连接字符串,如果是这种情况,只需转到用户控件 (.ascx.cs) 后面的代码并尝试以下操作,将 SqlDataSource 绑定到 asp gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
}
private void LoadData()
{
//ConnectionStringSettingsCollection ConnctionStringCollection = ConfigurationManager.ConnectionStrings;
string ConnectionString = <your connection string>;
string SelectQuery = string.Empty;
//if (!HasStoredProcedure)
// SelectQuery = QueryOrStoredProcedure;
DashGridDataSource.ConnectionString = ConnectionString;
DashGridDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
DashGridDataSource.SelectCommand = SelectQuery;
DashGridDataSource.EnableCaching = true;
DashGrid.DataSourceID = DashGridDataSource.ID;
}
}
我的 .ascx 文件如下所示:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DashGridWidget.ascx.cs" Inherits="*****.Widgets.DashGridDefault" %>
<asp:SqlDataSource ID="DashGridDataSource" runat="server" EnableCaching="true" CacheDuration="300"></asp:SqlDataSource>
<asp:GridView ID="DashGrid" runat="server"
AllowPaging="True" AllowSorting="True" onsorting="DashGrid_Sorting">
您还可以在 .ascx.cs 的开头向您的自定义控件添加自定义属性:
public partial class DashGridDefault : System.Web.UI.UserControl
{
public string QueryOrStoredProcedure { get; set; }
public bool HasStoredProcedure { get; set; }
public int DatabaseEnum { get; set; }
这就是我使用控件的方式:
<Dashboard:IconGrid runat="server" ID="proceedings" DatabaseEnum="3" HasStoredProcedure="false" SessionVariableID="Proceeding" QueryOrStoredProcedure="SELECT TOP 100 CONTACTS FROM CUSTOMERS" />