从动态生成的网格视图中选择控件

时间:2015-01-13 09:49:55

标签: asp.net .net vb.net

下面是我拥有的网格视图的代码。 Gird View由数据库返回的一组数据填充。

<asp:GridView ID="GridView1" runat="server" CellPadding="3" AutoGenerateColumns="False" EnableModelValidation="True">
  <RowStyle BackColor="#000000" ForeColor="#8C4510" />
  <FooterStyle BackColor="#000000" ForeColor="#8C4510" />
  <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
  <SelectedRowStyle BackColor="#000000" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#FF9933" Font-Bold="True" ForeColor="Black" />
  <Columns>
    <asp:TemplateField HeaderText="Requestor">
      <ItemTemplate>
        <asp:Label ID="Requestor" runat="server" Text='<%# Bind("REQUESTED_BY") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Card Number">
      <ItemTemplate>
        <asp:Label ID="CardNumber" runat="server" Text='<%# Bind("CARD_NUMBER") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Date Submitted">
      <ItemTemplate>
        <asp:Label ID="DateSubmitted" runat="server" Text='<%# Bind("DATE_SUBMITTED") %>'></asp:Label>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Confirm?">
      <ItemTemplate>
        <asp:CheckBox ID="chkConfirm" runat="server" Checked='<%# Bind("EMAIL_SENT") %>' />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" />

当在网格视图中勾选一个方框时,用户应该可以按一个调用ConfirmCards_Click Sub的按钮。

Protected Sub ConfirmCards_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ConfirmCards.Click

    Dim NumberOfRows = GridView1.Rows.Count
    Dim i As Integer = 0

    Dim ConfirmValue As CheckBox
    Dim CardNumber As Label

    Dim CardsConfirmed As String
    Dim RunNumber As Integer = 0

    For i = 0 To NumberOfRows - 1

        ConfirmValue = GridView1.Rows(i).FindControl("chkConfirm")
        CardNumber = GridView1.Rows(i).FindControl("CardNumber")

        MsgBox(ConfirmValue.Checked & " " & CardNumber.Text, vbOKOnly, "Card and checked status")

    Next

End Sub

在实际系统中,此子将具有标记数据库中字段的功能,确认卡已被确认。

但是,当单击调用ConfirmCards_Click子的按钮时,警告框始终返回False和行的CardNumber,无论是否已勾选chkConfirm

我该如何解决这个问题?我在另一个页面中有非常相似的代码,它可以正常运行 - 是否需要将一些设置应用于页面,以便服务器意识到复选框已被勾选?

编辑:根据要求包含Page_Load VB代码。

Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim qryRaisedCards As String

    If TeamName.Text = "Admin" Then
        qryRaisedCards = "select CARD_NUMBER, REQUESTED_BY, DATE_SUBMITTED, EMAIL_SENT from SUBMISSIONS where EMAIL_SENT = 0 order by DATE_SUBMITTED, CARD_NUMBER"
    Else
        qryRaisedCards = "select CARD_NUMBER, REQUESTED_BY, DATE_SUBMITTED, EMAIL_SENT from SUBMISSIONS where EMAIL_SENT = 0 and TEAM_NAME = :TeamName order by DATE_SUBMITTED, CARD_NUMBER"
    End If

    Using cn2 As New OracleConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString), _
        cmd2 As New OracleCommand(qryRaisedCards, cn2)

        If TeamName.Text <> "Admin" Then
            cmd2.Parameters.Add(":TeamName", OracleDbType.Varchar2, 60).Value = TeamName.Text
        End If

        Dim RaisedCardsAdapter As New OracleDataAdapter(cmd2)
        Dim RaisedCardsDataset As New DataSet()
        RaisedCardsAdapter.Fill(RaisedCardsDataset, "UserPermissions")

        cn2.Open()
        cmd2.ExecuteNonQuery()
        cn2.Close()

        If RaisedCardsDataset.Tables(0).Rows.Count > 0 Then
            ErrorMessage.Visible = False
            GridView1.DataSource = RaisedCardsDataset
            GridView1.DataBind()
        Else
            GridViewPanel.Visible = False
            ErrorMessage.Visible = True
            ErrorMessage.Text = "There are no submitted cards that have not been confirmed at this time."
        End If

    End Using

End Sub

1 个答案:

答案 0 :(得分:1)

从代码我看到的是你的gridview也受到了回发的约束。这导致其被回收的州的价值减少。尝试在If If IsPostback中包装你的Page_Load代码。

Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim qryRaisedCards As String
If Not IsPostBack Then
    If TeamName.Text = "Admin" Then
        qryRaisedCards = "select CARD_NUMBER, REQUESTED_BY, DATE_SUBMITTED, EMAIL_SENT from SUBMISSIONS where EMAIL_SENT = 0 order by DATE_SUBMITTED, CARD_NUMBER"
    Else
        qryRaisedCards = "select CARD_NUMBER, REQUESTED_BY, DATE_SUBMITTED, EMAIL_SENT from SUBMISSIONS where EMAIL_SENT = 0 and TEAM_NAME = :TeamName order by DATE_SUBMITTED, CARD_NUMBER"
    End If

    Using cn2 As New OracleConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString), _
        cmd2 As New OracleCommand(qryRaisedCards, cn2)

        If TeamName.Text <> "Admin" Then
            cmd2.Parameters.Add(":TeamName", OracleDbType.Varchar2, 60).Value = TeamName.Text
        End If

        Dim RaisedCardsAdapter As New OracleDataAdapter(cmd2)
        Dim RaisedCardsDataset As New DataSet()
        RaisedCardsAdapter.Fill(RaisedCardsDataset, "UserPermissions")

        cn2.Open()
        cmd2.ExecuteNonQuery()
        cn2.Close()

        If RaisedCardsDataset.Tables(0).Rows.Count > 0 Then
            ErrorMessage.Visible = False
            GridView1.DataSource = RaisedCardsDataset
            GridView1.DataBind()
        Else
            GridViewPanel.Visible = False
            ErrorMessage.Visible = True
            ErrorMessage.Text = "There are no submitted cards that have not been confirmed at this time."
        End If

    End Using
    End If
End Sub