加入收藏 | 设为首页 | 会员中心 | 我要投稿 江门站长网 (https://www.0750zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在ASP.NET 2.0中操作数据之四十九:为GridView控件添加RadioButt

发布时间:2016-11-24 00:05:35 所属栏目:MsSql教程 来源:站长网
导读:导言: GridView控件提供了大量的内置功能。它包含了一系列的域(field)来显示诸如text、images、hyperlinks和buttons。另外它支持模板(template)用于用户自定义界面。我们可以构建这样一个GridView控件,用户仅需要点击控件里的一个按钮,每一条记录行

  接着,在Click事件处理器里添加代码,如果SuppliersSelectedIndex的值小于0时Label控件ChooseSupplierMsg将可见,并将用户直接链接到~/Filtering/ProductsForSupplierDetails.aspx?SupplierID=SupplierID页面:

protected void SendToProducts_Click(object sender, EventArgs e)
{
 // make sure one of the radio buttons has been selected
 if (SuppliersSelectedIndex < 0)
  ChooseSupplierMsg.Visible = true;
 else
 {
  // Send the user to ~/Filtering/ProductsForSupplierDetails.aspx
  int supplierID = 
   Convert.ToInt32(Suppliers.DataKeys[SuppliersSelectedIndex].Value);
  Response.Redirect(
   "~/Filtering/ProductsForSupplierDetails.aspx?SupplierID=" 
   + supplierID);
 }
}

  在浏览器里访问该页面,在不选供应商的情况下点击SendToProducts按钮,如图14所示,label控件ChooseSupplierMsg变为可见了。然后,选择一个供应商,再单击SendToProducts按钮,这会将你链接到一个显示该供应商产品的页面。图15显示了当选择供应商Bigfoot Breweries时,ProductsForSupplierDetails.aspx
页面的情况。

/uploads/allimg/c161121/14OI939135060-A24b.gif
图14:未选供应商时,Label控件ChooseSupplierMsg为可见

/uploads/allimg/c161121/14OI939163T0-BN46.gif
图15:ProductsForSupplierDetails.aspx页面显示了供应商的产品

第5步:在本页显示所选供应商的产品

  在第4步,我们探讨了怎样在另一页面显示供应商的产品,另外我们也可以在本页面显示产品。鉴于此,我们在RadioButtonField.aspx页面添加另一个GridView控件展示所选供应商的产品。

  一旦选择一个供应商后,我们只想在GridView里显示该供应商的产品。在名为Suppliers的GridView控件下,添加一个Panel Web控件,设其ID为ProductsBySupplierPanel,将Visible属性设置为false,text属性设置为“Products for the Selected Supplier”, 紧接着添加一个名为ProductsBySupplier的GridView控件。在其智能标签里,将其绑定到一个名为ProductsBySupplierDataSource的ObjectDataSource控件。

/uploads/allimg/c161121/14OI9391UG0-C4P4.gif
图16:将GridView控件ProductsBySupplier绑定到一个新的ObjectDataSource

   然后,将设置该ObjectDataSource使用ProductsBLL类。由于我们只想获取一个供应商的产品数据,指定该ObjectDataSource控件调用GetProductsBySupplierID(supplierID)方法,同时在UPDATE, INSERT和DELETE选项卡的下拉列表里选“(None)” 。

/uploads/allimg/c161121/14OI939212340-DXL.gif
图17:指定该ObjectDataSource控件调用GetProductsBySupplierID(supplierID)方法

/uploads/allimg/c161121/14OI939241T0-E43D.gif
图18:在UPDATE, INSERT和DELETE选项卡的下拉列表里选“(None)”

  完成对SELECT, UPDATE, INSERT和DELETE选项卡的设置后,点下一步。因为GetProductsBySupplierID(supplierID)方法需要一个输入参数,因此向导提示我们指定参数值的来源。

  我们有几种选择指定参数值来源。我们可以使用默认值,也可以在ObjectDataSource控件的Selecting事件处理器里,通过编程的方式,用SuppliersSelectedIndex属性的值对Parameter的DefaultValue属性赋值。可以在前面的教程《编程设置ObjectDataSource的参数值》里温习相关内容。

  此外,我们可以使用一个ControlParameter,它涉及到名为Suppliers的GridView控件的SelectedValue属性(见图19)。GridView控件的SelectedValue属性将返回与SelectedIndex属性相匹配的DataKey值。为此,当点击ListProducts按钮时,我们将编程把GridView控件的SelectedIndex属性设置为选中的那行记录。另外设置了SelectedIndex后,选中的记录将运用定义在DataWebControls主题中的SelectedRowStyle(显示为黄色背景)。

/uploads/allimg/c161121/14OI9392N350-FL42.gif
图19:用一个ControlParameter当参数源来指定GridView的SelectedValue值

  完成向导后,Visual Studio将自动添加产品的数据域(data fields)。将除了ProductName, CategoryName和UnitPrice外的其它列全部删除,并将HeaderText属性分别设为“Product”, “Category”和“Price”。再将UnitPrice列格式化为货币形式。完成上述修改后,Panel, GridView和ObjectDataSource的声明代码看起来应该像下面的差不多:

<asp:Panel runat="server" ID="ProductsBySupplierPanel" Visible="False">
 <h3>
  Products for the Selected Supplier</h3>
 <p>
  <asp:GridView ID="ProductsBySupplier" runat="server" 
   AutoGenerateColumns="False" DataKeyNames="ProductID"
   DataSourceID="ProductsBySupplierDataSource" EnableViewState="False">
   <Columns>
    <asp:BoundField DataField="ProductName" HeaderText="Product" 
     SortExpression="ProductName" />
    <asp:BoundField DataField="CategoryName" HeaderText="Category" 
     ReadOnly="True" SortExpression="CategoryName" />
    <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" 
     HeaderText="Price" HtmlEncode="False" 
     SortExpression="UnitPrice" />
   </Columns>
  </asp:GridView>
  <asp:ObjectDataSource ID="ProductsBySupplierDataSource" runat="server" 
   OldValuesParameterFormatString="original_{0}"
   SelectMethod="GetProductsBySupplierID" TypeName="ProductsBLL">
   <SelectParameters>
    <asp:ControlParameter ControlID="Suppliers" Name="supplierID" 
     PropertyName="SelectedValue" Type="Int32" />
   </SelectParameters>
  </asp:ObjectDataSource>
 </p>
</asp:Panel>

(编辑:江门站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读