使用C#后端代码检查隐藏或不隐藏的html字段

时间:2016-02-22 07:38:06

标签: c# asp.net .net

我有以下代码,其中我显示并隐藏基于下拉列表选择值的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>

这是我在Backend尝试的代码,但它的返回始终适用于所有字段

  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
    }

2 个答案:

答案 0 :(得分:0)

您无法在后端实际检查此内容。 .show()和.hide()是jQuery方法,只在客户端运行。

一种解决方法可能是使用带有knockout.js的MVVM模式(不要试试角度,你会在文档中丢失)

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

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

最后,您可以放置​​一个隐藏字段,该字段将在提交时填充,并为您输入“隐藏,显示等”值。

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

答案 1 :(得分:0)

是的,Jurion是对的。只需添加更多解释:您可以尝试

tr_CompileFile.Style["HTML Style key here"]
例如,您可以尝试

tr_CompileFile.Style["display"]

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

如果你定义了

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

检查时,

btnTest.Style["display"]

将返回“none”