开发者

Totally lost – data binding expressions inside GridView’s template

开发者 https://www.devze.com 2022-12-25 12:37 出处:网络
1) On aspx page we define GridView control named gvwPolls, and inside its template we define a user control named pollBox1

1) On aspx page we define GridView control named gvwPolls, and inside its template we define a user control named pollBox1

<asp:GridView ID="GridView1" DataSourceID="objPolls" ...>
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                Question is : <%# Eval("QuestionText") %> <br />
                <mb:PollBox ID="PollBox1" runat="server" PollID='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:ObjectData开发者_StackOverflow中文版Source ID="objPolls" ...></asp:ObjectDataSource>

a) I assume that inside gvwPolls’s template, the gvwPollBox1.DataBind is called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?!

b) Can someone offer some explanation how or why is gvwPollBox1.DataBind called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?

2) Continuing with the above example:

-- pollBox1 user control defines a Repeater control named rptOptions:

<asp:Repeater runat="server" ID="rptOptions">
    <ItemTemplate>
        <%# Eval("pollBoxTitle") %>
    </ItemTemplate>
</asp:Repeater>

-- In pollBox1’s code-behind file we bind rptOptions to a data source inside DoBinding() method.

-- We also override pollBox1’s DataBind() method:

  public override void DataBind()
  {
      base.DataBind();
      DoBinding();
  }

a) I assume that due to overriding pollBox1.DataBind(), the data binding expression <%# Eval("pollBoxTitle") %> ( defined inside rptOptions’s template ) will get evaluated prior to a call to DoBinding method? If so, won’t then <%# Eval("pollBoxTitle") %> get evaluated before rptOptions is actually bound to a data source?

b) If that is the case, how then is rptOptions able to extract value ( from data source’s pollBoxtitle property) from a data source, if at the time the <%# Eval("pollBoxTitle") %> expression got evaluated, rptOptions wasn’t yet bound to any data source?

thanx


I can't explain why the page life cycle is the way that is, probably has something to do with rendering childs before the parent object. When exactly do you call .DataBind() in the PollBox control? Try to move it into an event that is later in the life cycle, like PreRender.

There is also another way to ensure it is working the way you want to: Subscribe to the RowDataBound Event, use .FindControl("YourPollBoxID") to get the instance of the control current bound row, set the properties and perform a manuall .DataBind();

0

精彩评论

暂无评论...
验证码 换一张
取 消