Question

I'm trying to populate the site menu on the site master page, according to the current user's role. For some reason, the C# code behind file does not recognize the SqlDataSource nor some other hidden fields that I put on the mater page's aspx code. here's my code:

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# code behind:

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 

Thanks all

Was it helpful?

Solution 2

I think you need to define the MasterType so that the child page knows what class the Master Page is. This will make the Master Page strongly typed.

See MSDN documentation.

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

Also, I'm not sure if controls declared on the ASPX page are by default public. So you may need to add them as a property on the Master Page so that they're publicly accessible from other classes.

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

OTHER TIPS

SqlDataSource1.DataBind();

should be

getButtons.DataBind();

Make sure you have:

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

in your

       Site.Master
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top