0

Is here any way of binding <%# Page.ClientID %> without binding Page child controls?

For example:

<%# SomePageProprtyThatReturnsString %>
<someTag:SomeControl ID="SomeControlID" runat="server" OnDataBinding="SomeControlID_DataBinding"></someTag:SomeControl>

If I have following than SomeControlID will be bound on each unwanted post-back

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    DataBind();
}

If I will not bind page on each post back then SomePageProprtyThatReturnsString will not be visible on post-backs

4
  • Could you elaborate on this? I don't quite understand what the problem is... You only want SomePageProprtyThatReturnsString to be evaluated once but show up all the time? Or SomePageProprtyThatReturnsString should be within the context of your control? Commented May 3, 2013 at 16:24
  • Sorry, maybe I not so correctly described my question. We can use instead <%# SomePageProprtyThatReturnsString %> this <%# "some string" %>. So I want to see "some string" on my page always, but SomeControlID shold be bound only if page loaded initially(without post-back) Commented May 3, 2013 at 16:31
  • As far as I understood <%# %> evaluated only if called Page.DataBind() but If I call this that DataBind() of all page children will be invoked. So I wan't <%# %> to be evaluated each time when page loads, but child control should be bound only if !IsPostBack Commented May 3, 2013 at 16:33
  • Problem is that I don't know what is <%# %> and how it works so I cannot even google for it. Commented May 3, 2013 at 16:34

2 Answers 2

3

You can choose which controls you want to bind. So, if you have something like this:

<asp:Label runat="server" ID="lblSomething" Text='<%# SomePageProprtyThatReturnsString %>' />
<someTag:SomeControl ID="SomeControlID" runat="server" OnDataBinding="SomeControlID_DataBinding"></someTag:SomeControl>

You can call Databind only on the Label:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    lblSomething.DataBind();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for answering, but this is not exactly what I wanted to know. In your scenario I could just put Literal and set it's Text value in OnLoad. Or here is no way to evaluate code within <%# %> on a page without calling DataBind() of Page?
@Vladimirs No, there isn't. But you can databind the parent control. If you databind an asp:Panel for example all the child controls/expressions are binded
I wasn't aware of the fact that you can DataBind() other controls than ListViews or Repeaters - thanks!
1
<%# SomePagePropertyThatReturnsString %>

Looks like it might be some public property you've created on the Page itself? If that is the case you can output that property directly without the need for databinding by changing your code:

<%= this.Page.SomePropertyThatReturnsString %>

1 Comment

Sorry for delay. This works fine for me. Could you please explain what is <%# and <%=(I don't know how it called so I can't google any info for that)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.