我正在尝试根据从URL传递的id从数据库中提取数据。但是我总是从id = 1获取数据?为什么?仅供参考我直接从ClubWebsite入门套件中获取此代码,并将其复制并粘贴到我的项目中进行一些更改,ClubWebsite一个正常工作..但是这个没有,也找不到任何理由,因为它们看起来都是同样的。
<%@ 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"> </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 :(得分:2)
请检查您的SelectCommand。它应该是:
SelectCommand="SELECT id, starttime, endtime, title, description, staticURL, address FROM dbo.Events WHERE id=@id"
HTH
答案 1 :(得分:0)
请参阅数据源的“选择参数”(非选择命令)?它指定只获取id = 1的行。
删除该部分。
更新:哎呀,实际上,将其更改为查询字符串参数并在那里引用您网址中的ID。然后将参数引用添加到Select命令中。