从代码隐藏的GridView SelectParameter

时间:2015-10-04 06:30:35

标签: vb.net gridview

我正在使用VB.NET webforms。 我有一个GridView,它在页面加载时从数据库中提取数据。带有Select SQL的SQLDataSource如下:

<asp:SqlDataSource 
     ID="ScopeDataSource" 
     runat="server" 
     ConnectionString="<%$ ConnectionStrings:SQLServerConnectionString %>"
     SelectCommand="SELECT [SCO_LINENUM], [SCO_LINETEXT], [SCO_SCOPEID], [SCO_TAB1LINK], [SCO_TAB2LINK], [SCO_TAB3LINK] FROM [SCOPE] WHERE [SCO_SCOPEID] = 'AAAREN02'"></asp:SqlDataSource>

我的GridView如下:

<asp:GridView 
     ID="ScopeGrid" 
     runat="server" 
     DataSourceID="ScopeDataSource" 
     AutoGenerateColumns="False" 
     GridLines="None"
     DataKeyNames="SCO_SCOPEID, SCO_LINENUM">
     <Columns>
       <asp:BoundField DataField="SCO_LINENUM" HeaderText="Sr#" SortExpression="SCO_LINENUM">
         <HeaderStyle Height="40px" />
         <ItemStyle Width="30px" />
       </asp:BoundField>
       <asp:BoundField DataField="SCO_LINETEXT" HeaderText="Parameter.." SortExpression="SCO_LINETEXT">
         <HeaderStyle Height="40px" />
         <ItemStyle Width="100px" />
       </asp:BoundField>
       <asp:TemplateField HeaderText="Operator                    Values...........">
       <ItemTemplate>                                                    
         <asp:DropDownList ID="DropDownList1" runat="server" CssClass="DropDownMargin">
           <asp:ListItem>BETWEEN</asp:ListItem>
           <asp:ListItem>EQUALTO</asp:ListItem>
           <asp:ListItem>GREATERTHAN</asp:ListItem>
           <asp:ListItem>LESSTHAN</asp:ListItem>
         </asp:DropDownList>
         <asp:TextBox runat="server" ID="RangeStart" Text="None" Width="50px" CssClass="TextBoxMargin"></asp:TextBox>
         <asp:TextBox runat="server" ID="RangeEnd" Text="None" Width="50px"></asp:TextBox>
       </ItemTemplate>                                                
       <HeaderStyle Height="40px" CssClass="WhiteSpacePreserve" />
       </asp:TemplateField>
       <asp:BoundField DataField="SCO_SCOPEID" HeaderText="ScopeID" SortExpression="SCO_SCOPEID">
       <HeaderStyle CssClass="hidegrid" />
       <ItemStyle CssClass="hidegrid" />
       </asp:BoundField>
       <asp:BoundField DataField="SCO_TAB1LINK" HeaderText="Tab1" SortExpression="SCO_TAB1LINK" />
       <asp:BoundField DataField="SCO_TAB2LINK" HeaderText="Tab2" SortExpression="SCO_TAB2LINK" />
       <asp:BoundField DataField="SCO_TAB3LINK" HeaderText="Tab3" SortExpression="SCO_TAB3LINK" />
       </Columns>
      </asp:GridView>

如上面的SQLDataSource所示,WHERE子句是硬编码的。我想通过从代码隐藏传递一个变量到WHERE子句来使这个动态化。我的Page_Load事件也是空的。到目前为止,由于SQLDataSource WHERE子句在我的aspx中是硬编码的,我可以在gridview中看到数据,但我不知道如何从代码隐藏中向它发送变量?

1 个答案:

答案 0 :(得分:0)

这样的东西可行。

&#xA;&#xA;

将你的选择命令更改为

&#xA;&#xA;
  SELECT [SCO_LINENUM ],[SCO_LINETEXT],[SCO_SCOPEID],[SCO_TAB1LINK],[SCO_TAB2LINK],[SCO_TAB3LINK] FROM [SCOPE] WHERE [SCO_SCOPEID] = @ SCO_SCOPEID&#xA;  
&#xA;&#xA ;

然后执行

&#xA;&#xA;
  ScopeDataSource.SelectParameters(“SCO_SCOPEID”)。DefaultValue =您的值&#xA;  
& #xA;