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 6002545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:59:37+00:00 2026-05-23T00:59:37+00:00

In our intranet ASP .NET application we use an architecture approach which is similar

  • 0

In our intranet ASP .NET application we use an architecture approach which is similar to the MVVM pattern.
Our viewmodel, which describes the state of the view, is either created or loaded in Page_Load from the ViewState.
Depending on the viewmodel content the page then is dynamically created.
Right now I am facing a problem where I cannot find an elegant solution.

The problem is that we have to handle events that are fired by the controls, which are dynamically created in the ListView. However, those registered events do only fire, if the listView is databound in Page_Init. If the databinding takes place in Page_Load those events are not fired.
The thing now is, that we need our ViewModel in order to databind the ListView to it. This ViewModel is cached in the ViewState, which – to my knowledge – only comes accessible in Page_Load. That means that in Page_Init we cannot access the ViewModel.

I created the following example:

ASP:

<form id="form1" runat="server">
    <asp:ListView runat="server" ID="lstView" ItemPlaceholderID="itemPlaceHolder">
        <LayoutTemplate>
            <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
        </LayoutTemplate>
        <ItemTemplate>
            <asp:DropDownList runat="server" ID="dropDown" SelectedValue='<%# Bind("SelectedValue") %>' DataSource='<%# Eval("Choice") %>' AutoPostBack="true" />
        </ItemTemplate>
    </asp:ListView>
</form>

Code Behind:


namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
                     //if i put the Initialization logic here, the event gets fired, however the Model is not accessible from the viewstate

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeModel();

            lstView.DataSource = _model.NestedModels;
            lstView.ItemCreated += (k, args) =>
            {
                var dropDown = (DropDownList)args.Item.FindControl("dropDown");
                dropDown.SelectedIndexChanged += new EventHandler(dropDown_SelectedIndexChanged);
            };
            lstView.DataBind();
        }

        void dropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write("Selected Index Changed fired");
        }

        private MyModel _model;
        void InitializeModel()
        {
            if (ViewState["model"] == null)

                ViewState["model"] = new MyModel();

            _model = (MyModel) ViewState["model"];

        }
    }

    [Serializable]
    public class MyModel
    {
        [Serializable]
        public class NestedModel
        {
            public NestedModel()
            {
                Choice = new List(new[] { "Value1", "Value2", "Value3", "Value4", "Value5" });
            }

            public string SelectedValue { get; set; }

            public List Choice { get; set; }
        }

        private List _nestedModels = new List();

        public MyModel()
        {
            Init();
        }

        public List NestedModels
        {
            get { return _nestedModels; }
        }

        void Init()
        {
            NestedModels.Add(new NestedModel() {SelectedValue = "Value1"});
            NestedModels.Add(new NestedModel() { SelectedValue = "Value2" });
            NestedModels.Add(new NestedModel() { SelectedValue = "Value3" });
            NestedModels.Add(new NestedModel() { SelectedValue = "Value4" });
            NestedModels.Add(new NestedModel() { SelectedValue = "Value5" });
        }
    }
}

I hope one of you guys knows a way either to preserve my event to get fired or to retrieve my cached ViewModel in Page_Init.

Best Regards,
TH

  • 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-05-23T00:59:38+00:00Added an answer on May 23, 2026 at 12:59 am

    You have to override the CreateChildControls event and dynamically add the control there on page postback. This will be called first and then your dynamically event handler is called.

    protected override void CreateChildControls()
    {
       PopulateControls();//populate your controls here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an (ASP.NET 3.5) intranet application which has been designed to use forms
I've got an ASP.NET web application, that is essentially our intranet site. I made
Hi I'm writing an ASP.Net MVC intranet application which is accessed via handhelds and
We have written Web application using ASP.NET MVC, which will be hosted in our
I am trying to build an application on our company's intranet using ASP.NET and
I'm working on an ASP.NET web application for our corporate intranet users. I have
I have an intranet ASP.NET web application in which I need to get the
In our ASP.NET MVC 3 intranet application, configured with WindowsTokenRoleProvider for authentication, users must
I have an ASP.NET application that runs on our intranet. In production I can
In our asp.net intranet application we are using windows authentication to authenticate the users.

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.