【问题标题】:c# asp Textbox Text does only change from Code behind when Textbox is readonlyc# asp Textbox Text 仅在 Textbox 为只读时从 Code behind 更改
【发布时间】:2013-04-23 21:54:15
【问题描述】:

我很困惑。 我尝试更改文本框的默认文本。

这里有一个简单的代码来重现问题。

TextBox Textbox1 和标签在任何单击按钮时都会更新。 Textbox var 永远不会更新。仅在第一个 PageLoad 时。

问题是,我必须从代​​码后面添加我的文本框,而不是在 aspx 页面中。

任何想法发生了什么以及如何让它发挥作用?

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="tbtester.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div >

        <asp:Panel ID="mypanel" runat="server">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" />
            </asp:Panel>
    </div>
    </form>
</body>
</html>

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

    namespace tbtester
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                System.Web.UI.WebControls.TextBox var = new System.Web.UI.WebControls.TextBox();
                //if (!this.Page.IsPostBack)
                Label test = new Label();

                mypanel.Controls.Add(test);
                mypanel.Controls.Add(var);

                var.Text = DateTime.Now.ToLongTimeString();
                test.Text = DateTime.Now.ToLongTimeString();

                var.ReadOnly = false;

                TextBox1.Text = DateTime.Now.ToLongTimeString();



            }
        }
    }

【问题讨论】:

    标签: c#


    【解决方案1】:

    禁用其read only 标志before 将其添加到您的panel

         TextBox var = new TextBox();
         var.ReadOnly = false;
         mypanel.Controls.Add(var);
    

    【讨论】:

    • 只读默认是禁用的。该行仅用于调试。当我启用只读时,文本框将更新,但用户无法更改其浏览器中的值。有点疯狂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多