2009-02-02 2 views
0

"모든"옵션을 제공하기 위해 내 DataList에 레코드를 주입하는 방법에 대한 제안이 필요합니다. 다음은 Northwind 데이터베이스에서 가져온 데이터입니다.데이터 목록에 레코드를 입력하십시오.

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" 
     RepeatLayout="Flow" ShowFooter="False" ShowHeader="False" 
     RepeatDirection="Horizontal" 
     onitemcommand="DataList1_ItemCommand"> 
     <ItemStyle CssClass="datalist" /> 
    <ItemTemplate> 
     <%#(((DataListItem)Container).ItemIndex+1).ToString() %> 
     <asp:LinkButton ID="lbtnRegion" runat="server" 
      Text='<%# Eval("RegionDescription").ToString().Trim() %>' 
     CommandName='<%# DataBinder.Eval(Container.DataItem,"RegionID")%>' />      
    </ItemTemplate>  
    </asp:DataList> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
    SelectCommand="SELECT [RegionID], [RegionDescription] FROM [Region]" 
     ondatabinding="SqlDataSource1_Databinding" 
    onselected="SqlDataSource1_Selected"> 
</asp:SqlDataSource> 

Datalist의 링크 버튼을 사용하여 지역을 필터링하고 GridView에 표시합니다. 내가 무엇을하고 싶은지 데이터 바인딩 과정에서 모든 옵션, 어떤 제안을 주시면 감사하겠습니다 DataList에 항목을 추가 할 수 있습니다.

답변

0

한 가지 방법은 UNION에 항목 드롭 다운 목록을 가져 오는 쿼리에 모두 '전체'값

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) 
{ 
    LinkButton lbtn; 

    foreach (DataListItem dli in DataList1.Items) 
    { 
     lbtn = (LinkButton)dli.FindControl("lbtnRegion"); 
     if (lbtn != null) 
      lbtn.ForeColor = System.Drawing.Color.White; 
    } 
    string command = e.CommandName; 
    lbtn = (LinkButton)e.Item.FindControl("lbtnRegion"); 
    if (lbtn != null) 
     lbtn.ForeColor = System.Drawing.Color.YellowGreen; 

    DataView dv = GetData(ref command); // Pass the RegionId 
    gvTerritory.DataSource = dv; 
    gvTerritory.DataBind(); 
} 

감사합니다.

SELECT 'All', 'All Regions' 
UNION ALL 
SELECT [RegionID], [RegionDescription] FROM [Region] 

그러나이 같은 목록 (또는 드롭 다운) 많은 경우, 그것은 당신을 위해 '모든'기록을 주입 사용자 지정 컨트롤을 만들 수있는 더 나은 좋습니다.

+0

중대한 제안, 감사에서 동일하게 작동 할 수 드롭 다운에이 사용됩니다. – Picflight

0

그것은 다음과 같은 SQL과 협력 :

SELECT '-1' AS 'RegionID', 'All Regions' AS 'RegionDescription' 
UNION ALL SELECT [RegionID], [RegionDescription] FROM [Region] 
0

이 DataList에

Protected Sub ddlDataSources_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDataSources.DataBound 
    ddlDataSources.Items.Insert(0, New ListItem("All Data Sources", 0)) 
    End Sub 
관련 문제