[英]How to get the value from a disabled textbox in WebForm using C#
I am creating a WebForm in which I have given a functionality of enabling and disabling a text box. 我正在创建一个WebForm,其中提供了启用和禁用文本框的功能。 However, when I enter the value in the TextBox and then disable it. 但是,当我在TextBox中输入该值然后将其禁用时。 I am not able to save the TextBox value in the database. 我无法将TextBox值保存在数据库中。 Whenever the code try to save the value, the gets an empty value in it and therefore it stores an empty value. 每当代码尝试保存该值时,都会在其中获取一个空值,因此它将存储一个空值。 When I do no disable the TextBox, then the value get successfully stored in the table. 当我不禁用TextBox时,该值将成功存储在表中。 Below is some code related to it... 以下是与此相关的一些代码...
protected void btnCreateSubAcct_Click(object sender, EventArgs e)
{
int subAccountID = 0;
try
{
if (Page.IsValid)
{
subAccountID = SaveUpdateSubAccount();
if (string.IsNullOrEmpty(HiddenFieldSubAccntID.Value))
{
SessionHandler.Set(SessionHandler.SubAccountIDKey, subAccountID);
Response.Redirect("~/newaccountwelcome");
}
else
{
lblDialogMessage.Text = "Subaccount updated successfully!";
modalPage.Visible = true;
SetFocus(btnOK.ClientID);
}
}
}
catch (ThreadAbortException)
{
}
catch (Exception ex)
{
_log.Error("Error Occured While Creation of SubAccount.", ex);
throw;
}
}
private int SaveUpdateSubAccount()
{
SubAccount objSubAccount = null;
if (!string.IsNullOrEmpty(HiddenFieldSubAccntID.Value) && !HiddenFieldSubAccntID.Value.Equals("0"))
{
objSubAccount = SubAccount.GetSubAccount(Convert.ToInt32(HiddenFieldSubAccntID.Value));
}
if (objSubAccount == null)
{
objSubAccount = new SubAccount();
objSubAccount.CreatedByUser = SessionHandler.CurrentUser.UserID;
}
objSubAccount.FBO1FirstName = txtFBO1FirstName.Text.Trim();
objSubAccount.FBO1MiddleInitial = txtFBO1MiddleName.Text.Trim();
objSubAccount.FBO1LastName = txtFBO1LastName.Text.Trim();
objSubAccount.FBO1AccountTitling = txtFbo1AccountTitling.Text.Trim();
objSubAccount.FBO1TaxIDType = fbo1RadioButtonList.SelectedItem.Value;
objSubAccount.FBO1TaxID = Helpers.GetUnFormattedTIN(txtFbo1TaxId.Text.Trim());
}
//under the class SubAccount, the information is getting stored in the database
Whenever, I try to save the value of a text FBO1TaxID, when it is in disabled mode, the value under this TextBox is empty. 每当我尝试保存文本FBO1TaxID的值时,当它处于禁用模式时,此TextBox下的值都是空的。
How will I save the value of a TextBox in disabled mode under the table in Database? 如何在禁用模式下将“文本框”的值保存在数据库表中? Please help! 请帮忙!
The issue is, is ASP does not store the values of disabled elements in the view state. 问题是,ASP不在视图状态下存储禁用元素的值。 You should have a better chance at setting the textbox as read-only. 您应该有更大的机会将文本框设置为只读。
what you can do is: add a hidden field, when disable the textbox, set its value to the hidden field. 您可以做的是:添加一个隐藏字段,当禁用文本框时,将其值设置为隐藏字段。 then when you need the textbox value, check whether it is NullOrEmpty first. 然后在需要文本框值时,首先检查它是否为NullOrEmpty。 If no, yes textbox's value. 如果否,则为文本框的值。 otherwise, use the hidden field's value. 否则,请使用隐藏字段的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.