2013-07-10 37 views
2

我想將DataListControl中的標籤中的數據綁定到SQL數據源,並在其他頁面中將其視爲GridView將數據從標籤綁定到SQL數據源

我不知道如何從標籤獲取數據,並把它放在數據庫...

我試圖代碼這一點,但我所需要從標籤的方式來GetData並投入到變量。

我已經創建:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Data; 

public partial class Products : System.Web.UI.Page 
{ 
    // string Name = Label.Equals(Namelable) // What should I do here to Get data from Lable?! 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString); 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void btnOrder_Click(object sender, EventArgs e) 
    { 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("sp_Order", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@Name", Name); 

    } 
} 

任何知道該怎麼辦?

回答

3

假設你的標籤是一個asp:Label

cmd.Parameters.AddWithValue("@Name", YourLabel.Text); 

如果你指的是從DataList拉着一個標籤,你需要找到控制第一如

var label = dataList.Items[i].FindControl("yourLabel") as Label; 
cmd.Parameters.AddWithValue("@Name", label.Text); 

僅供參考 - 這通常不是一個好主意,夫婦與您的視圖數據訪問層...

+0

它實際上不是一個標籤標準控制。 這是一個** DataList **中的數據綁定,我把它放在一個標籤中。 –

+0

謝謝你的時間和建議兄弟。 我正在建立一個奧德行動的場景,我需要抓住**產品編號**從這個_DataList_和**用戶ID **來自打開此會話的用戶可能從登錄_txtName_ 任何意見? –

+0

如何拉取User_ID字段完全取決於您的身份驗證/登錄過程是如何設置的。你能提供更多的信息嗎?通常,如果您使用內置的ASP.NET身份驗證機制,您應該可以從「Request.User.Identity.Name」獲取登錄名。 – James

0

我覺得你的要求是找一個DataList內側的標籤,然後綁定它的價值

所以如果你assuem在itemcommand

做你發現有控制

stirng str = ((label)e.item.findcontrol("lable1")).text.trim(); 

其餘代碼去這裏