0

QUESTIONS

  1. How to set master page object id (e.g div id) in code behind (VB)?
  2. How to change the id in child pages?

Root.Master

<%@ Master Language="VB" CodeFile="~/Root.master.vb" Inherits="Root"%>

<div id="<%=MyPage%>">
  <asp:ContentPlaceHolder ID="Content" RunAt="Server"/>
</div>

Root.Master.vb

Partial Class Root
    Inherits BaseMaster
End Class

Page.vb (In App_Code Folder)

Public Class BaseMaster
    Inherits System.Web.UI.MasterPage    
    Public MyPage As String    
End Class

Index.aspx.vb

Partial Class Index
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CType(Me.Master, BaseMaster).MyPage = "Page"
    End Sub
End Class
DreamTeK
  • 32,537
  • 27
  • 112
  • 171

1 Answers1

1

I think you cannot change the ID of an element, when it is set as runat="server".

In order to have more than one IDs for the body tag you must have one master page for every different ID you want, and choose the master page via code.

If you want to change its ID for CSS purposes, consider using classes instead.

Edit: If you don't need the runat="server" attribute in the body tag, you could something like this:

The basic idea is to have a variable for the dynamic id in your master page and be able to set its value from your .aspx pages and .ascx controls.

The variable BodyID is declared in a class that inherits the MasterPage class and your master page inherits this class instead.

Master Page Markup

<body id="<%=BodyID%>">
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>

Maste page code behind

Public Class Site1
    Inherits BaseMaster

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class

The BaseMaster Class

Public Class BaseMaster
    Inherits System.Web.UI.MasterPage

    Public BodyID As String

End Class

How to use it in a Web Form

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    CType(Me.Master, BaseMaster).BodyID = "body_dynamic_id"
End Sub
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
  • Hello, I just edited my answer, if you don't need the runat="server" tag I gave an example on how to set the id dynamically. I hope it helps – Tasos K. Oct 18 '13 at 09:33
  • You need to set it once in your project. Add an empty .vb file to your project and just paste the code there. – Tasos K. Oct 18 '13 at 10:07
  • Genuinely appreciate your help on this @codingstill however I cannot get it to work. Please see my edits above. What am I missing? Error is:Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl). – DreamTeK Oct 18 '13 at 10:33
  • It seems that there is a namespace issue. Are you seeing this error in the code behind of the Master Page? If yes, try this "Inherits RootNamespace.BaseMaster" where RootNamespace put the root namespace of your application – Tasos K. Oct 18 '13 at 10:49
  • I made a demo website and it worked with no errors. Did you put the Page.vb file inside the App_Code folder? – Tasos K. Oct 18 '13 at 11:59
  • So, is everything ok now? Do you have any other issues? – Tasos K. Oct 18 '13 at 12:30
  • After much fiddling got it working. Many thanks for your time on this. – DreamTeK Oct 18 '13 at 13:17