ASP.NET Nested FormView

2019-07-18 18:58发布

问题:

I have this HTML.

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">
    <asp:FormView ID="FormView2" runat="server" DefaultMode="Insert" DataSourceID="SqlDataSource2">
       <asp:TextBox runat="Server" Text='<%# Eval("Terms") %>'></asp:TextBox>
    </asp:FormView>
</asp:FormView>

The code above works without any error but I want to get terms in the textbox fetched from SqlDataSource1 of FormView1 instead of FormView2 (SqlDataSource2). What I am missing here?

回答1:

You can access the value of Parent formView DataSource value in child formview as what you are currently doing. But there is another way you set value. like..

protected void ChildFormWiew_DataBound(object sender, EventArgs e)
{
    if (ChildFormView.CurrentMode == FormViewMode.Edit)
    {
        TextBox txtTemrs = ParentFormView.FindControl("Terms") as TextBox;
        ((TextBox)ChildFormView.FindControl("Terms")).Text = txtTemrs.Text;
    }
}