文本框的文本在提交按钮单击时不会更新

Text of textbox doesn’t update in submit button click

本文关键字:文本 更新 单击 按钮 提交      更新时间:2023-09-26

我有两个单选按钮。如果选中其中一个文本框,则文本框处于活动状态并获取数据。它们在更新页面中。在页面加载中,通过数据库中的数据填充它们,我检查回发。如果文本框在页面加载中有文本,则其文本会更改并且一切正常,但如果它首先没有文本,它将保留默认文本,并且不会在单击提交按钮时更新。

    String val = "0";
    if (radiobutton2.Checked && textbox1.Text.Length != 0)
            val = textbox1.Text;

在页面加载中,使用以下代码初始化单选按钮:

    t.ReadOnly = true;
    t.BackColor = System.Drawing.Color.Gray;

并且有一个用于活动和非活动的JavaScript代码此文本框

    function CheckedChanged(rbtnelement, txtelement) {
                    if (document.getElementById(rbtnelement).checked) {
                        document.getElementById(txtelement).readOnly = false;
                        document.getElementById(txtelement).style.backgroundColor = "white";
                    }
                    else {
                        document.getElementById(txtelement).readOnly = true;
                        document.getElementById(txtelement).style.backgroundColor = "grey";
                    }
                }

这是因为文本框只读属性在 C# 代码中变为 false(它在服务器端运行),而在 JavaScript 中变为 true(它在客户端运行)。在服务器端(c# 代码)文本框保持只读,不发送其文本。

如果删除其中一个并在一侧(客户端或服务器)管理文本框,它将正常工作。