Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:27:38+00:00 2026-06-03T20:27:38+00:00

This is my code : private string[] MesiSelezionati; protected void Page_Load(object sender, EventArgs e)

  • 0

This is my code :

private string[] MesiSelezionati;

protected void Page_Load(object sender, EventArgs e)
{
    MesiSelezionati = new string[] { "2", "4" };
    UpdateMesi();
}

override protected void OnInit(EventArgs e)
{
    for (int i = 1; i <= 12; i++)
    {
        HtmlGenericControl meseItem = new HtmlGenericControl("a") { InnerHtml = "mese" };
        meseItem.Attributes.Add("href", "javascript:void(0);");

        HiddenField hf = new HiddenField();
        hf.Value = "0";
        hf.ID = "idMese_" + i.ToString();

        meseItem.Controls.Add(hf);

        panelMesi.Controls.Add(meseItem);
    }

    base.OnInit(e);
}

private void UpdateMesi()
{
    foreach (HtmlGenericControl a in panelMesi.Controls.OfType<HtmlGenericControl>())
    {
        HiddenField hf = a.Controls.OfType<HiddenField>().LastOrDefault();
        if (MesiSelezionati.Contains(hf.ID.Split('_').LastOrDefault()))
        {
            hf.Value = "1";
            a.Attributes.Add("class", "box-ricerca-avanzata-item link-box selected");
        }
    }
}

When I call the page, all is ok! The problem is when I call the same page (so, postback) thanks to a asp:LinkButton. I get a System.NullReferenceException on if (MesiSelezionati.Contains(hf.ID.Split('_').LastOrDefault())).

Seems that the HiddenField of the 2° and 4° link (which corrispond to the position at MesiSelezionati = new string[] { "2", "4" };) are null. Why? And how can I fix it?

EDIT : code for Mark M

HtmlGenericControl optionBox = new HtmlGenericControl("div");
optionBox.Attributes["class"] = "option-box";

HtmlGenericControl optionBoxItem = new HtmlGenericControl("a") { InnerHtml = "&nbsp;" };
optionBoxItem.Attributes.Add("href", "javascript:void(0);");
optionBoxItem.Attributes.Add("class", "option-box-item");

HtmlGenericControl optionBoxTesto = new HtmlGenericControl("a") { InnerText = Categoria.Categoria };
optionBoxTesto.Attributes.Add("href", "javascript:void(0);");
optionBoxTesto.Attributes.Add("class", "option-box-testo");

HiddenField hf = new HiddenField();
hf.Value = "0";
hf.ID = "categoria_" + Categoria.UniqueID;

optionBox.Controls.Add(optionBoxItem);
optionBox.Controls.Add(optionBoxTesto);
optionBox.Controls.Add(hf);

panelCategorieGuida.Controls.Add(optionBox);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:27:40+00:00Added an answer on June 3, 2026 at 8:27 pm

    You can update hidden fields on postback, just not before Load. When executing OnInit, the controls have not been populated using the request & view state values. Your updates are being overwritten.

    EDIT: I found the root cause of your issue & learned something in the process.

    1. You set the InnerHtml property of the anchor tag (InnerHtml = "mese") in the OnInit method. Under the covers this assignment is ViewState["innerhtml"] = "mese".
    2. You assign a css class to the anchor after ViewState tracking has begun, so the ViewState restoration machinery will apply to this control on postback.

    When you postback the anchor tags with added css classes will be subject to HtmlContainerControl.LoadViewState (which occurs between InitComplete and PreLoad). If the LoadViewState method detects that ViewState[“innerhtml”] has a value it wipes out all of the control’s child controls (calling Controls.Clear()) and creates a LiteralControl to contain the innerhtml value, adding it as the sole child control.

    Basically this means that you cannot set both the InnerHtml property and add any controls to a descendant of HtmlContainerControl if that control will ever be subject to ViewState tracking.

    To fix your example; instead of setting InnerHtml to add the link text, create a LiteralControl with the desired text and add it to the anchor’s child control collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this code private void btnConnect_Click(object sender, EventArgs e) { string localHost = 192.168.10.3;
I am using this code: private void Form1_Load(object sender, EventArgs e) { } private
I have this code: private void button2_Click(object sender, EventArgs e) { //add pgs selected
With this code: private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { const int TICKETSOURCE_COLUMN =
A colleague of mine refactored this code: private void btnGeneral_Click(object sender, RoutedEventArgs e) {
I have this code private void writeReport(IReport report, string reportName) { string reportString =
this is my code private void sendToClient(Dictionary<string, string> reportDic) { Response.Clear(); Response.BufferOutput = false;
I have this code: private static void saveMetricsToCSV(String fileName, double[] metrics) { try {
I have this code : private void insertValus() { ContentValues initialValues = new ContentValues();
Consider this code: public void actionPerformed(ActionEvent e) { setEnabled(false); new SwingWorker<File, Void>() { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.