检查 html 字段是否隐藏是否使用 C# 后端代码

本文关键字:是否 后端 代码 隐藏 html 字段 检查 | 更新日期: 2023-09-27 18:35:42

我有以下代码,其中我根据下拉列表选定值显示和隐藏html控件。显示和隐藏工作正常,但我想在后端检查,即 C# 哪个<tr>可见,哪个隐藏

这是我的 aspx 页面代码

 <script type="text/javascript">
         $(document).ready(function () {
             $("#AttachemntType").change(function () {
                 if ($(this).val() == "F") {
                     $("#tr_CompileFile").show();
                     $("#tr_SourceFile").show();
                 }
                 else if ($(this).val() == "R") {
                     $("#tr_CompileFile").hide();
                     $("#tr_SourceFile").show();
                 }
                 else {
                     $("#tr_SourceFile").hide();
                     $("#tr_CompileFile").hide();
                 }
             });
         });
    </script>
                 <tr bgcolor="white">
                    <td style="height: 20px">
                        Attachment Type
                    </td>
                    <td>
             <select id="AttachemntType" name="AttachemntType" style="width: 344px">
                <option value="0">Select</option>
                <option value="F">Form</option>
                <option value="R">Report</option>
            </select>  
                    </td>
                </tr>
                <tr bgcolor="white" id="tr_SourceFile" style="display:none;" runat="server">
                    <td style="height: 20px">
                        Source File
                    </td>
                    <td>
                        <input class="body_text" type="file" id="src_File" name="src_File" runat="server"
                            style="width: 420px" />
                    </td>
                </tr>

                  <tr bgcolor="white" id="tr_CompileFile" style="display:none;" runat="server"> 
                    <td style="height: 20px">
                        Compiled File
                    </td>
                    <td style="height: 16px; width: 625px;">
                        <input class="body_text" type="file" id="comp_File" runat="server" style="width: 420px" />
                    </td>
                </tr>

这是我在后端尝试的代码,但它对所有字段的返回始终为 true

  if (tr_CompileFile.Visible == true && tr_SourceFile.Visible == true)
       {
    //This Condition is always true
    }
     else if (tr_SourceFile.Visible == true && tr_CompileFile.Visible == false)
     {
    //something
    }
    else
    {
    //something else
    }

检查 html 字段是否隐藏是否使用 C# 后端代码

你实际上无法在后端检查这一点。 .show() 和 .hide() 是只在客户端操作的 jQuery 方法。

一种解决方法可能是使用带有挖空的 MVVM 模式.js(不要为此尝试角度,您将迷失在文档中)

另一种选择是实际将选择框和隐藏/可见的部分放入UpdatePanel中。因此,当您更改选择器时,将调用服务器端,您可以分配/删除"可见"字段。

更多 : https://msdn.microsoft.com/en-us/library/bb399001.aspx

在最后的手段中,您可以放置一个隐藏字段,该字段将在提交时填充"隐藏,显示等"值供您输入。

就个人而言,我会投票给第一个,但第二个是最容易实现的

是的,朱里昂是对的。 只需添加更多说明:您可以尝试

tr_CompileFile.Style["HTML Style key here"]

例如,您可以尝试

tr_CompileFile.Style["display"]

但它将返回 ASPX 文件中定义的特定样式的值,而不是客户端显示和更改的实际样式。

如果您定义了

<asp:Button ID="btnTest" runat="server" style="display:none" />

当你检查它时,

btnTest.Style["display"]

将返回"无"