2

大家好,因为我是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 上编写什么代码

4

2 回答 2

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();
于 2013-10-26T10:57:53.947 回答
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>

创建新的详细信息表单。添加“Detailsview”组件以查看文件。在这样的 .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组件

于 2013-10-26T10:47:31.827 回答