使用超链接控制从数据库中检索数据

本文关键字:检索 数据 数据库 超链接 控制 | 更新日期: 2023-09-27 18:37:05

大家好,因为我是C#asp.net 新手,我需要前辈的帮助

有一个包含以下列的表:

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();
}

它显示最后五行的表记录,我希望当我单击任何标题时,它会在另一个页面上显示与该标题相关的其余列信息,我单击了该标题,这将是详细信息.aspx

我知道这对老年人来说既简单又容易,但我被它击中了,请帮助我,提前感谢。我将在细节上编码什么.aspx以及我必须在细节上编码什么.aspx.cs

使用超链接控制从数据库中检索数据

使用以下代码

//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();

您的组件 ID .cs文件中是错误的。将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>

创建新的详细信息窗体。添加"详细信息视图"组件以查看文件。在这样的.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();
}

根据自组织详细信息视图组件