有关更新行数据不起作用的功能

时间:2014-02-12 15:41:04

标签: c# asp.net gridview

最近几天我开始使用asp.net所以我面临一些困难,所以现在我遇到了第一个问题,我无法配置它。我的问题是我有一个webform,其中我有两个字段名称并且在一个按钮下方和网格视图下面进行说明。

我在字段上应用了必要的字段验证,所以问题是当我点击数据添加到数据库并在网格视图中显示编辑和删除网格视图的功能但是当我点击更新时它没有进行更新和到期要求现场验证员应用于领域,所以
这是我的问题,我也附上我的代码以及代码可能是你可以抓住。

aspx代码: -

<div id="Organization-Form" class="CssForm">
            <p>Create New Project</p>
            <div id="fields">
                <table width="100%" title="New Project" cellspacing="10">
                    <tr>
                        <td style="text-align: right;">
                            <asp:Label ID="Label1" runat="server" Text="*Name:" Font-Bold="True" CssClass="label"></asp:Label>
                        </td>
                        <td>
                               <asp:TextBox ID="Txt_Input_Name" runat="server" placeholder="Please Enter Name" 
                                   TextMode="SingleLine" CssClass="txt-input-class" Height="20px" 
                                   Width="191px" ToolTip="Please Enter Name"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="Name_Required" runat="server" 
                                                    ControlToValidate="Txt_Input_Name" 
                                                    ErrorMessage="Name is required." 
                                                    ToolTip="Name is required." Font-Bold="True" ForeColor="Red"
                                                    >* Name is required.</asp:RequiredFieldValidator>
                        </td>

                    </tr>
                    <tr>
                        <td style="text-align: right;">
                            <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="*Description:" CssClass="label"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox placeholder="Please Enter Description" CssClass="txt-input-class" ID="Txt_Input_Description" runat="server"
                                TextMode="MultiLine" Height="100px" 
                                   Width="191px" ToolTip="Please Enter Description" MaxLength="250"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="Description_Required" runat="server" 
                                                    ControlToValidate="Txt_Input_Description" 
                                                    ErrorMessage="Description is required." 
                                                    ToolTip="Description is required." Font-Bold="True" ForeColor="Red"
                                                    >* Description is required.</asp:RequiredFieldValidator>
                        </td>                        
                    </tr>
                    <tr>
                        <td colspan="6">
                            <div id="btn-div" style="padding-bottom:20px;">
                                  <asp:Button CssClass="btn" ID="submit_button" Text="Create Project" runat="server" OnClick="submit_button_Click" />
                            </div>
                         </td>
                    </tr> 
                    <tr>
                        <asp:GridView ID="GridView1" CssClass="Grid" runat="server" 
    AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" 
    EmptyDataText="There are no data records to display." AllowPaging="True" BackColor="#CCCCCC" 
                        BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="5" 
                        CellSpacing="5" ForeColor="Black" Width="931px" 
                        PageSize="4">
                        <Columns>
                            <asp:BoundField DataField="Id" HeaderText="Id" Visible="false"
            ReadOnly="True" SortExpression="Id" />
                            <asp:BoundField DataField="Name" HeaderText="Name" 
            SortExpression="Name" />
                            <asp:BoundField DataField="Description" 
            HeaderText="Description" SortExpression="Description" />
                            <asp:CheckBoxField DataField="IsActive" 
            HeaderText="Is Active" SortExpression="IsActive" />
                            <asp:BoundField DataField="CreateDate" 
            HeaderText="Create Date" SortExpression="CreateDate" />
                            <asp:BoundField DataField="ModifyDate" 
            HeaderText="Modify Date" SortExpression="ModifyDate" />
                            <asp:BoundField DataField="CreatedBy" 
            HeaderText="Created By" SortExpression="CreatedBy" />
                            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                        </Columns>

                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ connectionString%>" 
    DeleteCommand="DELETE FROM [Projects] WHERE [Id] = @Id" 
    ProviderName="<%$ ConnectionStrings:LocalSqlServer.ProviderName %>" 
    SelectCommand="SELECT [Id], [Name], [Description], [IsActive], [CreateDate], [ModifyDate], [CreatedBy] FROM [Projects]"
    UpdateCommand="UPDATE [Projects] SET [Name] = @Name, [Description] = @Description, [IsActive] = @IsActive, [CreateDate] = @CreateDate, [ModifyDate] = @ModifyDate, [CreatedBy] = @CreatedBy WHERE [Id] = @Id">
                        <DeleteParameters>
                            <asp:Parameter Name="Id" Type="Int32" />
                        </DeleteParameters>
                        <UpdateParameters>
                            <asp:Parameter Name="Name" Type="String" />
                            <asp:Parameter Name="Description" Type="String" />
                            <asp:Parameter Name="IsActive" Type="Boolean" />
                            <asp:Parameter Name="CreateDate" Type="DateTime" />
                            <asp:Parameter Name="ModifyDate" Type="DateTime" />
                            <asp:Parameter Name="CreatedBy" Type="String" />
                            <asp:Parameter Name="Id" Type="Int32" />
                        </UpdateParameters>
                    </asp:SqlDataSource>
                    </tr>                  
                </table>
            </div>
        </div>

aspx.cs代码: -

   protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void clear()
    {
        Txt_Input_Name.Text = "";
        Txt_Input_Description.Text = "";

    }


    protected void submit_button_Click(object sender, EventArgs e)
    {
        string sqlQuery = "insert fields working well";
        DbObj.ExecuteStringQuery(sqlQuery);
        clear();
        GridView1.DataBind();
    }

这里是你可以更新的图像屏幕截图点击它不更新而是显示必要的字段验证,当我在上面的文本框中添加值时,然后在gridview中更新它的工作。

http://i.imgur.com/UEm5pnW.png

1 个答案:

答案 0 :(得分:0)

您希望在添加字段和按钮中添加验证组,这样当您从GridView进行更新时,它不会触发验证。

作为您的名称验证器的示例,请添加以下内容:

<asp:RequiredFieldValidator ID="Name_Required" runat="server" 
                                                    ControlToValidate="Txt_Input_Name" 
                                                    ErrorMessage="Name is required." 
                                                    ToolTip="Name is required." Font-Bold="True" ForeColor="Red" validationgroup="Add"
                                                    >* Name is required.</asp:RequiredFieldValidator>

将此添加到您的说明中:

<asp:RequiredFieldValidator ID="Description_Required" runat="server" 
                                                    ControlToValidate="Txt_Input_Description" 
                                                    ErrorMessage="Description is required." 
                                                    ToolTip="Description is required." Font-Bold="True" ForeColor="Red" validationgroup="Add" 
                                                    >* Description is required.</asp:RequiredFieldValidator>

最后你的添加按钮:

<asp:Button CssClass="btn" ID="submit_button" Text="Create Project" runat="server" OnClick="submit_button_Click" validationgroup="Add" />

http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx