2010-04-09 83 views
0

我想從我從數據庫根據從URL傳遞的ID從表中的數據。不過,我總是從id = 1獲取數據?爲什麼?僅供參考,我直接從ClubWebsite入門套件中獲取此代碼,並將其複製並粘貼到我的項目中以進行一些更改,Club網站上的一項工作正常..但這個人沒有也找不到任何理由,因爲他們都看起來完全一樣一樣。問題sqldatasource和數據綁定

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

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="splash" Runat="Server"> 
<div id="splash4">&nbsp;</div> 
</asp:Content> 
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
<div id="content"> 
    <div class="post">     
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubDatabase %>" 
       SelectCommand="SELECT dbo.Events.id, dbo.Events.starttime, dbo.events.endtime, dbo.Events.title, dbo.Events.description, dbo.Events.staticURL, dbo.Events.address FROM dbo.Events"> 
       <SelectParameters> 
        <asp:Parameter Type="Int32" DefaultValue="1" Name="id"></asp:Parameter> 
       </SelectParameters> 
      </asp:SqlDataSource> 

      <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="id" AllowPaging="false" Width="100%"> 
        <ItemTemplate> 
         <h2> 
          <asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" /> 
         </h2> 
         <div> 
          <br /> 
          <p> 
           <asp:Label Text='<%# Eval("address") %>' runat="server" ID="addressLabel" /> 
          </p> 
          <p> 
           <asp:Label Text='<%# Eval("starttime","{0:D}") %>' runat="server" ID="itemdateLabel" /> 
           <br /> 
           <asp:Label Text='<%# ShowDuration(Eval("starttime"),Eval("endtime")) %>' runat="server" ID="Label1" /> 
          </p> 
         </div> 

         <p> 
          <asp:Label Text='<%# Eval("description") %>' runat="server" ID="descriptionLabel" /> 
         </p> 

        </ItemTemplate> 
       </asp:FormView> 
       <div class="dashedline"> 
       </div> 
    </div> 
</div> 
</asp:Content> 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Data; 

public partial class Events_View : System.Web.UI.Page 
{ 

    const int INVALIDID = -1; 

    protected void Page_Load(object sender, System.EventArgs e) 
    { 
     SqlDataSource1.SelectParameters["id"].DefaultValue = System.Convert.ToString(EventID); 
    } 

    public int EventID 
    { 
     get 
     { 
      int m_EventID; 
      object id = ViewState["EventID"]; 
      if (id != null) 
      { 
       m_EventID = (int)id; 
      } 
      else 
      { 
       id = Request.QueryString["EventID"]; 
       if (id != null) 
       { 
        m_EventID = System.Convert.ToInt32(id); 
       } 
       else 
       { 
        m_EventID = 1; 
       } 
       ViewState["EventID"] = m_EventID; 
      } 
      return m_EventID; 
     } 
     set 
     { 
      ViewState["EventID"] = value; 
     } 
    } 


    protected void FormView1_DataBound(object sender, System.EventArgs e) 
    { 
     DataRowView view = (DataRowView)(FormView1.DataItem); 
     object o = view["staticURL"]; 
     if (o != null && o != DBNull.Value) 
     { 
      string staticurl = (string)o; 
      if (staticurl != "") 
      { 
       Response.Redirect(staticurl); 
      } 
     } 
    } 

    protected string ShowLocationLink(object locationname, object id) 
    { 
     if (id != null && id != DBNull.Value) 
     { 
      return "At <a href='Locations_view.aspx?LocationID=" + Convert.ToString(id) + "'>" + (string)locationname + "</a><br/>"; 
     } 
     else 
     { 
      return ""; 
     } 
    } 

    protected string ShowDuration(object starttime, object endtime) 
    { 
     DateTime starttimeDT = (DateTime)starttime; 
     if (endtime != null && endtime != DBNull.Value) 
     { 
      DateTime endtimeDT = (DateTime)endtime; 
      if (starttimeDT.Date == endtimeDT.Date) 
      { 
       if (starttimeDT == endtimeDT) 
       { 
        return starttimeDT.ToString("h:mm tt"); 
       } 
       else 
       { 
        return starttimeDT.ToString("h:mm tt") + " - " + endtimeDT.ToString("h:mm tt"); 
       } 
      } 
      else 
      { 
       return "thru " + endtimeDT.ToString("M/d/yy"); 
      } 
     } 
     else 
     { 
      return starttimeDT.ToString("h:mm tt"); 
     } 
    } 

} 
+0

調試呢?我相信你只需要調試就能搞清楚。如果沒有,至少您可以與我們分享調試結果,我們可能會幫助您更好。 – 2010-04-09 01:23:28

回答

2

請檢查您的SelectCommand。它應該是:

SelectCommand="SELECT id, starttime, endtime, title, description, staticURL, address FROM dbo.Events WHERE [email protected]" 

HTH

+0

我認爲OP實際上希望看到的不止是'id = 1'行,所以他需要刪除這些參數並使用查詢字符串參數。 – BigChrisDiD 2010-04-09 01:31:50

+0

我不明白爲什麼我給這個-1 :-(Where Where子句丟失了,這是正確的答案。 – Raja 2010-04-09 01:34:16

0

看到選擇的參數(不選擇命令)爲數據源?它指定你只能得到id = 1的行。

刪除該部分。

更新:實際上,它會將其更改爲查詢字符串參數,並在您的網址中引用該ID。然後將參數引用添加到您的選擇命令中。