【问题标题】:Validating Entries in an ASP.NET GridView without JavaScript在没有 JavaScript 的情况下验证 ASP.NET GridView 中的条目
【发布时间】:2010-12-23 09:26:39
【问题描述】:

背景

我有一个GridView,其中Column1 中的输入取决于输入Column2。

  • 如果用户在Column2 中输入N,系统将在Column1 中输入Y。

验证是使用Regexs 和自定义验证实现的。我更喜欢不使用 JavaScript 的验证解决方案。

|第1栏|栏目2| __________________ |是 | ñ __________________ |N |是 __________________ |N | ñ

问题

如何在不使用 JavaScript 的情况下验证 Gridview 中的这些条目?

【问题讨论】:

  • 你为什么不想使用javascript?从技术上讲,如果您使用 asp.net、gridview 和任何回发行为,您将使用 javascript。

标签: c# .net asp.net validation gridview


【解决方案1】:

为什么不使用两个单选按钮?

<asp:templatefield>
  <itemtemplate>
    <asp:radiobutton id="rbYes" runat="server" groupname="YesNo" text="Yes" />
    <asp:radiobutton id="rbNo" runat="server" groupname="YesNo" text="No" />
  </itemtemplate>
</asp:templatefield>

【讨论】:

    【解决方案2】:

    您可以使用单选按钮和 GridView 的“模板”列功能。 GridView 标记如下所示:

        <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false" 
            OnRowDataBound="gvTest_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Column 1">
                    <ItemTemplate>
                        <asp:RadioButton ID="rbSelect1" runat="server" Text="" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Column 2">
                    <ItemTemplate>
                        <asp:RadioButton ID="rbSelect2" runat="server" Text="" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    

    接下来的诀窍是正确设置每个单选按钮的“GroupName”属性,以便浏览器将生成的网格的每一行视为单个单选按钮组。这就是网格上指定的“OnRowDataBound”处理程序发挥作用的地方。 'gvTest_RowDataBound' 处理程序方法的定义可能类似于:

    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButton rb1 = (RadioButton)e.Row.FindControl("rbSelect1");
            RadioButton rb2 = (RadioButton)e.Row.FindControl("rbSelect2");
            rb1.GroupName = rb2.GroupName = string.Format("Select_{0}", e.Row.RowIndex);
        }
    }
    

    通过将行索引附加到每行中的两个单选按钮的组名称中,您将确保浏览器将它们视为一个组,并且每行只允许选择一个值。结果看起来像这样:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 2016-02-19
      • 2011-04-18
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多