ASP.NET新手。
我使用向导创建了一个sqldatasource并设置了基本选择查询(SELECT * FROM Accounts)。然后我让sqldatasource向导创建INSERT,EDIT和DELETE查询。将此数据源连接到gridview,并启用EDITING和DELETING。一切正常。 SELECT查询返回所有记录,我可以编辑/删除它们。
现在我需要向SELECT命令发送一个参数,以便将记录过滤到具有用户id的记录(从Membership.GetUser中提取)。当我添加此参数时,SELECT命令工作正常,但gridview中的EDIT / DELETE按钮不再起作用。
未生成错误。页面刷新但记录未在数据库中更新。我不明白出了什么问题。
CODE:
<%
Dim u As MembershipUser
Dim userid As String
u = Membership.GetUser(User.Identity.Name)
userid = u.ProviderUserKey.ToString
SqlDataSource1.SelectParameters("UserId").DefaultValue = userid
%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
<asp:BoundField DataField="AccountName" HeaderText="AccountName"
SortExpression="AccountName" />
<asp:BoundField DataField="DateAdded" HeaderText="DateAdded"
SortExpression="DateAdded" />
<asp:BoundField DataField="LastModified" HeaderText="LastModified"
SortExpression="LastModified" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CheckingConnectionString %>"
DeleteCommand="DELETE FROM [Accounts] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [Accounts] ([UserId], [AccountName], [DateAdded], [LastModified]) VALUES (@UserId, @AccountName, @DateAdded, @LastModified)"
SelectCommand="SELECT * FROM [Accounts] WHERE [UserId] = @UserId"
UpdateCommand="UPDATE [Accounts] SET [UserId] = @UserId, [AccountName] = @AccountName, [DateAdded] = @DateAdded, [LastModified] = @LastModified WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="UserId" Type="String" />
<asp:Parameter Name="AccountName" Type="String" />
<asp:Parameter Name="DateAdded" Type="DateTime" />
<asp:Parameter Name="LastModified" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="UserId" Type="String" />
<asp:Parameter Name="AccountName" Type="String" />
<asp:Parameter Name="DateAdded" Type="DateTime" />
<asp:Parameter Name="LastModified" Type="DateTime" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:Parameter Name="UserId"/>
</SelectParameters>
</asp:SqlDataSource>
答案 0 :(得分:0)
您确认记录仍在数据库中吗?并且这不仅仅是Grid在编辑,更新或删除后没有完成DataBind?这让我不止一次!
另请查看您的代码: SqlDataSource1.SelectParameters(“UserId”)。DefaultValue = userid
您正在设置SelectParameters ...但您也需要设置更新,删除等参数。
答案 1 :(得分:0)
你为什么选择那种方法.getuser? SQL命令行中的简单连接使用语句可以完成这项工作。你不需要一个向导。
答案 2 :(得分:0)
找到答案。我将代码拉动会员提供程序密钥移动到datasource_selecting事件。
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
' Get a reference to the currently logged on user
Dim currentUser As MembershipUser = Membership.GetUser()
' Determine the currently logged on user's UserId value
Dim currentUserId As String = currentUser.ProviderUserKey.ToString()
' Assign the currently logged on user's UserId to the @UserId parameter
e.Command.Parameters("@UserId").Value = currentUserId
End Sub