send custom parameters to user control ascx

前端 未结 1 1035
独厮守ぢ
独厮守ぢ 2020-12-15 20:44

I need to use user controls (.ascx) on a page, it\'s a related post user control based in 2 parameters:

 1. Current post
 2. Relation type

相关标签:
1条回答
  • 2020-12-15 20:53

    Create public properties of the user-control like:

    public partial class SampleUC : UserControl
    {
        public string CurrentPost {get;set;}
        public string RelationType {get;set;}
    
        //...
    
        //...
    }
    

    Assign those from the page using it either from markup like:

    <%@ Register TagPrefix="cc" TagName="SampleUC" Src="SampleUC.ascx" %>
    ...
    ...
    <cc:SampleUC id="myUC" runat="server" CurrentPost="Sample Post Title" RelationType="Title" />
    

    or from code-behind (of the page using it):

    protected void Page_Load(object sender, EventArgs e)
    {
        //...
    
        myUC.CurrentPost = "Sample Post Title";
        myUC.RelationType = "Title" ;
    
        //...
    }
    
    0 讨论(0)
提交回复
热议问题