【问题标题】:unable to update in gridview when require validation occur in textbox当文本框中需要验证时无法在gridview中更新
【发布时间】:2015-11-04 06:29:29
【问题描述】:

我想通过 update 命令修改 gridview 中的项目,但在同一页面中我还有一个需要验证的文本框。

当文本框控件中发生需要验证时,我无法在 gridview 中更新

这里还有源代码...

 <div class="row">
    <asp:Button ID="Button1" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="Button1_Click"
        ValidationGroup="btn" />
    <asp:Button ID="Button2" runat="server" CssClass="btn btn-primary" Text="Reset" OnClick="Button2_Click"
        CausesValidation="False" />
    <asp:Button ID="Button3" runat="server" CssClass="btn btn-primary" Text="Show" CausesValidation="False"
        OnClick="Button3_Click" />
</div>
</form> </div> </div>
<asp:GridView ID="GridView1" runat="server" CssClass="table-hover table-responsive table-condensed table-bordered"
    AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
    DataSourceID="SqlDataSource1" Visible="False">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
            SortExpression="id" />
        <asp:BoundField DataField="Loc" HeaderText="Location" SortExpression="Loc" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mycon %>"
    SelectCommand="SELECT * FROM [location]" DeleteCommand="Delete From Location Where Id=@id"
    UpdateCommand="update  location set loc=@loc where id=@id">
    <DeleteParameters>
        <asp:ControlParameter ControlID="TextBox1" Name="id" PropertyName="Text" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="loc" />
        <asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue" />
    </UpdateParameters>
</asp:SqlDataSource>

这里有cs代码

protected void Button1_Click(object sender, EventArgs e)
{
    string qry = "insert into location (loc)values(@loc) ";
    SqlConnection con = Connection.Getconnection();
    con.Open();
    SqlCommand cmd = new SqlCommand(qry, con);
    cmd.Parameters.AddWithValue("@loc", TextBox1.Text);
    int x = cmd.ExecuteNonQuery();
    if (x > 0)
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation!", "<script>alert('Successfully Add')</script>");
    }
    else
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation!", "<script>alert('Error')</script>");
    }
}
protected void Button2_Click(object sender, EventArgs e)
{
    TextBox1.Text = string.Empty;
}
protected void Button3_Click(object sender, EventArgs e)
{
    GridView1.Visible = true;
}

【问题讨论】:

  • 你有什么错误吗?
  • 在添加位置文本框上应用验证组。
  • @Webruster 没有收到错误但无法在 gridview 中更新我的项目
  • @KKZone 发布您的代码。您是否使用try catch 来捕获异常?
  • @KKZone 你没有为button1_click 添加try catch。 2.validation group只有在CausesValidation property的值设置为true时才有效。validation group需要指定一个valueCheck this link

标签: asp.net validation gridview updatecommand


【解决方案1】:
  1. 请尝试为 button1_click 添加try catch 以捕获异常。
  2. 只有当 CausesValidation 属性的值设置为 true 时,验证组才有效。并且验证组需要指定一个value 请参考这个Link

【讨论】:

  • @kkzone 我能知道即使在解决了你的问题之后也不接受它作为答案的原因吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多