简体   繁体   English

如何使用C#从WebForm中的禁用文本框中获取值

[英]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.

相关问题 如何在ASP.NET C#WebForm中的文本框和下拉列表的GridView页脚行中获取值 - How Can I get the value in the gridview footer row of textbox and dropdownlist in asp.net c# webform 如何从占位符获取所有文本框? asp.net c#网络表格 - How to get all textbox from a placeholder? asp.net c# webform 如何在C#中将数据从文本框传递到同一Web窗体中的标签? - How to pass data from a textbox to a label in the same webform in c#? 使用C#从母版页获取文本框值 - Get Textbox Value from Masterpage using c# 我如何从 C# 中的字符串文本框获取值 - How do i get value from string textBox in C# 如何从使用JavaScript动态添加的文本框中获取价值-C# - How to get value from textbox dynamically added with javascript - c# 我如何通过文本框C#WebForm将参数传递给Crystal报表中的子报表 - How do i pass parameter to subreports in crystal report from textbox c# webform (webform)如何从9个不同的文本框中获取值并回传值,然后升序 - (webform) how can i get the value from 9 different textbox and post back the value follow by ascending 如何获取动态文本框值 c#? - How to get dynamic textbox value c#? 如何检查文本框是否在 selenium C# 中被禁用 - How to check if textbox is disabled in selenium C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM