2011-12-14 7 views
0

実際に私はasp.netとC#を使用してテンプレートを開発しています。
私は私のascxファイルのページで、リストビューを使用していて、私のItemTemplateには、以下の通りである:codebehindから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; 
} 

ともI続くよう背後に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をコードの背後から変更すると、データベースのデータをコードの背後のラベルからこれらのラベルに渡すことを意味します。
ご了承ください。このイベントは、(すべてのポストバックにだけ、データバインディングにトリガされません

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

+2

http://stackoverflow.com/questions/8063311/how-to-set-label-text-inside-listview-from-code-behind –

答えて

0

あなたはそれのDataSourceにListViewコントロールをバインドした後、すべての項目に解雇されたものをリストビューのItemDataBoundイベントを処理する必要がありますListViewのItemCreatedイベントとは異なります)。

関連する問題