大家好,因为我是asp.net C的新手#我需要老年人的帮助
有一个包含以下列的表:
ID int unique not null
Title varchar(250) not null
Username varchar(100) not null
Datetime datetime not null
Shortviews varchar(500) not null
Fullviews varchar(1000) not null
Photo image not null
我已经成功编写了用于在此表中插入数据的页面现在我想在页面上显示它,我使用转发器数据控件来显示其标题并将其放在属性中,代码在下面
<asp:Repeater ID="article_rep" runat="server"
onitemcommand="article_rep_ItemCommand">
<itemtemplate>
<ul class="list1">
<li><a href="#"><%# Eval("Title")%></a></li>
</ul>
</itemtemplate>
</asp:Repeater>
在我使用以下代码
选择数据的代码后面protected void Page_Load(object sender, EventArgs e)
{
string strConnString = ConfigurationManager.ConnectionStrings["LgnConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select top 5 Title from table ORDER BY Datetime DESC";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
world_rep.DataSource = reader;
world_rep.DataBind();
con.Close();
}
它显示最后五行的表记录,我希望当我点击任何标题时,它会显示与该标题相关联的其余列信息(我点击),另一个页面将是Details.aspx
我知道这对老年人来说既简单又容易,但是我对此感到震惊,请提前帮助我。我需要在Details.aspx上编写代码以及我需要在Details.aspx.cs上编写代码
答案 0 :(得分:1)
使用以下代码
//Define the class to hold the Tite property values.
public class RepeaterTitle
{
public string Title { get; set; }
}
string strConnString = ConfigurationManager.ConnectionStrings["LgnConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select top 5 Title from table ORDER BY Datetime DESC";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
List<RepeaterTitle> TitleLIst = new List<RepeaterTitle>();
while (reader.Read())
{
RepeaterTitle oTitle = new RepeaterTitle();
oTitle.Title = reader.GetValue(0).ToString();
TitleLIst.Add(oTitle);
}
article_rep.DataSource = TitleLIst;
article_rep.DataBind();
con.Close();
答案 1 :(得分:0)
.cs文件中的组件ID错误。将 world_rep 更改为 article_rep 。其他的东西看起来不错
protected void Page_Load(object sender, EventArgs e)
{
string strConnString = ConfigurationManager.ConnectionStrings["LgnConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select top 5 Title from table ORDER BY Datetime DESC";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
//world_rep.DataSource = reader;
//world_rep.DataBind();
article_rep.DataSource = reader;
article_rep.DataBind();
con.Close();
}
如果您需要详细信息页面,请添加此类链接;
<asp:Repeater ID="article_rep" runat="server"
onitemcommand="article_rep_ItemCommand">
<itemtemplate>
<ul class="list1">
<li><a href="details.asp?id=<%# Eval("ID")%>"><%# Eval("Title")%></a></li>
</ul>
</itemtemplate>
</asp:Repeater>
创建新的详细信息表单。添加&#34;详细信息视图&#34;组件来查看文件。在像这样的.cs文件中;
protected void Page_Load(object sender, EventArgs e)
{
string strConnString = ConfigurationManager.ConnectionStrings["LgnConnectionString"].ConnectionString;
string str;
int requestId = int.Parse(Request.QueryString["id"]); //Get the ID
SqlCommand com;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from table where ID=@ID";
com = new SqlCommand(str, con);
com.parameters.addWithValue("@ID", requestId); //add ID parameter to query
SqlDataReader reader;
reader = com.ExecuteReader();
myDetailsview.DataSource = reader;
myDetailsview.DataBind();
con.Close();
}
根据自组织Detailsview组件