当前位置: 首页 > 知识库问答 >
问题:

vb asp中gridview中触发链接按钮单击事件的问题。net web应用程序

伍皓
2023-03-14

我用VB Visual Studio 2008创建了一个web应用程序。我创建了一个新页面来显示一个网格视图,其中显示了数据库中具有特定状态的合同。在网格视图中,我有一个带有链接按钮的项目模板字段。链接按钮的默认文本为“添加”。

单击按钮时,我想将db中合同的id号添加到整数列表中。如果合同id出现在列表中,则按钮的文本应为“删除”,然后应将其从列表中删除。如果列表中没有,按钮的文本应为“添加”,并应添加到列表中。在按钮的点击事件中,我有实现这一点的代码。

页面加载良好,并在网格视图中显示项目,但是当我单击“添加”按钮时,它生成了一个空异常错误,指向我的链接按钮。然后,我将网格视图放在更新面板中,不再出现null异常错误,但现在什么也没发生。

这是aspx代码

       <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.html" target="_blank">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>...

导入系统。数据

部分公共类契约队列继承系统。网状物用户界面。页

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

末班

共有1个答案

赫连华皓
2023-03-14
     <asp:TemplateField>
                     <ItemTemplate>
                         <asp:LinkButton ID="lnkButtonID" runat="server"
                             OnClick="lnkButton_Click"  Text='<%#DataBinder.GetPropertyValue(Container.DataItem, "ID")%>'
                             name = '<%#DataBinder.GetPropertyValue(Container.DataItem, "Name")%>'
                             address = '<%#DataBinder.GetPropertyValue(Container.DataItem, "address")%>'
                             description = '<%#DataBinder.GetPropertyValue(Container.DataItem, "Description")%>'
                         ></asp:LinkButton>
                     </ItemTemplate>
 类似资料:
  • 问题内容: 似乎是一个简单的问题,但解决其他问题的方法似乎对我没有用。 试图通过单击按钮来触发AJAX请求,但似乎并未触发。 HTML示例 javascript 问题答案: 您拥有的代码在拨弄中工作得很好。初始页面加载后,按钮是否通过AJAX动态呈现? 用 代替

  • 我在一个页面上有两个GridView,每个GridView都在代码隐藏中被修改为使用RowDataBound进行分组,在RowDataBound中,我为每个分组添加了一个文本框和按钮。该按钮将添加一个单击事件。此外,每个GridView都位于UpdatePanel中。 为了解决RowDataBound项和动态控件的回发问题,我添加了一个简单的响应。重定向到同一页。这段代码可以工作,但按钮会在Gri

  • 问题内容: 这是我想做的事情:单击页面上的一个按钮,这又使(2)事情发生: 显示一个ModalPopup,以防止用户按下任何按钮或更改值 在方法后面调用我的代码,完成后隐藏ModalPopup 这是ASP标记: 现在,这是我在C#代码后面的代码: 为什么不起作用?ModalPopup可以完美显示,但是btnSaveData_Click事件从不触发。 更新: 第一个建议对我不起作用。我还尝试了您的第

  • > 按常规声明代码 Page language=“vb”autoeventwireup=“false”masterpageFile=“~/site.master”codeBehind=“RetailCompetition.aspx.vb”inherits=“company.applications.production.ui.RetailCompetition” 事件

  • 我正在开发一个chrome扩展。有一个第三方网站是用Vuejs开发的。我需要触发页面上按钮的点击事件。但当我使用JavaScript执行此操作时,页面将重新加载,但当用户手动单击按钮时,它将处理验证&如果任何验证失败,则不要重新加载。 我使用以下代码来单击按钮(jQuery代码): 注意:上面的代码正确地启动了相关控件的click事件,但是页面重新加载而不是等待验证结果。另外,如果我使用JavaS