2013-10-26 105 views
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> 

乙ehind的代碼,我選擇的數據與下面的代碼

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

那麼,什麼是你的問題? –

+0

先生的問題是:當我點擊標題鏈接時,將獲取details.aspx頁面上所有記錄的過程是什麼。就像新聞在新聞網站點擊新聞一樣,並且開始有新聞的詳細的信息的新的一頁,更早點擊 –

+0

好的,但是有什麼問題嗎?你嘗試過什麼嗎? –

回答

0

您的組件ID在.cs文件中是錯誤的。更改world_reparticle_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 [email protected]"; 
    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控件組件

+0

先生,那不是我要求幫助的問題如何顯示其他頁面上的其他信息,即details.aspx,它就像新聞,當我們點擊新聞網站上的新聞時,它會打開一個新的頁面,那條消息的信息,我需要一些幫助我有什麼要在details.aspx頁面中編寫代碼,以及details.aspx.cs中的代碼是什麼 –

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