Reputation: 10546
I have to hidden buttons on my page that I can trigger using JavaScript (__dopostback...) I also have a list of object on my page and a dropdownlist giving the user the possibility to sort those objects on the page.
My code checks the current value of the dropdownlist on Databind of the list and applies the choosen sort method to the list.
The first button is triggered when the user has selected another sort method from the list.
The second button is triggered when the user adds a new item to the list. Sortmethod "sort2" should be set automatically in the dropdownlist if this happens.
So when the second button has been clicked, I need to set the selected item of the dropdownlist dynamically so i can check the value of the selected value later in the DataBind method of the dropdownlist and know what sort method should be applied.
I hope it's somehow clear so you can help me out here. I don't succeed in changing the selected value dynamically.
I'm using to controls: First is the objects.vb and the second is the ObjectContainer.vb Objects.vb contains the two buttons, the dropdownlist and an instance of the objectcontainer. The objectcontainer contains the list with objects that needs to be sorted based on the value selected in the objects control.
Objects.vb:
Private sortMethod As DropdownList
Private container as ObjectContainer
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
EnsureChildControls()
End Sub
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
' Add sorting
sortMethod = New DropDownList()
sortMethod.ID = "sortlist"
CType(sortMethod.Items, IStateManager).TrackViewState()
If Not Page.IsPostBack Then
Dim sort1 As New ListItem()
sort1.Text = "Sort Method 1"
sort1.Value = "sort1"
Dim sort2 As New ListItem()
sort2.Text = "Sort Method 2"
sort2.Value = "sort2"
Dim sort3 As New ListItem()
sort3.Text = "Sort Method 3"
sort3.Value = "sort3"
sortMethod.Items.Add(sort1)
sortMethod.Items.Add(sort2)
sortMethod.Items.Add(sort3)
sortMethod.AutoPostBack = False
sortMethod.EnableViewState = True
sortMethod.Attributes.Add("onchange", "javascript:TriggerPostbackButtonUpdate()")
End If
Me.Controls.Add(sortMethod)
container = New ObjectContainer
container.ID = "oc"
Me.Controls.Add(container)
End Sub
Protected Sub ButtonUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
container.SortMethodAndOrder = sortMethod.SelectedValue
container.DataBind()
End Sub
'when this event is triggered, the "sort2" item should be selected after the postback
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sortMethod.SelectedIndex = sortMethod.Items.IndexOf(sortMethod.Items.FindByValue("sort2"))
container.SortMethodAndOrder = "sort2"
container.DataBind()
End Sub
ObjectContainer.vb:
Public Overloads Overrides Sub DataBind()
EnsureChildControls()
Dim objects As ObjectsCollection = Nothing
If objects.Count <> 0 Then
Dim sortList As DropDownList = DirectCast(Me.Parent.FindControl("sortlist"), DropDownList)
If sortList IsNot Nothing Then
'sort based on the selected value in the dropdownlist
End If
End If
End Sub
I can provide any info you need to help me out here. I really appreciate your time so thanks a lot already for taking some time to read this!
Upvotes: 0
Views: 2992
Reputation: 10546
Here's how id did it using jquery to set the selected value of the dropdownlist before triggering the postback.
$(".myDropDownList").val("sort2");
__doPostBack(uniqueIdOfMyButton,'');
Upvotes: 0
Reputation: 15413
I don't see a reason to call TrackViewState manually in your code. Is there a specific reason to call it ? Otherwise, I would remove it.
Don't know if this can be of any help, but I tend to use the two following lines (as a C# user, but I guess it does not make any difference ) :
sortMethod.ClearSelection()
sortMethod.Items.FindByValue("sort2").Selected = true
instead of :
sortMethod.SelectedIndex = sortMethod.Items.IndexOf(sortMethod.Items.FindByValue("sort2"))
Upvotes: 1