Gridview和传递SQL参数

时间:2011-07-02 07:12:56

标签: asp.net sql gridview

<%@ Page Title="" Language="C#" MasterPageFile="~/CP.master" AutoEventWireup="true" CodeFile="CP_AllQuestions.aspx.cs" Inherits="CP_AllQuestions" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:GridView ID="GridView1" runat="server" Height="464px" Width="932px" 
        AutoGenerateColumns="False" DataSourceID="Answered">
        <Columns>
            <asp:BoundField DataField="ThreadTitle" HeaderText="ThreadTitle" 
                SortExpression="ThreadTitle" />
        </Columns>
</asp:GridView>
    <asp:SqlDataSource ID="Answered" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CP_AllQuestionsAnswered %>" SelectCommand="SELECT ThreadTitle
FROM Threads AS t
INNER JOIN Users AS u ON  t.UsersID=u.UsersID
WHERE t.UsersID=@UserID">
        <SelectParameters>
            <asp:Parameter Name="UserID" />
        </SelectParameters>
    </asp:SqlDataSource>
</asp:Content>

我想在页面加载事件中传递@UserID。

MembershipUser CurrentUser = Membership.GetUser();

 @UserID = (Guid)CurrentUser.ProviderUserKey;

但我该怎么做?

1 个答案:

答案 0 :(得分:1)

MembershipUser CurrentUser = Membership.GetUser();
Guid userid = (Guid)CurrentUser.ProviderUserKey;
Answered.SelectParameters.Add("UserID", userid.ToString().Trim());