2011-12-14 11 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; 
} 

도 난을 다음과 같이 뒤에 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

당신은 모든 항목에 대해 해고 무엇 ListView에의 ItemDataBound 이벤트를 처리한다 ListView의 ItemCreated 이벤트와 달리).

관련 문제