2011-12-14 46 views
0

其實我正在開發模板使用asp.net和c#。
我使用的ListView在我的ascx頁面,我的ItemTemplate中是如下:如何在代碼隱藏中更改listview中的標籤值?

<ItemTemplate> 
<tr style="background-color:#FFF8DC;color: #000000;"> 
    <td> 
     <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this Product Details?');" /> 
     <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" CausesValidation="True" /> 
    </td> 
    <td> 
     <asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' /> 
    </td> 
    <td> 
     <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' /> 
    </td> 
    <td> 
     <asp:Label ID="DepartmentLabel" runat="server" Text='<%# Eval("Department") %>' /> 
    </td> 
    <td> 
     <asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' /> 
    </td> 
    <td> 
     <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' /> 
    </td> 
</tr> 
</ItemTemplate> 

,我從數據庫中檢索的數據ASCX後面的代碼波紋管:

public DataTable GetEmployee(string query) 
{ 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString); 
    SqlDataAdapter ada = new SqlDataAdapter(query, con); 
    DataTable dtEmp = new DataTable(); 
    ada.Fill(dtEmp); 
    return dtEmp; 
} 

,也是我綁定在ASCX代碼背後的數據如下:

private void BindLVP(string SortExpression) 
{ 
    string UpdateQuery = "Select * from Employee" + SortExpression; 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString); 
    hid_UpdateQTP.Value = UpdateQuery; 

    lvProduct.Items.Clear(); 
    lvProduct.DataSource = GetEmployee(UpdateQuery); 
    lvProduct.DataBind(); 
} 

我的問題是我怎麼能刪除<%# Eval("EmpID") %>和所有其他的標籤文字像這在ItemTemplate中,並從後面的代碼中更改ItemTemplate中的label.text,我的意思是將數據庫的數據從後面的代碼傳遞到這些標籤。
感謝您的考慮。

+2

看看http://stackoverflow.com/questions/8063311/how-to-set-label-text-inside-listview-from-code-behind – 2011-12-14 09:57:29

回答

0

您應該處理ItemDataBound事件是什麼,每項引發的ListView控件的你已經綁定的ListView控件後,它的數據源:

protected void LVP_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListViewItemType.DataItem) 
    { 
     Label EmpIDLabel = (Label)e.Item.FindControl("EmpIDLabel"); 

     System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView; 
     EmpIDLabel.Text = rowView["EmpID"].ToString(); 
    } 
} 

此事件不是每次回發觸發,但只有在數據綁定(不像ListView的ItemCreated事件)。

相關問題