简体   繁体   中英

Passing value from Page to UserControl

Good day. I have a trouble with pass data between webform and UserControl.

I have the webform webform1.aspx. And i have a component GridView1. I also have UserControl - grdControl.ascx.

In webform1.aspx i mention UserControl:

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

Also webform1.aspx.cs contains an event

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

In turn UserControl grdControl.ascx.cs contains the following code:

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.

<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.

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. I don't think SelectedValue would be set at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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