簡體   English   中英

ASP.NET在母版頁上使用SqlDatasource

[英]ASP.NET Use SqlDatasource on master page

我正在嘗試根據當前用戶的角色填充網站主頁上的網站菜單。 由於某些原因,文件后面的C#代碼無法識別SqlDataSource或我放在母版頁的aspx代碼上的其他一些隱藏字段。 這是我的代碼:

ASP:
<form id="Form1" runat="server">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
  <asp:SqlDataSource ID="getButtons" runat="server" ConnectionString="<%$ ConnectionStrings:RMSConnectionString %>" SelectCommand="sp_getSiteMasterButtons" SelectCommandType="StoredProcedure">
    <SelectParameters>
      <asp:ControlParameter ControlID="idsid" Name="idsid" PropertyName="Value" Type="String" />
    </SelectParameters>
  </asp:SqlDataSource>

  <asp:HiddenField ID="idsid" runat="server" />

</asp:ContentPlaceHolder>
</form>

后面的C#代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        idsid.Value = Environment.UserName;
        getButtons.DataBind();

    }
}

Error   1449    The name 'idsid' does not exist in the current context
Error   1450    The name 'getButtons' does not exist in the current context 

謝謝大家

SqlDataSource1.DataBind();

應該

getButtons.DataBind();

確保您具有:

 <%@ Master Language="C#" AutoEventWireup="true" 
         Inherits="SiteMaster" CodeFile="Site.Master.cs" %>

在你的

       Site.Master

我認為您需要定義MasterType,以便子頁面知道Master Page是什么類。 這將使母版頁具有較強的鍵入性。

請參閱MSDN文檔

<%@ MasterType virtualpath="~/Masters/Master1.master" %>

另外,我不確定在ASPX頁上聲明的控件是否默認為公共控件。 因此,您可能需要將它們作為屬性添加到母版頁上,以便可以從其他類中公開訪問它們。

public SqlDataSource MasterDataSource {get {return this.getButtons;}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM