通过列表框中选定的项目更新文本框
在 ASP.NET 中,我有一个包含一些项目的下拉列表
,我有一个按钮
和一个文本框
。我订阅了下拉列表的 SelectedIndexChanged
事件,在该事件中,我将下拉列表的新选定索引传递给一个结构,该结构将其索引(通过 enum
)转换为字符串。然后通过类中的属性获取该字符串并将其放入文本框中。
//Enum and struct representing index to string conversion for dropdown
Public Enum e_action
AcOne = 0
AcTwo
AcThree
AcThree
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
// 这是下拉列表的 selectedIndexChanged 函数
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
现在我遇到的问题是,当您第一次单击它(按钮)时,操作将使用列表框中选择的当前操作进行更新,但是当您单击按钮时再次没有更改列表框中的任何内容,文本框显示文本框中的第零项(似乎重置),尽管它实际上根本没有更改。
我猜这可能与单击按钮引起的回发有关,这会重置全局状态或其他东西,但我不确定。为什么下拉框还是第一次设置的时候却被重置了?
有人可以帮忙吗?
如果不清楚,请对不清楚的地方发表评论。谢谢!
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
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二次(当您单击按钮但未选择列表中的任何内容时),将不会引发 SelectedIndexChanged 事件(因为您没有更改所选索引)。
您需要确保按钮处理程序中的逻辑也可以为当前选定的下拉列表索引获取正确的操作。 p_two_action 似乎正在重置 - 当你说它是全局的时,你是如何实现它的?
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?