文本框未显示在页面上

Textbox not showing on page

我试图在 select 从填充的列表框中输入一个项目并单击一个按钮后显示一个文本框。 单击按钮后,文本框应显示在屏幕上,其中包含所需的文本,但它从未出现过。 visible 属性 的初始值设置为false,然后在Code Behind 中将其设置为true 并在文本框中填充文本。我调试了代码,visible和text的属性肯定在更新,但我不知道是什么问题。

ASP.net

<asp:FormView ID="FormSectionFormView" runat="server" DataKeyNames="FormSectionID" DataSourceID="FormSectionDataSource" RowStyle-VerticalAlign="NotSet">
<ItemTemplate>
    <tr>
        <td>
            <asp:Button ID="InsertButton"
            runat="server"
            Text="Insert" 
            OnClick="FormSectionButton_Click" 
            Font-Size="1.2em" />
            <asp:Button ID="UpdateButton"
            runat="server"
            Text="Update"
            Font-Size="1.2em"/>              
        </td>
        <td align="center">
            <div style: align="center">
                <asp:Label ID="Label1"
                runat="server"
                Font-Bold="true"
                Text="Section Instruction"
                Font-Size="1.2em">
                    </asp:Label>
            </div>
            <div style="width:800px; height:auto; overflow:auto">
                <asp:ListBox ID="SectionInstructionListBox"
                DataSourceID="SectionInstructionSource"
                runat="server"
                DataTextField="Instruction"
                Visible="True" />
            </div>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <div style="padding-top: 4em; width:800px">
                <asp:Label ID="Label5"
                runat="server"
                Font-Bold="true"
                Text="Insert New Instruction"
                Font-Size="1.2em">
                </asp:Label>              
                <asp:TextBox ID="SectionInstructionTextBox"
                runat="server"
                Width="800px" />
            </div>
       </td>       
   </tr>
   <tr>
       <td>

       </td>
       <td>
           <asp:TextBox runat="server" ID="updatetextbox" AutoPostBack="True" Visible="False"></asp:TextBox>
       </td>
   </tr>
</ItemTemplate>
</asp:FormView>   

代码隐藏

protected void FormSectionUpdateButton_Click(object sender, EventArgs e)
{
   var ctrl = (Control)sender;
   var updatetextbox = (TextBox)ctrl.FindControl("updatetextbox");
   var instructionlistbox = (ListBox)ctrl.FindControl("SectionInstructionListBox");
   updatetextbox.Visible = true;
   updatetextbox.Text = instructionlistbox.SelectedItem.Text;
   FormSectionListView.DataBind();
}

我目前在页面加载方面做的不多。我只是在页面加载时隐藏列表视图,直到我 select 其他值。

protected void Page_Load(object sender, EventArgs e)
{
    _connection = DataAccess.SelfRef().GetConnection();

    var list = InstructionDropDown.SelectedValue;
    switch (list)
    {

        case "Form Section":
            FormSectionListBox.DataSourceID = "FormSectionDataSource";
            FormSectionListView.DataBind();
            RenderView(FormSectionListView, "hidden"); // hide listview on page load
            break;

    }

}

您要确保没有在回发时重新绑定 FormView。每次绑定 FormView 时,它都会将所有内容重置为初始状态。检查它是否是 post 返回。

protected void Page_Load(object sender, EventArgs e)
{
    _connection = DataAccess.SelfRef().GetConnection();

    if ( !Page.IsPostBack ) 
    {

        var list = InstructionDropDown.SelectedValue;
        switch (list)
        {

        case "Form Section":
            FormSectionListBox.DataSourceID = "FormSectionDataSource";
            FormSectionListView.DataBind();
           RenderView(FormSectionListView, "hidden"); // hide listview on page load
            break;

        }
    }

}