简体   繁体   English

将值从用户控件传递到后面的代码

[英]Passing a value from a User Control to code behind

I'm trying to pass a value from a User Control to a code behind without luck. 我正在尝试将用户控件中的值传递给后面的代码而没有运气。 I can write the correct value (IDuser_uc) on the aspx page but I can't pass it to the code behind. 我可以在aspx页面上写入正确的值(IDuser_uc),但无法将其传递给后面的代码。 Any tips? 有小费吗?

User Control 用户控制

protected void FormView_IDuser_DataBound(object sender, EventArgs e)
    {
            Label IDuserText = FormView_IDuser.FindControl("IDuserLabel") as Label;
            IDuser_uc = Convert.ToString(IDuserText.Text);

    }

ASPX page ASPX页面

<uc4:IDuser id="IDuser" runat="server" />
<% Response.Write(IDuser.IDuser_uc); // Here correct value %>

Code Behind 背后的代码

protected void Page_Load(object sender, EventArgs e)
    {
     SqlDataSource_userConnections.SelectParameters["IDuser_par"].DefaultValue = IDuser.IDuser_uc ; // Here NO value
     Response.Write("Connections :" + IDuser.IDuser_uc); // Here NO value

    }

UPDATE UPDATE

The problem was due to the fact that the User Control is run after the PageLoad which explains why I got an empty string. 问题是由于用户控件在PageLoad之后运行,这解释了为什么我得到一个空字符串的事实。 To solve the problem I used Page_PreRender instead of Page_Load on the code behind page. 为了解决该问题,我在页面后面的代码中使用了Page_PreRender而不是Page_Load。

Create a public method in your user control. 在用户控件中创建一个公共方法。 When you post back the page have the Parent.aspx page call the user control method. 当您发回页面时,让Parent.aspx页面调用用户控制方法。 Example.... 例....

  1. In your User Control: 在您的用户控件中:

    Public string GetUserControlValue() { return Label IDuserText; 公共字符串GetUserControlValue(){返回标签IDuserText;

    } }

  2. In your Parent ASPX Pgae 在您的父ASPX Pgae中

    var UserControlString = IDuser_uc.GetUserControlValue(); var UserControlString = IDuser_uc.GetUserControlValue();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM