Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • claire 15 posts 36 karma points
    Nov 10, 2010 @ 00:51
    claire
    0

    Using postbackurls and viewstate in c# user control

    Hi,

    I have created a user control with 2 panels: the first panel has a form with a 'next' button that toggles the visibility of the panels; only one panel is visible at a time. On the second panel, I have a form with 3 buttons: Save, Add New and Delete.

    Basically, the user can enter and save details of mulitple items, e.g. title and description. I am using ViewState to store the list of items.

    Item myItem = new Item(itemId, txtTitle.Text, txtDesc.Text);
    List<Item> items = (List<Item>)ViewState["Items"];
    items.Add(myItem);
    ViewState["Items"] = items;

    When the user hits the Save button, it dynamically creates an Edit link for the item and displays the Edit link with the title in a list below the form and clears the form.

    E.g:

    link_edit = new LinkButton();
    link_edit.Text = "Edit";
    link_edit.PostBackUrl = "/umbraco/page_name.aspx?id=" + itemId;

     Expected output:

    Edit  Description of my first item
    Edit  Description of my second item
    Edit  Description of my third item

     

    When the user hits the Edit link it loads the title and description back into the form, allowing the user to Delete or Edit the details and Save.

    I set postbackurls for the Save and Delete buttons to ""/umbraco/page_name.aspx?" to remove the querystrings on postback. 

    Now.. I have created and tested this user control with c# in Visual Studio and it works perfectly. When I created a macro from this user control and inserted it into a page, the postbackurls and viewstate don't work :(

    Getting past the first panel is OK; panelOne is hidden and panelTwo is visible. If I enter an item and description, then hit Save, postback appears to reload the page as though for the first time. The eventhandler for the buttons aren't firing (I put in debug statements).

    I am hoping someone can shed some light because I'm stumped! Hopefully I've given enough information about what I've done and what I'm trying to achieve...I am running Umbraco 4.5.2, IIS 7 and Windows 7.

    Thanks in advance

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 10, 2010 @ 06:36
    Aaron Powell
    0

    Are you actually redirecting to separate pages each time? ViewState isn't cross-page so what you write into one pages ViewState shouldn't be accessible on the next.

    Also, you might be better off looking at the MultiView control, it has good handling for doing paged UIs.

  • claire 15 posts 36 karma points
    Nov 10, 2010 @ 10:58
    claire
    1

    No, all my controls are on the one page and I am using postbackurls to post back to itself.

    I removed the postbackurls on the buttons and have checked the value of IsPostBack when the page is reloaded. The first time I click Save, IsPostBack = true but if I try to save another item or click the Edit link, IsPostBack = false.

    Is this a ViewState issue? I don't understand why it works when I test the usercontrol in an aspx page in VisualStudio but not with Umbraco?

  • Lars Skjoldby 11 posts 29 karma points
    Jan 09, 2011 @ 22:04
    Lars Skjoldby
    0

    Hello Claire.

    I'm having the same issue. did you find a solution?

    - Lars Skjoldby, www.skjoldby.com

     

  • claire 15 posts 36 karma points
    Jan 11, 2011 @ 23:31
    claire
    0

    Hi there,

    My workaround was to get the name control that caused the postback and then calling an event depending on which control caused the postback.

    For example:

    if (Page.IsPostBack)
    {
    Control c = GetPostBackControl(this.Page);
    if (c != null)
    {
    if (c.ID == "btnSave") saveItem();
    }
    }

    For link buttons, use string ctrlname = this.Page.Request.Params.Get("__EVENTTARGET");

    Hope this helps!

Please Sign in or register to post replies

Write your reply to:

Draft