2010-08-10 24 views
0

是否有可能在ASP.NET中獲得單行? 我總是用dataList但是這破壞了我設計的項目模板,我需要一個沒有模板只有​​聲明如何使用ASP.NET獲取單行(SQL)?

所有的
+1

請詳細說明您的問題,現在還不清楚 – abatishchev 2010-08-10 06:58:41

+0

我的意思是,我不想使用像itemTemplate someting我只想設置一些<%# Eval() %>陳述 – mullek 2010-08-10 07:02:24

回答

0

首先somethink:你的問題是真的很基本的,你應該考慮讀一本書;-) 。

你可以做的是如果你使用普通的舊的SQL代碼可能看起來像這樣使用正常綁定在頁面上...

。但是,如果您使用Linq-To-Sql或事件實體框架,這是一個不同的故事。

注意力:在這個例子中是非常直截了當的。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace="System.Configuration" %> 

<script runat="server" type="text/C#"> 

    protected DataRow _currentRow = null; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["pubs"].ConnectionString)) 
     { 
      using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM authors", connection)) 
      { 
       DataTable table = new DataTable(); 
       adapter.Fill(table); 

       _currentRow = table.Rows[0]; 
      } 
     } 

     Page.DataBind(); 
    } 

</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Testpage</title> 
</head> 
    <body> 
     <form id="form1" runat="server"> 
      <asp:TextBox ID="TextBox1" runat="server" Text='<%# _currentRow["Name"] %>'></asp:TextBox> 
     </form> 
    </body> 
</html> 
+0

你能做一個更大的例子嗎? 我不知道該怎麼辦 – mullek 2010-08-10 07:09:04

0

,如果你只是在尋找<%# Eval() %>

然後將其綁定使用mylietral.DataBind();

0

採取文字的宮殿像#Eval並從後面的代碼綁定表達式是有可能得到一個ASP.NET中的單行? ......我需要一個沒有模板

在這種情況下,我建議你乾脆要麼使用一個LiteralLabel控制,而不是somethink。