.NET GridView 更新不会将 UserID 发送到 SQL 查询
我目前正在尝试学习如何使用基本的网格视图控件来显示我的数据。
首先,我尝试通过 aspx 页面完成所有操作,以使事情对自己来说更简单。
到目前为止,一切都运行良好,但我在使用更新查询时遇到问题。
我想要的结果只是一个简单的两列表,它将显示用户名,然后显示编辑按钮。
问题是,如果我不在列中包含用户 ID,则查询不会更新任何内容。
这是我的 SqlDataSource 代码:
<asp:SqlDataSource
ID="sqlDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:conn %>"
SelectCommand="SELECT UserID, Name FROM tblUsers;"
UpdateCommand="UPDATE tblUsers SET Name = @Name WHERE UserID = @UserID">
<UpdateParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="UserID" />
</UpdateParameters>
</asp:SqlDataSource>
这是我的 GridView:
<asp:GridView
ID="Clients"
DataSourceID="sqlDataSource"
runat="server"
AllowPaging="true"
AllowSorting="true"
PageSize="25"
AutoGenerateColumns="False"
DataKeyNames="UserID">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Users Name" SortExpression="Name" />
<asp:CommandField EditText="Edit" ShowEditButton="true" />
</Columns>
</asp:GridView>
当我转到该页面并单击编辑时,名称字段将更改为文本框,我可以按预期选择更新或取消。
但是使用上面的代码,当我单击“更新”时,它只会再次显示普通表,而不会更改数据,因为 SQL 只是没有获取任何 UserID。
但是,如果我将
添加到
,它作品。
有什么方法可以让我在不必将 UserID 列添加到表中的情况下完成这项工作吗?
I am at the moment trying to learn how to use basic grid view controls to display my data.
To start with I have tried to do everything through the aspx page to try and keep things simpler for myself.
Everything is working great so far, but I am having a problem with using an update query.
My desired result is just a simple 2 column table, which will display a users name, and then an edit button.
The problem is, that if I don't include the user ID in the columns the query does not update anything.
This is my SqlDataSource code:
<asp:SqlDataSource
ID="sqlDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:conn %>"
SelectCommand="SELECT UserID, Name FROM tblUsers;"
UpdateCommand="UPDATE tblUsers SET Name = @Name WHERE UserID = @UserID">
<UpdateParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="UserID" />
</UpdateParameters>
</asp:SqlDataSource>
And this is my GridView:
<asp:GridView
ID="Clients"
DataSourceID="sqlDataSource"
runat="server"
AllowPaging="true"
AllowSorting="true"
PageSize="25"
AutoGenerateColumns="False"
DataKeyNames="UserID">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Users Name" SortExpression="Name" />
<asp:CommandField EditText="Edit" ShowEditButton="true" />
</Columns>
</asp:GridView>
When I go to the page and click edit, the Name field changes to a text box, and I can choose either update or cancel as expected.
But with the code above, when I click update it just displays the normal table again, with no changes to the data because SQL just isn't getting any UserID.
However if I add <asp:BoundField DataField="UserID" HeaderText="Users ID" SortExpression="UserID" />
to the <Columns>
, it works.
Is there any way for me to make this work without having to add the UserID column to the table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 OldValuesParameterFormatString:
You can use the OldValuesParameterFormatString:
添加列并将
Visible
设置为False
。Add the column and set
Visible
toFalse
.