文本框未显示在页面上
本文关键字:显示 文本 | 更新日期: 2023-09-27 18:37:18
我正在尝试在从填充的列表框中选择一个项目并单击按钮后显示文本框。单击按钮后,文本框应以所需的文本显示在屏幕上,但它永远不会出现。visible 属性的初始值设置为 false,然后在"代码隐藏"中将其设置为 true 并填充文本框中的文本。我调试了代码,可见和文本的属性肯定会更新,但我不知道问题是什么。
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();
}
我目前在页面加载中没有做太多事情。我只是在页面加载时隐藏列表视图,直到我选择其他值。
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 时,它都会将所有内容重置为其初始状态。 检查是否是回发。
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;
}
}
}