DataGrid Web控件深度历险(3) part3

本文围绕DataGrid展开,介绍了如何通过ButtonColumn标记显示按钮,将事件处理程序与按钮点击关联,判断哪一行按钮被点击并执行相应动作。还讲述了隐藏列信息传递及改写事件处理程序显示详细信息,后续将扩展相关概念用于排序、分页和编辑数据。

在本文第二部分我们研究了如何通过ButtonColumn标记在DataGrid中显示按钮。此外,我们考察了如何将事件处理程序与按钮的点击联系起来。下面我们将了解到如何判断DataGrid中哪一行的按钮被点击并且基于这些信息执行相应的动作。

判断哪一行的按钮被点击

回想一下点击按钮的事件处理程序定义如下:

Sub eventHandlerName(sender as Object, e as DataGridCommandEventArgs)
   ...
End Sub

DataGridCommandEventArgs类包含一个Item属性,该属性包含了触发该事件的源对象。Item属性是TableRow类的一个实例,它指向DataGrid中被点击的那一行。可使用Cells属性访问TableRow类中的列。例如有一个DataGrid,它的列信息定义如下:

<asp:DataGrid runat="server" ... >
  <Columns>
    <asp:ButtonColumn Text="Details" HeaderText="FAQ Details" CommandName="details" />
    <asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID" />
    <asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
  </Columns>
</asp:datagrid>

那么在点击按钮的事件处理程序中,可通过以下方法获得被点击行的某一列的值:

Sub detailsClicked(sender as Object, e As DataGridCommandEventArgs)
    Dim buttonColumn as TableCell = e.Item.Cells(0)
    Dim FAQIDColumn as TableCell = e.Item.Cells(1)
    Dim DescColumn as TableCell = e.Item.Cells(2)
   
    Dim buttonColText as String = buttonColumn.Text
    Dim FAQIDColText as String = FAQIDColumn.Text
    Dim DescColText as String = DescColumn.Text
End Sub

示例运行结果如下:

更新按钮事件处理程序后的DataGrid示例

本示例展示了一个包含Detail按钮的DataGrid Web控件并演示了当按下按钮时如何触发一段代码。注意点击某个Detail按钮后你会看到被点击按钮所在行的信息。

Value of Clicked Button Column Text:
Value of FAQID Column Text: 181
Value of Clicked Description Column Text: How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.

FAQ Details

FAQ ID

FAQ Description

144

Where can I host my ASP Web site for free (similar to GeoCities or Tripod or any of the many other free Web site sites)?

181

How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.

 

 

源代码

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
                               BindData()
               End If
  End Sub
              
              
  Sub BindData()
    '1. Create a connection
    Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "sp_Popularity"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    'Set the datagrid's datasource to the datareader and databind

    myConnection.Open()
    dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgPopularFAQs.DataBind()         
  End Sub


               Sub dispDetails(sender as Object, e As DataGridCommandEventArgs)
                               Dim buttonColumn as TableCell = e.Item.Cells(0)
                               Dim FAQIDColumn as TableCell = e.Item.Cells(1)
                               Dim DescColumn as TableCell = e.Item.Cells(2)
   
                               Dim buttonColText as String = buttonColumn.Text
                               Dim FAQIDColText as String = FAQIDColumn.Text
                               Dim DescColText as String = DescColumn.Text
                              
                               lblBCT.Text = buttonColText
                               lblFCT.Text = FAQIDColText
                               lblDCT.Text = DescColText
               End Sub
</script>

<form runat="server">
               <b>Value of Clicked Button Column Text</b>:
               <asp:label id="lblBCT" runat="server" /><br />
                              
               <b>Value of FAQID Column Text</b>:
               <asp:label id="lblFCT" runat="server" /><br />

               <b>Value of Clicked Description Column Text</b>:
               <asp:label id="lblDCT" runat="server" /><br />

               <asp:DataGrid runat="server" id="dgPopularFAQs"
                               BackColor="#eeeeee" Width="85%"
                               HorizontalAlign="Center"
                               Font-Name="Verdana" CellPadding="4"
                               Font-Size="10pt" AutoGenerateColumns="False"
                               OnItemCommand="dispDetails">
                 <HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" />
                 <AlternatingItemStyle BackColor="White" />
                                
                 <Columns>
                               <asp:ButtonColumn Text="Details" HeaderText="FAQ Details" CommandName="details" ButtonType="PushButton" />
                               <asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID" />
                   <asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
                 </Columns>
               </asp:datagrid>
