C# 根据页面中的其他控件设置模板中文本框的默认值

C# 根据页面中的其他控件设置模板中文本框的默认值,c#,asp.net,C#,Asp.net,我有一个详细视图,我希望其中一个字段的默认值取自不同的标签,而这个标签又取自上一页 视图的代码为: <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="gkey" DataSourceID="SqlDataSourceEmployees" Height="50px" Width="125px" OnLoad="DetailsView1_Load">

我有一个详细视图,我希望其中一个字段的默认值取自不同的标签,而这个标签又取自上一页

视图的代码为:

 <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="gkey" DataSourceID="SqlDataSourceEmployees" Height="50px" Width="125px" OnLoad="DetailsView1_Load">
                <Fields>
                    <asp:BoundField DataField="gkey" HeaderText="gkey" InsertVisible="False" ReadOnly="True" SortExpression="gkey" />
                    <asp:TemplateField HeaderText="Department" SortExpression="Department">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Department") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Department") %>'></asp:TextBox>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                    <asp:BoundField DataField="SIP" HeaderText="SIP" SortExpression="SIP" />
                    <asp:BoundField DataField="color" HeaderText="color" SortExpression="color" />
                    <asp:CheckBoxField DataField="on_call" HeaderText="on_call" SortExpression="on_call" />
                    <asp:CheckBoxField DataField="on_leave" HeaderText="on_leave" SortExpression="on_leave" />
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
                </Fields>
            </asp:DetailsView>
但这给了我一个NullReferenceException。我尝试将同一行放在DetailsVie1\u加载中,但这会产生相同的错误。我错过了什么

编辑:我正在正确地传输上一页的数据,我刚刚跳过了该部分,因为它不相关,但由于您坚持:

protected void Page_Load(object sender, EventArgs e)
{

    Page previousPage = Page.PreviousPage;
    if (previousPage != null)
    {

        Label tst = FindControlRecursive(previousPage, "lblDepartment") as Label;
        LabelSelectedDepartament.Text = tst.Text;

        //And here is error
       ((TextBox)DetailsView1.FindControl("TextBox1")).Text = LabelSelectedDepartament.Text;

    }


}

//This fantastic procedure below allows to find controls within controls (i.e. label inside the table)

private Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
    {
        return root;
    }
    foreach (Control c in root.Controls)
    {
        Control t = FindControlRecursive(c, id);
        if (t != null)
        {
            return t;
        }
    }
    return null;
}
Edit2:我不想在上一页中查找值,它已经在当前页上,并且显示正确,所以我需要的是

TextBox1.Text=LabelSelectedDepartament.Text
编辑3

我已尝试将TextBoxDetailsView1.FindControlTextBox1.Text=LabelSelectedDepartment.Text;进入

详细信息查看1_数据绑定 模式改变 项目插入
我想我在某种程度上解决了这个问题。

您可以在DetailsView的数据绑定事件中访问TextBox1。在数据绑定之前,在该控件中找不到任何内容。

uhh,我正确地传递了值,我从代码中省略了它,因为它是不相关的。您有多个ID=TextBox1的控件。奇怪的是,这是由向导在其带模板的开箱即用详细信息视图上方生成的,因为它们不同时存在。更改它们的名称并不能解决我所期望的问题,但不幸的是,我仍然得到NullReferenceException-对象引用未设置为对象的实例。我相信这一定是另一个事件,因为在数据绑定上TexBox1不存在,它只显示在编辑上
TextBox1.Text=LabelSelectedDepartament.Text