0

I am creating a website in asp.net. My website has an admin page. The admin will use this page to daily update website's content. This content will get reflected to the main website. I have learned from the following link that how we can pass values from one page to another- How to pass values across the pages in ASP.net without using Session

I am using Application variable.

Admin.aspx

    <form runat="server">
    <div>  
        <asp:TextBox id="DailyMsgID" name = "DailyMsgName" runat="server"></asp:TextBox>
        <asp:Button id="b1" Text="Submit" runat="server" OnClick="b1_Click" />
        <asp:Label ID="Label_DailyMsgId" name="Label_DailyMsgName" runat="server" Text="Label"></asp:Label> 
    </div>
</form>
</body>

Admin.aspx.cs

protected void b1_Click(object sender, EventArgs e)
        {
            Label_DailyMsgId.Text = DailyMsgID.Text;
            Application["DailyMessage"] = Label_DailyMsgId.Text;
                
        }

Home.aspx

<!-- Page content-->
                <div id="div1" class="container-fluid">
                    <h1 id="myhead" class="mt-4">Welcome to the Official Website of ABC</h1>
                    <p id="DailyMessage"></p>
                    
                </div>

To set the paragraph, I want to do something like below. But it is not recognising the paragraph Id.

Home.aspx.cs

  protected void Page_Load(object sender, EventArgs e)
        {
            DailyMessage.Text = Application["DailyMessage"].ToString();
        }

How do I set the paragraph?

Both Admin and Home page are under the same solution.

Anonymous
  • 113
  • 1
  • 2
  • 13

1 Answers1

0

This issue got resolved. I was missing

runat="server"

Here is the updated code -

Home.aspx

                    <p id="DailyMessage" runat="server"></p>
                   

Home.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
        {
            DailyMessage.InnerText = Application["DailyMessage"].ToString();
        }
Anonymous
  • 113
  • 1
  • 2
  • 13
  • 1
    I would still use the database. Create a table with one record. You can have several columns like welcome message, maybe even a for more information columns that you want for some of the pages. You could even say add a some rows with say a date range _Christmas season message. App vars are lost for re-boot, re-start the web site, or even other issues will cause a re-start. Values in application vars do not NOT persist well. Application vars can be used for temp vars - and vars that don't have to really persist for long periods of time. I STRONG suggest you place this data into a database. – Albert D. Kallal Jan 02 '22 at 17:38