2013-07-22 111 views
1

我的代碼後面有一個ArrayList,用於存儲數據庫中各種項目的ID值。點擊按鈕後,我希望將這些ID值傳遞給SQLDataSource,以充當填充gridview的參數。不幸的是,我對VB的有限知識阻礙了我,我對如何去做這樣的事情感到困惑。將列表傳遞給sqldatasource的參數

代碼背後的.aspx頁

<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
       ConnectionString="<%$ ConnectionStrings:LOTOConnectionString %>" 
       SelectCommand="SELECT * FROM [DEVICES] WHERE ([devices_id] = @devices_id)"> 
    <SelectParameters> 
     <asp:Parameter Name="devices_id" Type="Int32" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

編輯

Protected Sub Preview_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Preview_Button.Click 

    Dim list As New ArrayList 
    Dim atLeastOneRowSelected As Boolean = False 
    'Iterate through the Devices.Rows property 
    For Each row As GridViewRow In GridView1.Rows 
     'Access the CheckBox 
     Dim cb As CheckBox = row.FindControl("DeviceSelector") 
     If cb IsNot Nothing AndAlso cb.Checked Then 
      atLeastOneRowSelected = True 
      'First, get the device_id for the selected row 
      Dim device_id As Integer = _ 
       Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value) 
      list.Add(device_id) 
     End If 
    Next 

End Sub 

的SqlDataSource了一下週圍打後,我意識到,我可以我的形成列入適當的SQL字符串usi加入連接命令。所以我仍然不理解的唯一部分是如何從後面的代碼中引用SQLDataSource。

回答

0

那麼,我想清楚我需要做什麼。我動態地創建一個SQL查詢並將其傳遞給我的SQLDataSource。我不確定這是否會讓我傾向於使用SQL注入,但由於我生成的查詢是從單獨的gridview中提取數據,我相信它是正常的?

代碼後面

Protected Sub Preview_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Preview_Button.Click 

    'Finds all checkboxes that have been selected in GridView1 
    Dim list As New ArrayList 
    Dim atLeastOneRowSelected As Boolean = False 

    'Iterate through each row of GridView1 
    For Each row As GridViewRow In GridView1.Rows 
     'Access the CheckBox 
     Dim cb As CheckBox = row.FindControl("DeviceSelector") 
     If cb IsNot Nothing AndAlso cb.Checked Then 
      atLeastOneRowSelected = True 
      'Get the device_id of the selected row 
      Dim device_id As Integer = _ 
       Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value) 
      list.Add(device_id) 
     End If 
    Next 

    If atLeastOneRowSelected Then 
     'Generate an SQL command using the selected checkboxes 
     Dim str As String = String.Join(" OR devices_id = ", list.ToArray()) 
     Dim query As String = "Select * FROM [DEVICES] where devices_id = " + str 
     SqlDataSource3.SelectCommand = query 

     'Enable the GenerateReport Button 
     GenerateReport_Button.Enabled = True 
    End If 
End Sub 

的SqlDataSource

<asp:SqlDataSource ID="SqlDataSource3" runat="server" 
       ConnectionString="<%$ ConnectionStrings:LOTOConnectionString %>" 
       SelectCommand=""></asp:SqlDataSource> 
0

只需使用SqlDataSource的選擇事件來設置參數的值:devices_id。此事件發生在DataSource執行查詢之前。所以在這個事件中設置的任何值都會被下一個要執行的查詢使用。

<asp:SqlDataSource ID="SqlDataSource3" runat="server"  
    OnSelecting="SqlDataSource3_Selecting" 
    ConnectionString="<%$ ConnectionStrings:LOTOConnectionString %>" 
    SelectCommand="SELECT * FROM [DEVICES] WHERE ([devices_id] = @devices_id)"> 

我不知道在VB中的語法,可能例如在C#下面可以幫助你的邏輯是一樣的,只是語法不同。

protected void SqlDataSource3_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
{ 
    for (int i=0;i < arrList.Count - 1;i++) 
    { 
     // just for example suppose Business logic needs the First ID value 
     if(i==0) 
     { 
     // the select parameters can be accessed using: e.Command.Parameters 
     e.Command.Parameters["@devices_id"].Value == Convert.ToInt32(list[i]); 
     } 
    } 
    }