This is one of those problems which seems like it should have a simple solution but I can't work out what it is!
这是其中一个似乎应该有一个简单解决方案的问题,但我无法弄清楚它是什么!
How can I pass a control from one sub to another if the first sub doesn't actually call the second? For example, where btnChangeText is in a panel that has a ModalPopupExtender called mpExample, and therefore isn't usually visible:
如果第一个sub实际上没有调用第二个sub,我怎样才能将控件从一个sub传递给另一个sub?例如,btnChangeText位于具有名为mpExample的ModalPopupExtender的面板中,因此通常不可见:
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
<SpecifiedTextBox>.Text = "Hello"
End Sub
And then on the main page, visible at all times, is a button associated with each textbox. In this example, it's textbox15:
然后在主页面上,始终可见,是与每个文本框关联的按钮。在这个例子中,它是textbox15:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
<Set TextBox15 as variable>
mpExample.Show()
End Sub
I know it's a silly example - believe me when I say that the real application I want to make of this actually makes sense! But the point is that I want to somehow store the name of the control to be updated by the first sub when the second sub is run.
我知道这是一个愚蠢的例子 - 相信我,当我说我想要的真正的应用程序实际上是有道理的!但关键是我想以某种方式存储控件的名称,以便在第二个子运行时由第一个子更新。
If I was calling the first sub from the second it'd be easy, I'd just pass it as an argument, but I'm not. The first sub is called from a button click and is an independent action from the running of the second sub.
如果我从第二个调用第一个sub它很容易,我只是把它作为一个参数传递,但我不是。从按钮单击调用第一个子,并且是从第二个子的运行开始的独立动作。
I don't seem to be able to use a session variable (my first thought) because I can't find any way to store the control name as a string and then convert it back to an actual control when the first sub runs. That'd be the easiest answer if somebody could tell me how to do it.
我似乎无法使用会话变量(我的第一个想法),因为我找不到任何方法将控件名称存储为字符串,然后在第一个子运行时将其转换回实际控件。如果有人能告诉我怎么做,那将是最简单的答案。
1 个解决方案
#1
1
One approach would be to store the control's ID as a string in a Session variable, and then use the FindControl
method to grab the control in your 2nd Click event.
一种方法是将控件的ID作为字符串存储在Session变量中,然后使用FindControl方法获取第二次Click事件中的控件。
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15.ID
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Page.FindControl(Session("currentTextBox")),TextBox)
currentTextBox.Text = "Hello"
End Sub
Note that if your TextBox15 control is inside some kind of container (a Panel or something), you'll need to use that container's FindControl
method, rather than Page.FindControl
.
请注意,如果TextBox15控件位于某种容器(Panel或其他)中,则需要使用该容器的FindControl方法,而不是Page.FindControl。
Another approach is to store the TextBox
itself in a Session variable, and then pull that out to set the text in your other method. Note that this only works if the methods are both called in the same request (which doesn't sound like it would work for your use-case). Here's what that would look like:
另一种方法是将TextBox本身存储在Session变量中,然后将其拉出以在其他方法中设置文本。请注意,这仅适用于在同一请求中调用方法的情况(这听起来不像它对您的用例有效)。这是什么样子:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Session("currentTextBox"), TextBox)
currentTextBox.Text = "Hello"
End Sub
#1
1
One approach would be to store the control's ID as a string in a Session variable, and then use the FindControl
method to grab the control in your 2nd Click event.
一种方法是将控件的ID作为字符串存储在Session变量中,然后使用FindControl方法获取第二次Click事件中的控件。
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15.ID
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Page.FindControl(Session("currentTextBox")),TextBox)
currentTextBox.Text = "Hello"
End Sub
Note that if your TextBox15 control is inside some kind of container (a Panel or something), you'll need to use that container's FindControl
method, rather than Page.FindControl
.
请注意,如果TextBox15控件位于某种容器(Panel或其他)中,则需要使用该容器的FindControl方法,而不是Page.FindControl。
Another approach is to store the TextBox
itself in a Session variable, and then pull that out to set the text in your other method. Note that this only works if the methods are both called in the same request (which doesn't sound like it would work for your use-case). Here's what that would look like:
另一种方法是将TextBox本身存储在Session变量中,然后将其拉出以在其他方法中设置文本。请注意,这仅适用于在同一请求中调用方法的情况(这听起来不像它对您的用例有效)。这是什么样子:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Session("currentTextBox"), TextBox)
currentTextBox.Text = "Hello"
End Sub