ASP:Textbox 的值由 Javascript 设置

Value of ASP:Textbox set by Javascript

本文关键字:Javascript 设置 Textbox ASP      更新时间:2023-09-26

我见过其他几个类似的问题,我的似乎有一个奇怪的转折。我有一个时间表 Web 应用程序,员工在表格中提交他们的时间,它以只读形式发送给 HR。然后,HR 可以根据某些条件批准或拒绝其时间表。我认为如果 HR 代表可以单击不正确的单元格并将其变为红色,那就太好了,这样当它被拒绝并发回给员工时,很容易看到他们需要更正的内容。

我用Javascript写了这个小片段,效果很好:

    var validTextArray = {};
    var backgroundColorArray = {};
    function clearTextBox(textBoxID) 
    {
        if (document.getElementById(textBoxID).value != "#ERROR")
        {
            backgroundColorArray[textBoxID] = document.getElementById(textBoxID).style.backgroundColor;
            validTextArray[textBoxID] = document.getElementById(textBoxID).value;
            document.getElementById(textBoxID).value = "#ERROR";
            document.getElementById(textBoxID).style.backgroundColor = "red";
        }
        else if (validTextArray[textBoxID] != null)
        {
            document.getElementById(textBoxID).value = validTextArray[textBoxID];
            document.getElementById(textBoxID).style.backgroundColor = backgroundColorArray[textBoxID];
        }
    }

为了清楚我的 C#,这里是:

            day1PH.Attributes["onclick"] = "clearTextBox(this.id)";
            day1PN.Attributes["onclick"] = "clearTextBox(this.id)";
            day1LV.Attributes["onclick"] = "clearTextBox(this.id)";
            day1TL.Attributes["onclick"] = "clearTextBox(this.id)";
            day2PH.Attributes["onclick"] = "clearTextBox(this.id)";
            day2PN.Attributes["onclick"] = "clearTextBox(this.id)";
            day2LV.Attributes["onclick"] = "clearTextBox(this.id)";
//etc...

这部分工作得很好,问题是我需要能够捕获这些错误。现在,当 H.R. 点击拒绝按钮时,表中的所有数据都将重写为 SQL 数据库,但由于文本是由 javascript 设置的,因此"#ERROR"值不会通过。

我该怎么做才能捕获这些值?

我猜你正在使用网络表单,如果你是,你可以使用一个隐藏字段并设置带有错误的 value 属性,然后当你将表单发布到你的网络服务器时,你可以通过使用 Request.Form["nameOfYourHiddenAttribute"] 来获取这个值。

<input id="hdnErrors" type="hidden" name="error" value="xpto">
string error = Request.Form["error"] + ""; //error is 'xpto'

如果你有多个错误,你可以在 JavaScript 中使用一些字典,最后你可以序列化为 JSON 对象 e 把它放在隐藏值中,然后在服务器端代码中,你可以反序列化和使用它。

像这样的东西(没有测试它)

var arrayObject = [];
var error = {txtId = "txt1", description="HR error"};
arrayObject.push(error);
document.getElementById('hdnErrors').value = JSON.stringify(arrayObject);