gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:00:22 25 4
gpt4 key购买 nike

最近几天我开始使用 asp.net,所以我遇到了一些困难,所以现在我遇到了第一个问题,我无法配置它。我的问题是我有一个网络表单,其中有两个字段名称以及该按钮下方和 GridView 下方的说明。

我已经在字段上应用了必需的字段验证所以问题是当我单击数据添加到数据库并在 GridView 中显示编辑和删除 GridView 的功能但是当我单击更新时它没有进行更新并且由于必需的字段验证器应用于领域所以
这是我的问题,我还附上了我的代码以及您可以获取的代码。

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

最佳答案

您想向添加字段和按钮添加验证组,这样当您从 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

关于c# - 有关更新行数据不起作用的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21732521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com