Search code examples
asp.netvb.netwebforms

using loadPostData instead of viewstate isn't updating my ddl


We can't use viewstate due to some network limitations so we're trying to refactor some of the site.

As a test I created a select

<asp:dropdownList ID="testDdl" ClientIDMode="static" runat="server"/>

in page_init I add options to it

testDdl.Items.Insert(0, New ListItem("zero", 0))
testDdl.Items.Insert(1, New ListItem("one", 1))
testDdl.Items.Insert(2, New ListItem("two", 2))
testDdl.Items.Insert(3, New ListItem("three", 3))

After selecting "2" and submitting the form: after page_init I'm expecting loadPostData to apply the proper value to the ddl.

In the submit.click handler the value doesn't appear to be applied to the dropdown as I expected.

temp = Request.Form("ctl00$cntMain$testDdl") '2, this is correct
temp = testDdl.SelectedValue.ToString '0, not the value i selected
temp = testDdl.SelectedIndex.ToString '0, not the value i selected
temp = testDdl.SelectedItem.Text '"Zero" not the value i selected

It was my understanding that the loadPostData would apply the proper values to the testDdl control.

I have seen this work correctly binding data on other pages and even during catch up events on dynamic controls. What am I doing wrong above?


Solution

  • Well, it is not all that clear as to your "use case" in which you suggest some reason for viewstate not being allowed.

    You should expand on this requirement - and I mean REALLY make an AMAZING explain as to what the reason is for disabling viewstate.

    However, it gets somewhat worse, since disabling viewstate will not prevent you from using the control's selected value.

    Assuming you turned off viewstate for the web page. (In the page directive). And assuming you turned off viewstate for the dropdownList control?

    Then you still have control state, and the correct selected value of the dropdownList will persist, and will persist despite that on each post-back, you are re-loading the control.

    So, this markup:

                <asp:dropdownList ID="testDdl" ClientIDMode="static" runat="server" 
                    ViewStateMode="Disabled"                    
                    />
    
            <br />
            <asp:Button ID="Button1" runat="server" Text="Submit"
                OnClick="Button1_Click"
                />
    

    And code behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    End Sub
    
    Private Sub WebForm3_Init(sender As Object, e As EventArgs) Handles Me.Init
    
        testDdl.Items.Insert(0, New ListItem("zero", 0))
        testDdl.Items.Insert(1, New ListItem("one", 1))
        testDdl.Items.Insert(2, New ListItem("two", 2))
        testDdl.Items.Insert(3, New ListItem("three", 3))
    
    End Sub
    
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
    
        Debug.Print($"value = {testDdl.SelectedItem.Value}")
        Debug.Print($"Text = {testDdl.SelectedItem.Text}")
    
    End Sub
    

    Running the above, we see/find that everything works just fine, and you are 100% free to simply use the control's selected item as we always have, and this is the case despite that viewstate for the page (or control) having been disabled.

    So, when we run above, we see this:

    enter image description here

    So, really, even with the page viewstate turned off. And with the control's viewstate turned off?

    You still are 100% free to use plain Jane regular code as you always done, and use of the control's "Value" property, or use of the control's "Text" property will continue to work as always. And this works without any special efforts on your part.

    No need to look at post-data, since the control's state will persist, and does so even when viewstate is turned off.