在三个方法之间传递两个变量
我将如何将 int 变量和字符串变量从一种方法发送到另一种方法,再发送到最后的第三种方法?这基本上就是我设置方法的方式。显然,每个方法都有运行的代码,但我只是想了解如何将这些变量传输到其他方法。
protected void deleteOkBtn_Click(object sender, EventArgs e)
{
remove()
}
protected void imgdelbtn_Click(object sender, EventArgs e)
{
}
public void remove()
{
}
How would I go about sending a int variable, and string variable from one method to another method, to a final third method? This is basically how I have my methods setup. Obviously there is code that runs in each of these, but I am just trying to understand how to transfer these variables to my other methods.
protected void deleteOkBtn_Click(object sender, EventArgs e)
{
remove()
}
protected void imgdelbtn_Click(object sender, EventArgs e)
{
}
public void remove()
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建隐藏字段控件在页面上。
然后设置它并在代码隐藏的(几乎)任何事件中引用它:
You can create a hidden field control on the page.
And then set it and reference it within (almost) any event in your code-behind:
在您的删除方法中引发一个事件
MyCustomEvent
,该事件采用MyCustomEventArgs
,其中包含您想要传递给另一个类的字段。然后,让其他类将处理程序挂钩到该类事件中。
In your delete method raise an event
MyCustomEvent
that takesMyCustomEventArgs
which contains the fields that you want to pass to another class.Then, have that other class hook a handler into this classes event.
您是从方法 1 调用方法 2 再调用方法 3 吗?或者它们都是由用户事件调用的?
鉴于您调用它们,但不想更改参数名称,您可以创建一个结构并将其作为发送者对象发送,然后将其强制转换回该结构,或者创建一个扩展 EventArgs 的新类并将它们传递到那里。
如果由用户事件调用并且您想要共享这些值,那么全局值将是可行的方法,或者正如 Kon 评论,这可能是一个 asp.net 页面,那么您将使用 ViewState 来保存这些值回发之间。
ViewState 的使用示例如下所示:
然后,在您的方法中,您可以将 ClauseId 作为页面的属性进行访问。
PS,我刚刚从一些代码中复制了这个。引号中的名称可以是您想要的任何名称,只要读写时相同即可,并且可以与属性名称不同。您也不必使用属性,我只是发现如果访问多个位置,则比在方法中直接读取和写入 ViewState 更容易读取。
Are you calling from method1 to method2 to method 3? Or are they each called by user events?
Given that you call them, but don't want to change parameter names, you could create a structure and send it as the sender object then cast it back to the structure, or create a new class that extends EventArgs and pass them there.
If the are called by user events and you want to share the values, then global values would be the way to go, or as Kon comments, that this may be an asp.net page, then you'd use ViewState to hold the values between postbacks.
A sample use of ViewState would look like:
Then in your methods you could access ClauseId as a property of the page.
Ps, I just copied this from some code. The names in quotes can be anything you want, just be the same for reading and writing, and it can be different than the property name. You also don't have to use a property, I just find it easier to read if accessed multiple places than reading and writing directly to ViewState in methods.