看起來你會一直擁有相同的連接字符串,如果是這樣的話,只需轉到用戶控件(.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" />
您好, 謝謝你的回覆。 這在我的情況下不起作用,AS在我的原始文章中聲明,用戶控件(ascx.vb)包含獲取數據的所有必要控件,父頁面(aspx.vb)包含gridview。所以SQLDatasource和Gridview位於2個不同的頁面上。 SQLDatasource位於ascx.vb中 Gridview位於aspx.vb 同樣的問題適用於原始文章。 – Hav0c
我想你總是可以使用會話變量... – ablaze