将用户控件中的值传递给后面的代码

时间:2014-08-25 18:19:25

标签: c# asp.net user-controls

我试图将用户控件中的值传递给后面没有运气的代码。我可以在aspx页面上写出正确的值(IDuser_uc),但我无法将其传递给后面的代码。有什么提示吗?

用户控制

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

    }

ASPX页面

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

背后的代码

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

    }

更新

问题是由于用户控件在PageLoad之后运行,这解释了为什么我得到一个空字符串。为了解决这个问题,我在页面后面的代码中使用了Page_PreRender而不是Page_Load。

1 个答案:

答案 0 :(得分:0)

在您的用户控件中创建一个公共方法。当您回发页面时,Parent.aspx页面调用用户控件方法。实施例....

  1. 在您的用户控件中:

    公共字符串GetUserControlValue() {     return Label IDuserText;

    }

  2. 在您的父ASPX Pgae中

    var UserControlString = IDuser_uc.GetUserControlValue();