Update textbox through selected item in listbox
In ASP.NET I have a dropdown
with some items in it, I have a button
and a textbox
. I am subscribed to the SelectedIndexChanged
event of the dropdown where I pass the new selected index of the dropdown to a struct that converts it's index (via enum
) to a string. That string is then gotten through a property in a class to put into the textbox.
//Enum and struct representing index to string conversion for dropdown
Public Enum e_action
AcOne = 0
AcTwo
AcThree
AcThree
开发者_Go百科 AcFour
End Enum
Public Structure Action
Public Sub New(ByVal index As Integer)
Select Case index
Case 0
action = e_action.AcOne
Case 1
action = e_action.AcTwo
Case 2
action = e_action.AcThree
Case 3
action = e_action.AcFour
Case 4
action = e_action.AcFive
End Select
End Sub
// this is the selectedIndexChanged function for the dropdown
Protected Sub dropdownAction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dropdownAction.SelectedIndexChanged
Dim ddl As DropDownList = TryCast(sender, DropDownList)
If Not IsNothing(ddl) Then
Dim act As New Action(ddl.SelectedIndex)
p_two_action = act //p_two_action is a global var of type Action
End If
End Sub
//inside the button handler
dim myclass as MyClass
MyTextBox.Text = myclass.getAction //returns string of action done in dropdown
Now the problem I have is that, when you click it (the button) the first time, the action get's updated with the current action selected in the listbox, but then when you click the button again WITHOUT changing anything in the listbox, the textbox shows the zero'th item in the textbox, (seems to reset), although it actually hasn't changed at all.
I'm guessing this might have something to do with the postback caused by the button click, which resets the state of the global or something, but I'm not sure. Why is it being reset while the dropdown box is still as I set it the first time?
Can someone help?
If this is not clear, please leave a comment on what is not clear. Thx!
The second time around (when you click the button but don't select anything in the list), the SelectedIndexChanged event wont get raised (since you didn't change the selected index).
You'll need to make sure that your logic in the button handler can also get the proper action for the current selected index of the drop down. The p_two_action seems to be getting reset - when you say it is a global how have you implemented it?
精彩评论