无法处理同一表单div内的输入字段

时间:2014-04-10 05:20:10

标签: c# asp.net forms html

我的ASPX页面中有一个表单,由c#后面的代码处理。 它工作正常,直到我需要根据用户需求hideshow某些输入。 这部分也很好用,但是当我输入div时,我的c#就不能再获得表单值了。 这是我的代码模拟:

<form id="form1" runat="server">
   <table width="100%" frame=box>
      <tr>
         <td>
           <div id='firstText' runat="server">
             <table>
               <tr>
                  <td>
                        <input name="INPUT1" id="idInput1" runat="server" type="radio" value="someVal" checked />
                  </td>
               </tr>
             </table>
            </div>
         </td>
      </tr>
      <tr>
         <td>
            <div id='secondText' runat="server">
             <table>
               <tr>
                  <td>
                        <input name="INPUT2" id="idInput2" runat="server" type="radio" value="someVal2" checked />
                  </td>
               </tr>
             </table>
            </div>
         </td>
      </tr>
   </table>
</form>

代码背后:

string selectedOption = Request.Form["INPUT2"];

根据上述情况,selectionOption中的内容始终为null。 但是当我从代码中删除div时,一切都很好。

任何建议都值得赞赏。

2 个答案:

答案 0 :(得分:0)

添加提交按钮时,selectedOption值正确显示。示例代码如下。

Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NestedIdTest.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
   <table width="100%" frame=box>
      <tr>
         <td>
           <div id='firstText' runat="server">
             <table>
               <tr>
                  <td>
                        <input name="INPUT1" id="idInput1" runat="server" type="radio" value="someVal" checked />
                  </td>
               </tr>
             </table>
            </div>
         </td>
      </tr>
      <tr>
         <td>
            <div id='secondText' runat="server">
             <table>
               <tr>
                  <td>
                        <input name="INPUT2" id="idInput2" runat="server" type="radio" value="someVal2" checked />
                  </td>
               </tr>
             </table>
            </div>
         </td>
      </tr>
   </table>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>

Default.aspx.cs(代码隐藏)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NestedIdTest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string selectedOption = Request.Form["INPUT2"];
        }
    }
}

请注意添加到Default.aspx的行

<asp:Button ID="Button1" runat="server" Text="Button" />

答案 1 :(得分:0)

谢谢。

我已经有了一个提交按钮,忘记在模拟代码中提及它。 我已经发现了问题。如果有人遇到这个问题,请详细说明。

在后面的代码的页面加载中我默认隐藏了一个div

secondText.visible = false;

这就是为什么我无法读取其输入值的原因。 实际上,我希望即使组件的值不可见,也可以访问该组件的值。我没有禁用它,只是更改visible属性。

无论如何,学到了新东西! 我在可见的div中创建了一个隐藏的div来保存隐藏的div中我想要的值。因此,即使隐藏了div,我也可以从显示的那个中获得我想要的值。

问候。