I am creating a WebForm in which I have given a functionality of enabling and disabling a text box. However, when I enter the value in the TextBox and then disable it. I am not able to save the TextBox value in the database. 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. 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.
How will I save the value of a TextBox in disabled mode under the table in Database? Please help!
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. If no, yes textbox’s value. otherwise, use the hidden field’s value.