SF 4.1 - Modules - ViewState, PostBacks and the .NET LifeCycle - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

SF 4.1 - Modules - ViewState, PostBacks and the .NET LifeCycle

  • SF 4.1 - Modules - ViewState, PostBacks and the .NET LifeCycle
  • One of the main new features mentioned as part of SiteFintiy 4.1 is the ability to use standard ASP.NET ViewState, PostBacks and the .NET LifeCycle to design the backend pages of modules.

    Is there something that needs to be turned to activate this functionality?

    I have created a new module using the Jobs module as a starting point.  On the main backend landing page for my module I have a repeater that displays the items that have been saved, (not a RadGrid).  In this repeater I have text boxes that the user can change, and there is a "Save" button at the bottom.

    When I click the Save button, though, the repeater appears to have lost its viewstate because it doesn't have any items.  I should be able to enumerate the Repeater Items, get a reference to the text box, and save the value that the user typed in.. 

    How can I solve this problem in SiteFinity 4.1?

    Thanks.
  • Hi Chris,

    Make sure that you enabled the ViewState from the page properties section.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  • I went into the "Administration -> Backend Pages" and clicked to edit the page that my module created during the Install step.  I went into the "Advanced Options" section and checked "Enable Viewstate".  When clicking save I was prompted to type in a name for the page, since the automatic creation of the page didn't give it a name.  I gave the page name and clicked save.  It saved successfully.

    I restarted my application and went back into my module's admin page.  Setting a breakpoint in my button click method shows that the repeater still doesn't retain any items after postback. 

    I also tried adding ViewStateMode="Enabled" on the ascx page for this interface.  That didn't work either.

  • Hello Chris,

    Please share the code you use to get/set the TextBox values and the way you are binding the repeater.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  • Binding the repeater (this works):

    protected void UpdateDisplay()
            
                var module = new SlideshowModule();
                Repeater SlideshowImageRepeater = GetControl<Repeater>("SlideshowImageRepeater");
                SlideshowImageRepeater.DataSource = module.GetSlideshowItems();
                SlideshowImageRepeater.DataBind();
            


    Setting the values (this also works):

    protected void SlideshowImage_OnItemDataBound(object sender, RepeaterItemEventArgs e)
            
                if(e.Item.DataItem == null)
                    return;
      
                Document doc = (Document)e.Item.DataItem;
      
                Literal IDLiteral = e.Item.FindControl<Literal>("IDLiteral");
                TextBox SortOrderTextBox = e.Item.FindControl<TextBox>("SortOrderTextBox");
      
                IDLiteral.Text = doc.Id.ToString();
                SortOrderTextBox.Text = doc.GetValue<int>("SortOrder").ToString();
            


    Saving the values (this doesn't work):

    void UpdateSortOrder_Click(object sender, EventArgs e)
            
                SlideshowModule module = new SlideshowModule();
                  
                Repeater SlideshowImageRepeater = GetControl<Repeater>("SlideshowImageRepeater");
                foreach(RepeaterItem item in SlideshowImageRepeater.Items)
                
                    Literal IDLiteral = item.FindControl<Literal>("IDLiteral");
                    TextBox SortOrderTextBox = item.FindControl<TextBox>("SortOrderTextBox");
      
                    module.UpdateSortOrder(new Guid(IDLiteral.Text), Convert.ToInt32(SortOrderTextBox.Text));
                
      
                UpdateDisplay();
            


    When I get to the foreach loop in the UpdateSortOrder_Click method, the SlideshowImageRepeater.Items collection is empty.  I suspect this is because the viewstate isn't maintaining through the postback.  This isn't anything fancy.  I've done this hundreds of times in a standard ASP.NET website. 

    Also, where can I find documentation regarding the new 4.1 feature regarding "Viewstate, PostBacks and the .NET LifeCycle"?
  • Any response?
  • Hi Chris,

    Where you call UpdateDisplay()? If you call this method inside PageLoad, CreateChildControls, InitializeControls and then you click a button there should not be a problem with the items collection, because all these methods will be executed before the Click of your button.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  • When the button is clicked, I don't want "UpdateDisplay()" to be called until AFTER I save my data.  Otherwise the repeater gets re-bound and I lose the values that the user typed into the textboxes.

    The first time I called UpdateDisplay() is in the "InitializeControls()" method.  But the code looks like this:

    if (!Page.IsPostBack)
        UpdateDisplay();

    This way it only binds the repeater when the page initially loads, but not when the button is clicked because, as I said before, I don't want to rebind the repeater until I capture the changed values and save them, which happens in the "UpdateSortOrder_Click()" method.

    This is the same code that I've written countless times on standard ASP.NET website.  I just need to know how to make this work within the SiteFinity framework.
  • Hi Chris,

    The only way that I am able to replicate this issue is when the EnableViewState property of the page is not set.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  • The reason of this problem, is that one of the Validator controls does not have a value for the validation group property