如何通过用户控制的价值在ASP.NET C# [英] how to pass value between user control in ASP.NET C#

查看:112
本文介绍了如何通过用户控制的价值在ASP.NET C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的第3页的用户控件。该用户控件的一个是关于fileUploading和第二约为fileUploade关键字。当文件上传fileUploading用户控件返回记录了插入的ID。现在我想用这个ID在fileUploaded关键字,但是当插入是标记为看到ID在fileuploading用户控制forexample显示正确的ID 1或2或..但在关键字用户控件只显示0值。我使用实体框架的工作。我怎么可以访问此ID在关键字的用户控件。
谢谢。

i have 3 user control in my page . one of this user control is about fileUploading and second is about fileUploade Keyword . when file uploaded the fileUploading usercontrol return an Id of the recorded that insert . now i want to use this id in fileUploaded keyword but when is insert to label for seeing the id in fileuploading user control show right id forexample 1 or 2 or .. but in keyword usercontrol show only 0 value . i use entity frame work . how can i access this id in keyword user control . thanks.

推荐答案

您可以使用委托来传递ID回父页面。当ID被传递回父页面,您运行调用了关键词用户控件公共函数父页面上的功能。

You can use a delegate to pass the id back to the parent page. When the id is passed back to the parent page, you run a function on the parent page that invokes a public function on the keyword usercontrol.

所以你的用户控件上传,首先创建委托父页面将监听:

So on your Uploading usercontrol, first create the delegate that the parent page will listen for:

public delegate void PassIDToParent(string ID);
public event PassIDToParent PassID;

接下来,火委托,一旦你的ID返回:

Next, fire the delegate once you have the id returned:

protected void Upload() //--- this is your current upload function
{
    //--- other upload code
    string ReturnedID = "1"; //--- whatever is returned
    PassID(ReturnedID);
}

现在你需要设置父页面上的监听器。一旦监听执行时,它将执行PassID功能。在这个函数中,您将调用用户控件关键字的公共职能LoadID。因此,父页上,这样做:

Now you need to setup the listener on the parent page. Once the listener executes, it will execute the "PassID" function. In this function, you will be calling the public function "LoadID" of the keyword usercontrol. So on the parent page, do this:

protected void Page_Load(object sender, EventArgs e)
{
    //--- setup the listener to receive the uploaded id
    UploadUserControlID.PassID += new UploadUserControlID_ClassName.PassIDToParent(PassID);
}
protected void PassID(string id)
{
    //--- this method is called from the listener above and it passes the id
    KeywordUserControlID.LoadID(id);
}

而code您的关键字用户控件应该是这样的:

And the code for your keyword usercontrol would look like this:

public void LoadID(string id)
{
    //--- do whatever you need to with the id
}

下面是一些关于使用代表更多的信息:<一href=\"http://webdeveloperpost.com/Articles/Return-value-from-user-control-in-ASP-NET-and-C-Sharp.aspx\" rel=\"nofollow\">http://webdeveloperpost.com/Articles/Return-value-from-user-control-in-ASP-NET-and-C-Sharp.aspx

Here is some more info regarding the use of delegates: http://webdeveloperpost.com/Articles/Return-value-from-user-control-in-ASP-NET-and-C-Sharp.aspx

希望帮助!祝你好运!不要忘记标记为答案!

Hope that helps! Good Luck! Don't forget to mark as answer!

这篇关于如何通过用户控制的价值在ASP.NET C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