</form>  

请仔细检查上面的示例。你可能注意到的第一件事就是按钮列不包含任何文本。这是因为仅需通过HTML即可显示按钮,因此TableCellText属性返回了一个空字符串。

在本文开始部分我讲述了一个电子商务公司的场景,该公司希望显示部分货运信息,但同时提供显示所有货运信息的选择。到目前为止的示例中,我们仅显示了sp_Popularity存储过程返回列中的一小部分列。想象一下我们仅希望显示最受欢迎的常见问题的描述列,然后提供一个Detail按钮允许用户查看某个常见问题的其余信息。

虽然我们不希望在DataGrid中显示FAQID列,但是我们仍然需要为detialsClicked事件处理程序提供该信息,因为它数据库中表的关键字并唯一标识了每个常见问题。通过对DataGrid标记进行小小的改动(在与数据库中FAQID列对应BoundColumn标记中增加Visible= "False"),我们仍然能够传递该信息。此改动隐藏了FAQID列,但仍然允许detailClicked事件处理程序访问某个常见问题的标识(通过e.Item.Cells(1).Text)

因此我们所要做的就是改写detailsClicked事件处理程序,以便当它被触发时获得用户希望显示的那个常见问题的信息,然后再显示该常见问题的详细信息。在阅读了一系列关于如何使用DataGrid的文章后,当需要显示数据库中的数据时,你的第一个想法应该就是使用DataGrid。因此我们的页面看起来应该是这样:

<script language="vb" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
      BindData() 'Only bind the data on the first page load
    End If
  End Sub
 
 
  Sub BindData()
    'Make a connection to the database
    'Databind the DataReader results to the gPopularFAQs DataGrid.
  End Sub


  Sub detailsClicked(sender as Object, e As DataGridCommandEventArgs)
    'Get detailed information about the selected FAQ and bind
    'the database results to the dgFAQDetails DataGrid
  End Sub
</script>

<form runat="server">
  <asp:DataGrid runat="server" id="dgFAQDetails" ... >
    ...
  </asp:datagrid>

  <asp:DataGrid runat="server" id="dgPopularFAQs" ... >
    <Columns>
      <asp:ButtonColumn Text="Details" HeaderText="FAQ Details"
                                     ButtonType="PushButton" />
      <asp:BoundColumn DataField="FAQID" Visible="False" />
      <asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
    </Columns>
  </asp:datagrid>
</form>

示例运行结果如下:

本示例展示了如何在DataGrid的每一行中显示概要信息和一个Detail按钮,当按钮被点击时,对所选择的数据项显示其余信息。

Category Name

FAQ Description

Views

Author

Author's Email

Date Added

Getting Started

Where can I host my ASP Web site for free (similar to GeoCities or Tripod or any of the many other free Web site sites)?

163,148

Scott Mitchell

mitchell@4guysfromrolla.com

03-20-2001

 

FAQ Details

FAQ Description

Where can I host my ASP Web site for free (similar to GeoCities or Tripod or any of the many other free Web site sites)?

 

