Using Code Nuggets as property values

前端 未结 1 337
没有蜡笔的小新
没有蜡笔的小新 2021-01-28 16:27

I have the following code





        
1条回答
  •  爱一瞬间的悲伤
    2021-01-28 16:56

    Why it's working outside and not with Control?

    Well <%= %> is called Content Code Nuggets because they inject content in the html which is sent by server to browser. It does the work of Reponse.Write. We use this mainly to call any code written in code behind file for example, you have a simple method in your code behind:-

    public string UserCity()
    {
       return "London";
    }
    

    Then you can call it from aspx page like this:-

    You live in <%= UserCity() %>.
    

    Content code nuggets used to inject the html to response at the last in PreRender event and thus it's called late binding. This is the main reason why it is NOT working with your control.

    How to fix this?

    You can use the data binding code nuggets (<%# %>). These are used with DataBound controls but you can force the Page's DataBound or control's DataBound method to bind your control like this:-

      
    

    Call DataBind method in code behind:-

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.DataBind();
        }
    }
    

    0 讨论(0)
提交回复
热议问题