简体   繁体   English

将值从Page传递到UserControl

[英]Passing value from Page to UserControl

Good day. 美好的一天。 I have a trouble with pass data between webform and UserControl. 我在webform和UserControl之间传递数据时遇到麻烦。

I have the webform webform1.aspx. 我有webform webform1.aspx。 And i have a component GridView1. 而且我有一个组件GridView1。 I also have UserControl - grdControl.ascx. 我也有UserControl-grdControl.ascx。

In webform1.aspx i mention UserControl: 在webform1.aspx中,我提到了UserControl:

<uc1:grdcontrol runat="server" id="grdControl" />

Also webform1.aspx.cs contains an event 而且webform1.aspx.cs包含一个事件

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
     grdControl.SelectedValue = GridView1.SelectedValue.ToString();
  }

In turn UserControl grdControl.ascx.cs contains the following code: 反过来,UserControl grdControl.ascx.cs包含以下代码:

private string _selectedValue;        
public string SelectedValue         
{            
  get { return _selectedValue; }            
  set { _selectedValue = value; }
}

In grdControl.ascx i use label and i try to get value SelectedValue and use it as text for Label1. 在grdControl.ascx中,我使用label,并尝试获取值SelectedValue并将其用作Label1的文本。

<asp:Label ID="Label2" runat="server" Text='<%# SelectedValue%>' ></asp:Label>

But is don't work correctly. 但是是无法正常工作。 In a web page i see nothing. 在网页上,我什么也看不到。

Setting the value of the label in the PreRender event of the code behind should fix it. 在后面代码的PreRender事件中设置标签的值应该可以解决该问题。

I'm thinking that GridView1_SelectedIndexChanged will get called after the Page_Load event of the inner grid control that's why the value is blank because you are trying to set it in the page itself and that happens in the Pre_init function of the inner control. 我以为GridView1_SelectedIndexChanged将在内部网格控件的Page_Load事件之后被调用,这就是为什么该值为空的原因,因为您试图在页面本身中进行设置,并且发生在内部控件的Pre_init函数中。 I don't think SelectedValue would be set at that point. 我认为此时不会设置SelectedValue。

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

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