0

我有一個gridview自動連接sqldatasource1等。對於網格,我也有一個搜索文本框如果用戶需要篩選記錄,它會調用另一個((sqldatasource2 - 另一個procuder )),並把它放在同一個gridview的..,所以我需要的gridView.datasourceID切換到另一個的SqlDataSource的onclick按鈕..使用ASP .NET更改Gridview數據源SQLDATASOURCE

注: sqldatasource1和sqldatasource2返回相同的查詢,

的sqldatasource1的預處理:

create proc AfficheDossiers 
@Nom_GIAC varchar(50) 
as 
begin 
select [ID_Dossier] as 'ID_Dossier' 
     ,[ID_Entreprise] as 'ID_Entreprise' 
     ,[Date_Depot] as 'Date_Dépôt' 
     ,[Type_Etude] as 'Type_Etude' 
     ,[Dernier_Type] as 'Dernier_Type' 
     ,[Eligibile] as 'Eligibilité' 
     ,[Fiche_Information] as 'Fiche_Information' 
     ,[Buletin_Adhesion] as 'Bulletin_d’adhésion' 
     ,[Fiche_Renseignment] as 'Fiche_Renseignment' 
     ,[Attestation] as 'Attestation' 
     ,[Date_Debut] as 'Date_Début' 
     ,[Date_Fin] as 'Date_Fin' 
     ,[ID_Cabinet] as 'ID_Cabinet' 
     ,[Montant_Demander] as 'Montant_Demander' 
     ,[Duree] as 'Durée' 
     ,[Porcentage_Taux] as 'Pourcentage,Taux' from Dossier where Nom_Giac = @Nom_GIAC 
end 

