Pergunta

This is a difficult scenerio to explain, so I coded up a simple example.

<asp:UpdatePanel runat="server" ID="upTest" ChildrenAsTriggers="true"
UpdateMode="Conditional">
  <ContentTemplate>
     <asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="true"></asp:DropDownList>
     <br /><br /> 
     In page: <asp:TextBox runat="server" ID="txtTest" Columns="50" Text="OnLoad</asp:TextBox>
     <br />
     <br />
     <asp:Button runat="server" ID="btnTest" Text="Click it" />
  </ContentTemplate>
  <Triggers>
     <asp:AsyncPostBackTrigger ControlID="ddlTest" />
  </Triggers>
 </UpdatePanel>

Code-Behind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack Then
        BindDropDown(Request.Form(ddlTest.UniqueID))
    Else
        BindDropDown(0)
    End If
End Sub

Public Sub BindDropDown(val As Integer)

    ddlTest.Items.Add(New ListItem("", 0))
    ddlTest.Items.Add(New ListItem("One", 1))
    ddlTest.Items.Add(New ListItem("Two", 2))
    ddlTest.Items.Add(New ListItem("Three", 3))
    ddlTest.Items.Add(New ListItem("Four", 4))
    ddlTest.SelectedValue = val

End Sub

Private Sub ddlTest_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlTest.SelectedIndexChanged
    txtTest.Text = "Dropdown changed"
End Sub

Private Sub btnTest_Click(sender As Object, e As System.EventArgs) Handles btnTest.Click
    txtTest.Text = "Button clicked"
End Sub

Also to note, I have EnableViewState="false" and ClientIdMode="Static" for the page.

When I click the button the partial postback occurs and the textbox has the expected value of 'Button Clicked'

When I change the dropdown the partial postback occurs, however the textbox does not have the expected value of 'Dropdown changed' because the code in ddlTest_SelectedIndexChanged is never hit (breakpoint placement on that line of code also never hits)

In the larger scope of my project this is the crux of the problem and I can't determine why this event is never hit (the core problem is when I load a dynamic user control, the data in the control is bound correctly, but then is overridden with the pre-post data after the user control is loaded - the override is occurring somewhere in the post back events). It appears to me it has something to do with the binding of the dropdown and where it happens in the page cycle, but I haven't been able to nail anything down, nor come up with google solutions.

One odd thing I did notice when looking at the post in Firebug - the EVENTTARGET value for the dropdown was the UniqueId of the dropdown, but the EVENTTARGET value is empty for the button click. My brain is thinking there is some connection here.

FYI - I have come up with a work around that seems to do the trick. I set autopostback on dropdown to false, and using jQuery I assign the change event on the dropdown to fire the button click - which causes the postback and fires btn_click event in code.

Foi útil?

Solução

I solved the initial issue here by moving the dropdown fill to the init event and then letting .NET load the selected value from view state

However in doing this, now the selectedindex event fires on EVERY postback.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top