源代码

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
                               BindData()
               End If
  End Sub
              
              
  Sub BindData()
    '1. Create a connection
    Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "sp_Popularity"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    'Set the datagrid's datasource to the datareader and databind
    myConnection.Open()
    dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgPopularFAQs.DataBind()         
  End Sub


               Sub dispDetails(sender as Object, e As DataGridCommandEventArgs)
                               Dim FAQID as Integer = Convert.ToInt32(e.Item.Cells(1).Text)
                              
                               'Get information about the particular FAQ                            
                               Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

                               '2. Create the command object, passing in the SQL string
                               Dim strSQL as String = "spGetFAQ"
                               Dim myCommand as New SqlCommand(strSQL, myConnection)

                               myCommand.CommandType = CommandType.StoredProcedure
                              
                               ' Add Parameters to SPROC
                               Dim parameterFAQId as New SqlParameter("@FAQID", SqlDbType.Int, 4)
                               parameterFAQId.Value = FAQID
                               myCommand.Parameters.Add(parameterFAQId)
                              

                               'Set the datagrid's datasource to the datareader and databind
                               myConnection.Open()
                               dgFAQDetails.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
                               dgFAQDetails.DataBind()
                              
                               dgFAQDetails.Visible = True    'Make the FAQ Details DataGrid visible
               End Sub
</script>

<form runat="server">

               <asp:DataGrid runat="server" id="dgFAQDetails"
                               BackColor="#eeeeee" Width="90%"
                               HorizontalAlign="Center"
                               Font-Name="Verdana" CellPadding="4"
                               Font-Size="10pt" AutoGenerateColumns="False"
                               Visible="False">
               <HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" />
                 <AlternatingItemStyle BackColor="White" />
                
                 <Columns>
                   <asp:BoundColumn DataField="CatName" HeaderText="Category Name"  />
                   <asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
                   <asp:BoundColumn DataField="ViewCount" DataFormatString="{0:#,###}"
                          HeaderText="Views" ItemStyle-HorizontalAlign="Center" />
                   <asp:BoundColumn DataField="SubmittedByName" HeaderText="Author"  />
                   <asp:BoundColumn DataField="SubmittedByEmail" HeaderText="Author's Email"  />
                   <asp:BoundColumn DataField="DateEntered" HeaderText="Date Added"
                                                             DataFormatString="{0:MM-dd-yyyy}"  />   
                 </Columns>
               </asp:datagrid>
               <p>
               <asp:DataGrid runat="server" id="dgPopularFAQs"
                               BackColor="#eeeeee" Width="85%"
                               HorizontalAlign="Center"
                               Font-Name="Verdana" CellPadding="4"
                               Font-Size="10pt" AutoGenerateColumns="False"
                               OnItemCommand="dispDetails">
                 <HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" />
                 <AlternatingItemStyle BackColor="White" />
                
                 <Columns>
                               <asp:ButtonColumn Text="Details" HeaderText="FAQ Details" ButtonType="PushButton" />
                               <asp:BoundColumn DataField="FAQID" Visible="False" />
                   <asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
                 </Columns>
               </asp:datagrid>
</form>

需要注意的第一件事就是ASP.Net Web页面中有两个DataGrid。第一个(dgFAQDetails)用于显示一个特定常见问题的详细信息。第二个(dgPopularFAQs)是我们一直用来显示最受欢迎的10个常见问题的那个DataGrid。注意dgPopularFAQs DataGridFAQID绑定列被修改了,增加了Visible="False"以便FAQID列不在dgPopularFAQs DataGrid的输出中显示。

在过去的三篇文章中我们研究了将数据库查询的结果显示在HTML表格中(第一篇),在无需编写代码的情况下美化HTML表格(第二篇)和本篇中如何在每列中增加按钮并对事件进行响应。我们将在今后的文章中看到“增加按钮并当按钮被点击时进行响应”的概念可被扩展以允许进行排序、分页和编辑数据。回见……

祝编程愉快!

 

