传递查询字符串

时间:2010-10-16 16:45:56

标签: asp.net query-string sqldatasource

考虑一个页面,当页面加载时,什么都没有显示。

当我在浏览器上传递查询字符串时,它可以正常工作:

  

http://localhost:51765/foo/foo.aspx?ID=c516f4f4-36a9-40a7-baad-d2419ea631b9

当我在浏览器上传递查询字符串时,我希望它在页面加载时不起作用。

有人可以帮我这个吗?

<asp:SqlDataSource ID="categoriesDataSource" runat="server" 
     connectionString="<%$ ConnectionStrings:ConnectionString %>"
     SelectCommand="SELECT [CategoryID], [Name] FROM [Categories] WHERE ([UserId] = @UserId) ORDER BY [Name]">
     <SelectParameters>
          <asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
     </SelectParameters>
</asp:SqlDataSource>

<asp:DropDownList ID="categories" runat="server" AutoPostBack="True"
       DataSourceID="categoriesDataSource" DataTextField="Name" 
         AppendDataBoundItems="True" DataValueField="CategoryID">
        <asp:ListItem  Value="">-- All Albums --</asp:ListItem>
</asp:DropDownList>

 <asp:SqlDataSource ID="picturesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
      SelectCommand="SELECT [PictureID], [Title], [UploadedOn] FROM [Pictures] WHERE UserId = @UserId AND 
       (CategoryID = @CategoryID Or @CategoryID IS NULL) ORDER BY UploadedOn DESC" 
        CancelSelectOnNullParameter="False">
  <SelectParameters>
      <asp:ControlParameter ControlID="categories" Name="CategoryID" PropertyName="SelectedValue"/>
      <asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
  </SelectParameters>
</asp:SqlDataSource> 


<asp:GridView ID="GridView1" runat="server" DataSourceID="picturesDataSource">
</asp:GridView>

1 个答案:

答案 0 :(得分:2)

如果不显示页面代码或至少解释它的作用,很难回答您的问题。从网址看来,该网页似乎依赖于ID参数并尝试将其解析为Guid。您需要测试是否传递了ID参数,并且仅在这种情况下使用它:

string id = Request["ID"];
if (!string.IsNullOrEmpty(id))
{
    // The ID parameter has been passed => use its value here
}