简体   繁体   English

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

[英]Retrieve data from database using hyperlink control

Hello everyone as I am new in asp.net C# I need some help from seniors 大家好,我是asp.net C#的新手,我需要长辈的帮助

There is a table with following columns: 有一个包含以下列的表格:

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

I have successfully coded the page for inserting data in this table Now I want to display it on the page, I used repeater data control to display its Title only and put it in attribute the code is below 我已经成功编码了用于在此表中插入数据的页面,现在我想在页面上显示它,我使用中继器数据控件仅显示其Title并将其放在属性中,下面的代码

   <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>

Behind the code I selected Data with following code 在代码的后面,我选择了带有以下代码的数据

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

it display the table records for last five rows, I want that when I click on any title it display the rest of columns information associated with that Title, which I clicked, on another page that will be Details.aspx 它显示了最后五行的表记录,我希望当我单击任何标题时,在与Details.aspx相同的另一页上显示与该标题相关的其余列信息。

I know it is simple and easy for seniors but I get struck on it please help me, thanks in advance. 我知道这对年长者来说既简单又容易,但是我对此感到震惊,请提前帮助我。 What I will have to code on Details.aspx and what will I have to code on Details.aspx.cs 我将在Details.aspx上进行编码的内容以及我将在Details.aspx.cs上进行编码的内容

Use the below code 使用下面的代码

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

Your component id is wrong in .cs file. .cs文件中的组件ID错误。 Change world_rep to article_rep . world_rep更改为article_rep Other things looks good 其他东西看起来不错

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

if you need detail page, add a link like this; 如果您需要详细信息页面,请添加这样的链接;

<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>

Create new detail form. 创建新的详细信息表单。 Add "Detailsview" component to view file. 添加“ Detailsview”组件以查看文件。 In the .cs file like this; 在这样的.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();
}

According to self-organize the Detailsview component 根据自组织Detailsview组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM