【问题标题】:Validate GridView edit row based on SQL results根据 SQL 结果验证 GridView 编辑行
【发布时间】:2016-09-06 20:20:36
【问题描述】:

对这里的编码相当陌生。我有一个 GridView,用户根据用户名 TextBox 在其中输入员工。它使用 GetUserInfo 方法进行 SQL 查询,并在用户创建或编辑行时自动填充其余的 TextBox 列。如果 SQL 没有找到任何行,我希望出现一条错误消息并且 GridView 不接受该行。本质上是对用户名字段的验证。

我必须以编程方式完成所有操作(受第 3 方 WorkflowGen 软件的限制)。我可以列出我尝试过的那些做作的事情的细节,但我觉得我在这里错过了最好的方法。如何混合验证器(如果需要)、SQL 查询的结果、客户端“无效”消息以及如果无效则拒绝行更新?我想我对太多的移动部件和页面生命周期感到困惑。

<asp:GridView id="COLLAB_GRIDVIEW" showheader="True" showfooter="True" runat="server" autogeneratecolumns="false" onrowdatabound="GridView_RowDataBound">
<Columns><asp:TemplateField><EditItemTemplate><asp:TextBox autopostback="true" ontextchanged="GetUserInfo" value="" id="COLLAB_GRIDVIEW_INTERNET_ID" text="<%# Bind('COLLAB_GRIDVIEW_INTERNET_ID') %>" runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate><asp:Label id="COLLAB_GRIDVIEW_INTERNET_ID" runat="server" text='<%# Eval("COLLAB_GRIDVIEW_INTERNET_ID") %>'></asp:Label></ItemTemplate></asp:TemplateField>
    <asp:TemplateField><EditItemTemplate><asp:TextBox id="COLLAB_GRIDVIEW_FIRST_NAME" text="<%# Bind('COLLAB_GRIDVIEW_FIRST_NAME') %>" runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate><asp:Label id="COLLAB_GRIDVIEW_FIRST_NAME" runat="server" text='<%# Eval("COLLAB_GRIDVIEW_FIRST_NAME") %>'></asp:Label></ItemTemplate></asp:TemplateField>
    <asp:TemplateField><EditItemTemplate><asp:TextBox value="" id="COLLAB_GRIDVIEW_LAST_NAME" text="<%# Bind('COLLAB_GRIDVIEW_LAST_NAME') %>" runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate><asp:Label id="COLLAB_GRIDVIEW_LAST_NAME" runat="server" text='<%# Eval("COLLAB_GRIDVIEW_LAST_NAME") %>'></asp:Label></ItemTemplate></asp:TemplateField>
    <asp:TemplateField><EditItemTemplate><asp:TextBox value="" id="COLLAB_GRIDVIEW_DEPT_NAME" text="<%# Bind('COLLAB_GRIDVIEW_DEPT_NAME') %>" runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate><asp:Label id="COLLAB_GRIDVIEW_DEPT_NAME" runat="server" text='<%# Eval("COLLAB_GRIDVIEW_DEPT_NAME") %>'></asp:Label></ItemTemplate></asp:TemplateField>
    <asp:TemplateField><EditItemTemplate><asp:TextBox value="" id="COLLAB_GRIDVIEW_COLLEGE" text="<%# Bind('COLLAB_GRIDVIEW_COLLEGE') %>" runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate><asp:Label id="COLLAB_GRIDVIEW_COLLEGE" runat="server" text='<%# Eval("COLLAB_GRIDVIEW_COLLEGE") %>'></asp:Label></ItemTemplate></asp:TemplateField>
    <asp:TemplateField><EditItemTemplate><asp:TextBox id="COLLAB_GRIDVIEW_CAMPUS" text="<%# Bind('COLLAB_GRIDVIEW_CAMPUS') %>" runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate><asp:Label id="COLLAB_GRIDVIEW_CAMPUS" runat="server" text='<%# Eval("COLLAB_GRIDVIEW_CAMPUS") %>'></asp:Label></ItemTemplate></asp:TemplateField>
    <asp:CommandField buttontype="Button" showdeletebutton="True" showeditbutton="True" causesvalidation="False" insertvisible="False"></asp:CommandField>
</Columns>

private void GetCollabInfo(object sender, EventArgs e)
{
    TextBox sdr= (TextBox)sender;
    string internetid = sdr.Text.ToString();

    GridViewRow row = (GridViewRow)sdr.NamingContainer;
    int rowIndex = row.RowIndex;

    string conn = ConfigurationManager.ConnectionStrings["Datasource_IGM"].ConnectionString;
    string sql = "SELECT DISTINCT FIRST_NAME, LAST_NAME, PRIMARY_DEPTID_DESCR, UM_RRC_DESCR, CAMPUS_DESCR FROM IGM.PRIMARY_JOB_WITH_TENURE WHERE INTERNET_ID = '" + internetid + "'";

    DataTable dtDemo = SelectStatement(conn, sql);

    if (dtDemo != null && dtDemo.Rows.Count > 0)
    {
        ((TextBox)row.Cells[1].Controls[0]).Text = dtDemo.Rows[0]["FIRST_NAME"].ToString();
    // etc for the rest of the columns
    }
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    只需将 GridView 列的 Behavior 设置为 Required。错误消息将自动生成并准备好在其中一个字段为空时触发。

    您可以使用表单设计器中的行为功能来设置它们。 或者通过修改操作参数“FORM_FIELD_REQUIRED”并添加所需的 GridView 字段的 ID。

    【讨论】:

      猜你喜欢
      • 2011-01-13
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多