内容概要:本文围绕“基于交流潮流的电力系统多元件N-k故障模型研究”展开,深入探讨了利用Matlab代码实现电力系统在发生多个关键元件同时故障(即N-k故障)情况下的交流潮流计算与故障分析方法。该模型不仅考虑了传统潮流方程的非线性特性,还引入了故障约束条件,能够精确模拟复杂多样的故障场景,如短路、断线等,进而评估电网在极端运行条件下的稳态与动态行为。研究通过构建典型电力系统算例,验证了所提模型在故障筛选、脆弱性识别及系统恢复策略制定方面的有效性,为电力系统安全评估、风险预警和防御体系构建提供了坚实的理论依据和技术支撑。此外,模型具备良好的扩展性,可进一步应用于连锁故障传播分析、恶意攻击模拟等高级安全分析领域。; 适合人群:具备电力系统分析基础理论知识和Matlab编程能力的高校研究生、科研院所研究人员以及电力公司从事电网规划、运行与安全管理的技术人员,特别适用于开展电力系统安全稳定、可靠性评估与应急响应机制研究的专业人士。; 使用场景及目标:①开展电力系统在多重故障条件下的交流潮流仿真,评估系统电压稳定性、线路过载风险及负荷损失程度;②识别电网中的关键薄弱环节与脆弱元件,支撑电网加固改造与防御资源配置;③用于科研项目中的故障场景建模与算法验证,或作为教学案例帮助学生理解复杂故障下的系统响应机制。; 阅读建议:此资源以Matlab代码为核心实现手段,建议读者结合理论推导与代码实现进行对照学习,重点关注故障建模过程中雅可比矩阵的修正方法、故障注入方式及收敛性处理策略,建议在仿真中逐步增加故障数量与复杂度,深入理解N-k故障对系统潮流分布的影响规律,并尝试将其拓展至含新能源接入的现代电力系统场景中进行验证与优化。
【重要提示】本资源设置为0积分下载,若非0积分请勿轻易下载 亲爱的CSDN用户: 首先感谢你点进这个资源页面。我需要提前说明一个重要情况: 本资源原本已设置为“0积分下载”,即作者希望完全免费共享。但CSDN平台有时会根据文件的下载热度、文件大小、用户权限等因素,自动将部分资源的积分调整为非0数值(如1积分、2积分、5积分等)。这是平台系统的自动行为,而非作者本人的设定。 因此,如果你当前看到该资源的下载所需积分不是0(例如显示为1、2、3……),请谨慎决定是否下载。 如果你按照非0积分支付并下载后发现资源内容不符合预期、链接失效,或者实际上该资源本应是免费的,作者无法为此承担积分损失或退还操作。强烈建议:仅在页面显示为0积分时进行下载。 另外,本资源描述中并未直接提供具体的下载地址或外部链接,因为它本身是一个通过CSDN官方上传通道提交的文件/内容包。如果你看到描述中没有外部网盘地址,这是正常的——资源文件应通过CSDN内置的“下载”按钮获取。若因平台积分显示异常导致你支付了积分,请优先联系CSDN客服咨询积分退还政策,作者没有权限修改平台自动设定的积分值。 感谢你的理解与支持。技术分享本应开放,但受限于平台规则,特此提醒如上。祝学习进步!
内容概要:本文详细介绍了基于PyTorch实现的并行物理信息神经网络(PINNs)在NLS–MB方程孤子演化预测中的应用实例,系统阐述了模型架构设计、损失函数构造、训练流程优化及并行计算策略的实施过程。通过深度融合物理先验知识与深度学习框架,该方法有效求解了非线性薛定谔类偏微分方程,实现了对孤子动力学行为的高精度、高效率数值模拟与长期演化预测,充分展现了PINNs在处理复杂科学计算问题中的强大建模能力与泛化性能。; 适合人群:具备一定深度学习理论基础和偏微分方程求解经验,熟练掌握Python编程语言及PyTorch深度学习框架,从事计算物理、流体力学、光学通信或相关工程仿真的研究生、科研人员及高级技术人员。; 使用场景及目标:①深入理解如何将物理守恒律与控制方程作为硬约束嵌入神经网络,提升模型在稀疏数据下的泛化能力与物理一致性;②掌握PINNs在非线性孤子波、色散介质传播等复杂动力系统建模中的关键技术实现路径;③应用于量子物理、非线性光学、大气海洋动力学等领域中传统数值方法难以求解的高维、强非线性偏微分方程的正/反问题研究。; 阅读建议:建议读者结合文末提供的完整代码资源(可通过公众号“荔枝科研社”获取)进行动手实践,重点关注物理残差项在自动微分框架下的精确计算、多任务损失权重的平衡策略,并尝试迁移模型至其他类型的非线性演化方程以深化理解与应用能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值