GridView控件的问题

时间:2011-07-02 07:58:04

标签: asp.net sql controls

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
    <asp:GridView ID="GridView1" runat="server" Height="464px" Width="743px" 
        AutoGenerateColumns="False" EmptyDataText="No Rows Selected" 
        DataSourceID="Answered" 

        style="z-index: 1; left: 327px; top: 334px; position: absolute; height: 464px; width: 743px">
        <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>
  </div>
</asp:Content>

代码文件:

protected void Page_Load(object sender, EventArgs e)
{
    MembershipUser CurrentUser = Membership.GetUser();
    Guid userid = (Guid)CurrentUser.ProviderUserKey;
    Answered.SelectParameters.Add("UserID", userid.ToString().Trim());
}

当我修改网格的数据源时,我会编写查询并使用特定网格对其进行测试,它会给出预期的结果。但是当我运行网格视图时,我得到的只是“空行”。那是为什么?!?

1 个答案:

答案 0 :(得分:2)

我的猜测是:在Page_Load中,您经常向SQL数据源添加新参数,即使该参数已经定义。

请尝试此操作 - 设置现有参数的值:

protected void Page_Load(object sender, EventArgs e)
{
   MembershipUser CurrentUser = Membership.GetUser();
   Guid userid = (Guid)CurrentUser.ProviderUserKey;
   Answered.SelectParameters["UserID"].DefaultValue = userid.ToString().Trim();
}

这会改变什么吗?