sqldatasource2的procuder((搜索器):

alter proc rechercherGIAC @nomgiac varchar(20),@nom varchar(30),@par varchar(50) 
      as 
      begin 
      if @nom='CNSS' 
       begin 
       select d.[ID_Dossier] as 'ID_Dossier' 
       ,d.[ID_Entreprise] as 'ID_Entreprise' 
       ,[Date_Depot] as 'Date_Dépôt' 
       ,[Type_Etude] as 'Type_Etude' 
       ,[Dernier_Type] as 'Dernier_Type' 
       ,[Eligibile] as 'Eligibilité' 
       ,[Fiche_Information] as 'Fiche_Information' 
       ,[Buletin_Adhesion] as 'Bulletin_d’adhésion' 
       ,[Fiche_Renseignment] as 'Fiche_Renseignment' 
       ,[Attestation] as 'Attestation' 
       ,[Date_Debut] as 'Date_Début' 
       ,[Date_Fin] as 'Date_Fin' 
       ,[ID_Cabinet] as 'ID_Cabinet' 
       ,[Montant_Demander] as 'Montant_Demander' 
       ,[Duree] as 'Durée' 
       ,[Porcentage_Taux] as 'Pourcentage,Taux' 
       from dbo.Dossier d inner join entreprise e on d.ID_Entreprise=e.ID_Entreprise 
       where [email protected] and [email protected] 
       end 
      else if @nom='RS' 
       begin 
       select [ID_Dossier] as 'ID_Dossier' 
       ,[ID_Entreprise] as 'ID_Entreprise' 
       ,[Date_Depot] as 'Date_Dépôt' 
       ,[Type_Etude] as 'Type_Etude' 
       ,[Dernier_Type] as 'Dernier_Type' 
       ,[Eligibile] as 'Eligibilité' 
       ,[Fiche_Information] as 'Fiche_Information' 
       ,[Buletin_Adhesion] as 'Bulletin_d’adhésion' 
       ,[Fiche_Renseignment] as 'Fiche_Renseignment' 
       ,[Attestation] as 'Attestation' 
       ,[Date_Debut] as 'Date_Début' 
       ,[Date_Fin] as 'Date_Fin' 
       ,[ID_Cabinet] as 'ID_Cabinet' 
       ,[Montant_Demander] as 'Montant_Demander' 
       ,[Duree] as 'Durée' 
       ,[Porcentage_Taux] as 'Pourcentage,Taux' 
       from dbo.Dossier 
       where [email protected] and ID_Entreprise in(select ID_Entreprise 
                  from dbo.Entreprise 
                  where [email protected])            
       end 
      else if @nom ='Date' 
      begin 
      declare @v smalldatetime,@b smalldatetime 
       set @b=SUBSTRING(@par,1,4) 
       set @v=SUBSTRING(@par,5,8) 
      select [ID_Dossier] as 'ID_Dossier' 
       ,[ID_Entreprise] as 'ID_Entreprise' 
       ,[Date_Depot] as 'Date_Dépôt' 
       ,[Type_Etude] as 'Type_Etude' 
       ,[Dernier_Type] as 'Dernier_Type' 
       ,[Eligibile] as 'Eligibilité' 
       ,[Fiche_Information] as 'Fiche_Information' 
       ,[Buletin_Adhesion] as 'Bulletin_d’adhésion' 
       ,[Fiche_Renseignment] as 'Fiche_Renseignment' 
       ,[Attestation] as 'Attestation' 
       ,[Date_Debut] as 'Date_Début' 
       ,[Date_Fin] as 'Date_Fin' 
       ,[ID_Cabinet] as 'ID_Cabinet' 
       ,[Montant_Demander] as 'Montant_Demander' 
       ,[Duree] as 'Durée' 
       ,[Porcentage_Taux] as 'Pourcentage,Taux' 
      from Dossier 
      where Date_Depot between @b and @v and Nom_Giac like @nomgiac 

      end 
      end 

這是搜索文本和按鈕:

Saisir la CNSS : 
        <asp:TextBox ID="CNSSTxt" class="TXTBOX" runat="server" Height="23px" placeholder="CNSS" style="font-family : Comic Sans MS, Arial, Tahoma; color:Red;"></asp:TextBox> 

        <asp:Button ID="BtnFiltrerCNSS" runat="server" Text="Filtrer" class="BTN" style="font-variant:small-caps;" /> 

這是在GridView:

<div class="GridViewDiv"> 

       <asp:UpdatePanel ID="DossierUpdatePanel" runat="server" UpdateMode="Conditional" > 
        <ContentTemplate> 
         <asp:SqlDataSource ID="Dossier" runat="server" 
           ConnectionString="<%$ ConnectionStrings:OfficeConnectionString %>" 
           SelectCommand="AfficheDossiers" SelectCommandType="StoredProcedure"> 
            <SelectParameters> 
             <asp:SessionParameter Name="Nom_GIAC" SessionField="Nom_GIAC" Type="String" /> 
            </SelectParameters> 
          </asp:SqlDataSource> 

         <asp:SqlDataSource ID="RechercheDossierDS" runat="server" 
          ConnectionString="<%$ ConnectionStrings:OfficeConnectionString %>" 
          SelectCommand="rechercherGIAC" SelectCommandType="StoredProcedure"> 
          <SelectParameters> 
           <asp:SessionParameter Name="nomgiac" SessionField="Nom_GIAC" Type="String" /> 
           <asp:ControlParameter ControlID="RecherhcerComboBox" Name="nom" 
            PropertyName="SelectedValue" Type="String" /> 
           <asp:SessionParameter Name="par" SessionField="Recherche" Type="String" /> 
          </SelectParameters> 
         </asp:SqlDataSource> 
         <asp:GridView ID="DossierGV" runat="server" AllowPaging="True" AllowSorting="True" 
      DataSourceID="Dossier" AutoGenerateColumns="False" DataKeyNames="ID_Dossier"> 
          <Columns> 
           <asp:CommandField ShowSelectButton="True" /> 
           <asp:BoundField DataField="ID_Dossier" HeaderText="ID_Dossier" ReadOnly="True" 
            SortExpression="ID_Dossier" /> 
           <asp:BoundField DataField="ID_Entreprise" HeaderText="ID_Entreprise" 
            SortExpression="ID_Entreprise" /> 
           <asp:BoundField DataField="Date_Dépôt" HeaderText="Date_Dépôt" 
            SortExpression="Date_Dépôt" /> 
           <asp:BoundField DataField="Type_Etude" HeaderText="Type_Etude" 
            SortExpression="Type_Etude" /> 
           <asp:BoundField DataField="Dernier_Type" HeaderText="Dernier_Type" 
            SortExpression="Dernier_Type" /> 
           <asp:BoundField DataField="Eligibilité" HeaderText="Eligibilité" 
            SortExpression="Eligibilité" /> 
           <asp:BoundField DataField="Fiche_Information" HeaderText="Fiche_Information" 
            SortExpression="Fiche_Information" /> 
           <asp:BoundField DataField="Bulletin_d’adhésion" HeaderText="Bulletin_d’adhésion" 
            SortExpression="Bulletin_d’adhésion" /> 
           <asp:BoundField DataField="Fiche_Renseignment" HeaderText="Fiche_Renseignment" 
            SortExpression="Fiche_Renseignment" /> 
           <asp:BoundField DataField="Attestation" HeaderText="Attestation" 
            SortExpression="Attestation" /> 
           <asp:BoundField DataField="Date_Début" HeaderText="Date_Début" 
            SortExpression="Date_Début" /> 
           <asp:BoundField DataField="Date_Fin" HeaderText="Date_Fin" 
            SortExpression="Date_Fin" /> 
           <asp:BoundField DataField="ID_Cabinet" HeaderText="ID_Cabinet" 
            SortExpression="ID_Cabinet" /> 
           <asp:BoundField DataField="Montant_Demander" HeaderText="Montant_Demander" 
            SortExpression="Montant_Demander" /> 
           <asp:BoundField DataField="Durée" HeaderText="Durée" SortExpression="Durée" /> 
           <asp:BoundField DataField="Pourcentage,Taux" HeaderText="Pourcentage,Taux" 
            SortExpression="Pourcentage,Taux" /> 
          </Columns> 
         </asp:GridView> 
        </ContentTemplate> 
       </asp:UpdatePanel> 
     </div>  

任何線索?

感謝,

+0

你可以在後面顯示一些代碼嗎?除了你的疑問?你在哪裏綁定你的數據源,按鈕點擊方法是什麼.. – Thousand

+0

我更新我的問題^^ –

回答

3

在剛剛設定的onclick按鈕事件

gridview.datasource = newDataSource;

然後調用

girdView.DataBind();

讓我知道如果這是你所需要的。

如果您使用visual studio 2010,則需要單擊.aspx文件,然後單擊窗口右下方的設計視圖。這會加載你的屏幕的模擬,你可以雙擊你想要點擊事件的按鈕。它會將您重定向到(.aspx.cs)文件後面的代碼。在那個方法中你需要寫這個。

,因爲我看到你的按鈕,沒有任何的onclick =「方法名」屬性

+0

我試試這個..但它不工作:s:s –

+0

看到我的編輯。如果您設置了中斷點並對其進行調試,您是否有代碼進入代碼隱藏中的onclick事件? – joncodo

1

您可以根據情況在後面的代碼給數據源我現在才這麼說。

if(someCondition) 
     myStoredProcedure = "storedProc1"; 
    else 
     myStoredProcedure = "storedProc2"; 

    SqlDataSource dataSource = new SqlDataSource(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, searchQuery); 
    dataSource.SelectCommandType = SqlDataSourceCommandType.StoredProcedure; 
    dataSource.SelectCommand = myStoredProcedure; 
    dataSource.ID = "myDataSource"; 

    // Your code to set datasource to grid 

    gridBookings.DataSourceID = dataSource.ID; 
    gridBookings.DataBind(); 
+0

我發現了一個undrstand dtentulte:myStoredProcedure =「storedProc1」; –

+0

是的,這是它tnkkss ..我現在明白^^ Adil阿拉伯名字tnks再次我的兄弟 –

+0

不客氣的兄弟。 – Adil