我有使用 VB Visual Studio 2008 创建的 Web 应用程序。我创建了一个新页面来显示一个网格视图,该视图显示来自具有特定状态的数据库的合同。在网格视图中,我有一个带有链接按钮的项目模板字段。链接按钮的默认文本是添加。
单击按钮时,我想将合同的 ID 号从数据库添加到整数列表中。如果合同 ID 出现在列表中,则按钮的文本应显示为“删除”,然后应将其从列表中删除。如果它不在列表中,按钮的文本应该读作“添加”并且应该添加到列表中。在按钮的点击事件中,我有代码来实现这一点。
该页面加载正常并在网格视图中显示项目,但是当我单击添加按钮时,它会生成指向我投射链接按钮的空异常错误。然后我将网格视图放在更新面板中,我不再收到空异常错误,但现在什么也没有发生。
这是网格视图的 aspx 代码和 vb 代码。使用 Dim btnT As New LinkButton 和使用 btnT = CType(sender, LinkButton) 的下一行触发了 null 错误。任何帮助将不胜感激,奇怪的是在 vb asp.net 网站中有非常相似的代码(我实际上复制了代码),并且工作正常。
<td align="center">
<div class="style82">
Contract Queue
</div>
<hr />
<div class = "queue">
<div class="status">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</div>
<div>
<asp:UpdatePanel ID="MainUpdate" runat="server">
<ContentTemplate>
<div>
<asp:GridView ID="grdvContract" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="4"
Font-Names="Arial" Font-Size="Small"
ForeColor="Black" GridLines="Vertical" PageSize="25" Width="100%"
OnRowDataBound="grdvContract_RowDataBound">
<FooterStyle BackColor="#333300" BorderColor="#FF5050" BorderStyle="Solid"
ForeColor="#FF9900" />
<RowStyle BackColor="#F7F7DE" />
<Columns>
<asp:BoundField DataField="dealno" HeaderText="Deal No" SortExpression="dealno" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Currency" HeaderText="Currency"
SortExpression="Currency" />
<asp:BoundField DataField="Amount" HeaderText="Amount"
DataFormatString="{0:#,###,###.00}" SortExpression="Amount" HtmlEncode="false" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
<asp:HyperLinkField DataNavigateUrlFields="dealno" Text='<img src="graphics/txt.gif" border="0" />'
DataNavigateUrlFormatString="deal_ticket_standard.aspx?id={0}"
HeaderStyle-HorizontalAlign="Left"
HeaderText="Standard View" ItemStyle-HorizontalAlign="Left" Target="javascript:window.open ('deal_ticket_standard.aspx?id={0}', 'win', 'height=400,width=400,location=no,menubar=no,resizable,scrollbars,status=no,toolbar=no');">
<HeaderStyle Width="50" />
<ItemStyle HorizontalAlign="Left"/>
</asp:HyperLinkField>
<asp:BoundField DataField="rate" HeaderText="Rate" ReadOnly="True" SortExpression="rate"/>
<asp:BoundField DataField="cDate" HeaderText="Contract Date" ReadOnly="True" SortExpression="cDate" />
<asp:TemplateField HeaderText = "Action" >
<ItemTemplate>
<asp:LinkButton ID="btnAction" runat="server" Text = "Add" AutoPostBack = "False"
OnClick = "btnAction_Click" CausesValidation="False"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
<div id="ActionStatus" runat="server" visible="false" class="status">
** <asp:Literal ID="ltActionSuccess" runat="server"></asp:Literal> **
</div>
<div>
<br />
Total Deals to be included in this file: <asp:Label ID="lblTotal" class="total" runat="server"></asp:Label>
<br />
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="MainUpdateProgress" runat="server" AssociatedUpdatePanelID="MainUpdate">
<ProgressTemplate>
<div >
Please wait, loading updated content...
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</div>
</div>
</td>...
导入 System.Data
部分公共类 contract_queue 继承 System.Web.UI.Page
Private _DealList As List(Of Integer)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DealList = New List(Of Integer)
FillContractQueue()
End If
End Sub
Public Property DealList() As List(Of Integer)
Get
Return _DealList
End Get
Set(ByVal value As List(Of Integer))
_DealList = value
End Set
End Property
Protected Sub grdvContract_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdvContract.RowDataBound
Dim grid_row As GridViewRow
grid_row = e.Row
If e.Row.RowType = DataControlRowType.DataRow Then
Dim deal_no As Integer = CInt(DataBinder.Eval(grid_row.DataItem, "dealno"))
Dim btnR As LinkButton = grid_row.FindControl("btnAction")
btnR.Attributes.Add("dealid", deal_no)
'If DealList.Contains(dealno) Then
' btnR.Text = "Remove"
'Else
' btnR.Text = "Add"
'End If
End If
End Sub
Protected Sub btnAction_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.ActionStatus.Visible = True
Me.ltActionSuccess.Text = "Test"
Dim total As Integer = 0
total = DealList.Count
Me.lblTotal.Text = total
Dim btnT As New LinkButton
btnT = CType(sender, LinkButton)
Dim deal_id As Integer = btnT.Attributes("dealid")
If DealList.Contains(deal_id) Then
DealList.Remove(deal_id)
btnT.Text = "Add"
total = DealList.Count
Me.ltActionSuccess.Text = "Deal " & deal_id & " added to file"
Me.lblTotal.Text = total
Else
DealList.Add(deal_id)
total = DealList.Count
btnT.Text = "Remove"
Me.ltActionSuccess.Text = "Deal " & deal_id & " removed from file"
Me.lblTotal.Text = total
End If
End Sub
Protected Sub FillContractQueue()
Dim ContractDeals As New DataSet()
'set up db connection'
Dim strConnect As String = ConfigurationManager.ConnectionStrings("ibl_treasuryConnectionString").ConnectionString
Dim objConnection As New System.Data.SqlClient.SqlConnection(strConnect)
Dim strQuery As String
strQuery = "SELECT convert(varchar, t_deal_register.contractdate, 106) AS cDate, t_deal_register.id AS dealno, t_deal_type.dealtype AS Type, t_client.client_name AS Client, t_dealer_ibl.dealer_ibl + '/' + t_dealer_client.dealer_client AS Dealers, t_deal_register.amount AS Amount, t_deal_register.fxrate AS rate, t_status.Status, t_currency.currency + '/' + t_currency_1.currency AS Currency FROM t_deal_register INNER JOIN t_deal_type ON t_deal_register.dealtype = t_deal_type.id INNER JOIN t_client ON t_deal_register.clientname = t_client.id INNER JOIN t_dealer_ibl ON t_deal_register.dealer_ibl = t_dealer_ibl.id INNER JOIN t_dealer_client ON t_deal_register.dealer_client = t_dealer_client.id INNER JOIN t_currency ON t_deal_register.currency = t_currency.id INNER JOIN t_status ON t_deal_register.status = t_status.id INNER JOIN t_currency AS t_currency_1 ON t_deal_register.pay_currency = t_currency_1.id where t_deal_register.status = '14' ORDER BY t_deal_register.id DESC"
Dim objCommand As New System.Data.SqlClient.SqlCommand(strQuery, objConnection)
objConnection.Open()
Try
Dim DealAdapter As New System.Data.SqlClient.SqlDataAdapter(objCommand)
DealAdapter.Fill(ContractDeals)
Catch ex As Exception
Me.lblMessage.Text = ex.Message
Finally
objConnection.Close()
If ContractDeals.Tables.Count > 0 Then
grdvContract.DataSource = ContractDeals
grdvContract.DataBind()
End If
End Try
End Sub
结束类