asp.netvb.netdropdownispostback

How to control dropdownlist auotpostback in asp.net?


I have a dropdownlist with autopostback and an update panel, it works correctly, but I have also other controls in my page with autopostback. What I need is to control when the page is autopostback but the dropdownlist is not autopostback do something, like this:

     If is not Page.autopostback then
    'do something

else if is not MyDropdownlist.autopostback then    
    ' do something different    
    End if

I can use this:

If is not Page.autopostback then
End If

But I can´t do this:

If is not MyDropdownlist.autopostback then
End If

So how can I do this? I hope my explanation has been helpful, thanks.


Solution

  • The __EVENTTARGET request form variable has the name of control that caused postback to happen. You can query the name of this control and do whatever you want to do.

    For example,

    If IsPostBack Then
        Dim postBackControlId As String = Request.Form("__EVENTTARGET")
        If Not String.IsNullOrEmpty(postBackControlId) Then
            If postBackControlId = "DropdownList1" Then
                ' the postback happened due to DropdownList1
    
            Else
                ' the postback happened due to some other control.
    
            End If
        End If
    End If