文本框在按钮Click上丢失其值

时间:2014-06-20 13:57:13

标签: c# asp.net

我有一个只读的texbox。该值来自另一个文本框。但是当我单击提交按钮时,文本框的值变为空白。

请参阅文本框的代码:

<asp:TextBox ID="txtTotalAmt" runat="server" class="txtfld-popup2" MaxLength="5"  ReadOnly="true" EnableViewState= "false"></asp:TextBox>

另请参阅页面加载代码和按钮点击:

 protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (radiobtnIsCustomer.Checked && txt_custId.Text == string.Empty)
    {
        div_errorMsg.InnerText = "Please enter your Customer Id.";
        return;
    }
    if (Page.IsValid)
    {
        SendMail();
        Response.Redirect("ThankYou_AgriBusiness.html"); 
    }
    if (!CaptchaControl1.IsValid)
    {
        lblCaptchaError.Text = "Invalid Captcha";
    }
    Customer newCustomer = new Customer();
    newCustomer.IsExistingCustomer = radiobtnIsCustomer.Checked;
    newCustomer.ExistingCustomerId = txt_custId.Text;
}

页面加载代码:

 protected void Page_Load(object sender, EventArgs e)
{
    lblCaptchaError.Text = "";
    div_errorMsg.InnerText = "";
    if (!IsPostBack)
    {
        txtTotalAmt.Attributes.Add("readonly", "readonly");
        conn.Open();
        SqlCommand cmd = new SqlCommand("select * from States_agri", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        ddl_state.DataSource = dr;
        ddl_state.Items.Clear();
        ddl_state.DataTextField = "StateName";
        ddl_state.DataValueField = "StateID";
        ddl_state.DataBind();
        ddl_state.Items.Insert(0, new ListItem("--Select State--", "0"));
        conn.Close();

        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        rm = new ResourceManager("Resources.Strings", System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadString(ci); 
    }
}

1 个答案:

答案 0 :(得分:-1)

问题在于ReadOnly属性。不要这样做。

ReadOnly="true" EnableViewState= "false"

如果您想要使用户无法更改客户端上的文本,您可以在文本框中添加一个html属性,以便在页面加载中将其标记为只读。

txtTotalAmt.Attributes.Add("readonly", "readonly")
相关问题