1

ポストバック後にデータが入力されると、GridView の行を反復処理するのが困難です。

要約すると、GridView 内に表示する必要があるデータを決定する 2 つのドロップダウン リストがあります。このページには、最初は 2 つのドロップダウン リストのみが読み込まれ、この段階では GridView は読み込まれません。ドロップダウンから値を選択した後、ユーザーはボタンを使用してページを送信します。コード ビハインドは、db を呼び出してデータを取得し、これを GridView にバインドします。私はこれを機能させることができました!

問題は、GridView に、itemtemplate に保持されているテキスト ボックス内の値をユーザーが編集できる列が 1 つあることです。ユーザーがテキスト ボックスの値を変更してページを送信すると、GridView の行を反復処理しようとすると、反復処理する行がないことがわかります。フロントエンドで利用可能な行とデータを確認できるのに、なぜこれが起こっているのか混乱しています。

ただし、ユーザーがドロップダウンを選択するステップを削除し、最初の page_load で GridView を設定すると、実際に行を反復処理できます。

したがって、GridView がボタン クリック イベント内にバインドされている場合、ViewState がポストバック間の行数を保持していないかのようになります。

何が起こっているのか、誰かが私に説明してくれませんか。

VB.NET のコードは次のとおりです。

Protected Sub Me_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.Load

End Sub

Protected Sub BtnGetData_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles BtnGetData.Click

    Dim ddlFirstValue As Integer = DropDownListOne.SelectedValue
    Dim ddlSecondValue As Integer = DropDownListSecond.SelectedValue
    MyGridView.DataSource = GetData(ddlFirstValue, ddlSecondValue)
    MyGridView.DataBind()

End Sub

Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles BtnSubmit.Click

    For Each row As GridViewRow In MyGridView.Rows 'this returning 0 rows
        'Do something
    Next

End Sub
4

1 に答える 1

0

特定のケースで動作が発生している理由を説明するには、さらにデータが必要になると思います。たとえば、 ASPX (XHTML) ファイルを投稿して、正確な状況をより詳しく把握できるようにすることを検討してください。

私はあなたのニーズを完全には理解していないかもしれませんが、あなたが望んでいることは可能であるようです. 以下は、あなたの状況に対する 私の理解を大まかにシミュレートするためにまとめた不自然な例です。

私が提出しているコードにより、ユーザーは次のことができます。

  1. 空の GridView でページを開く
  2. ドロップダウンから重みフィルターを選択します (ポストバックはまだありません)
  3. 別のドロップダウンからコスト フィルターを選択します (ポストバックはまだありません)
  4. [データの取得] ボタンを押して GridView にデータを設定する
  5. 各 GridView 行の説明フィールドの TextBoxes に情報を入力します。
  6. 送信ボタンをクリックします
  7. サーバー側でリテラル コントロールを変更することにより、ユーザーの選択を観察する

処理はサーバー上で行われるため、このコードは、事前に定義された方法で GridView を変更し、GridView の変更を読み取ることができることを示していると思います。

このコードがあなたの状況を説明していない場合は、コミュニティがより良い支援を提供できるようにコメントを追加してください:

ASPX シミュレーション ファイル

<%@ Page Language="VB" AutoEventWireup="false" 
CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>
    Demo of using DropDown Lists to Filter Data for 
    Editable DataGrid
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Literal ID="theLiteral" runat="server" />
    </div>
    <br />
    <div>
        <asp:GridView ID="myGridView" runat="server" 
        AutoGenerateColumns="false" 
        DataKeyNames="weight, cost, Description">
            <Columns>               
                <asp:BoundField DataField="weight" HeaderText="Weight" /> 
                <asp:BoundField DataField="cost" HeaderText="Cost" /> 
                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="descId"
                        Text='<%# Bind("Description") %>' />                          
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView><br />
        Get Items With Weight >=:<br />
        <asp:DropDownList ID="DropDownListOne" runat="server">
            <asp:ListItem Text="1" Value="1" />            
            <asp:ListItem Text="2" Value="2" />      
            <asp:ListItem Text="5" Value="5" />                  
        </asp:DropDownList><br />
        Get Items With Cost >=:<br />
        <asp:DropDownList ID="DropDownListSecond" runat="server">
            <asp:ListItem Text="1" Value="1" />            
            <asp:ListItem Text="5" Value="5" />      
            <asp:ListItem Text="51" Value="51" />    
            <asp:ListItem Text="101" Value="101" />                                
        </asp:DropDownList><br />
        <asp:Button ID="BtnGetData" runat="server" Text="Get Data" /><br />
        <asp:Button ID="BtnSubmit" runat="server" Text="Submit" /><br />
    </div>
    </form>
</body>
</html>

コード ビハインド シミュレーション ファイル

' Just some DataItem to stick in an IEnumerable Array to be 
' bound to CreativeInCode's MyGridView
public Class InventoryItem
    Public Sub New (
        ByVal iWeight As Integer, 
        ByVal iCost As Integer, 
        ByVal iDescription As String) 

        weight = iWeight
        cost = iCost
        description = iDescription
    End Sub

    ' Automatic VB.NET properties can be read about here:
    ' http://msdn.microsoft.com/en-us/library/dd293589.aspx
    Public Property weight As Integer
    Public Property cost As Integer
    Public Property description As String
End Class

' The code behind for Default.aspx starts here
Partial Class _Default
    Inherits System.Web.UI.Page

    ' Data to play with 
    Public storeItems As InventoryItem() = 
    {
        New InventoryItem(10, 20, "PaperWeight"), 
        New InventoryItem(1, 1, "Feather"), 
        New InventoryItem(2000, 20000, "Used SUV"), 
        New InventoryItem(3, 50, "Biology TextBook"), 
        New InventoryItem(1, 200, "Professional Isolation Headphones"), 
        New InventoryItem(1, 100, "Caviar (Does this Need to Be Refrigerated?)")
    }

    ' Function to simulate retrieval from DataBase
    Protected Function GetData( _ 
        ByVal weight As Integer, 
        ByVal cost As INteger
    ) As IEnumerable(Of InventoryItem)
        ' Example of LINQ can be found here
        ' http://msdn.microsoft.com/en-us/vstudio/bb688088.aspx
        ' I believe LINQ is the way of the future
        Dim returnValue As IEnumerable(Of InventoryItem) = 
            From items In storeItems
            Where (items.weight >= weight) AndAlso (items.cost >= cost)
            Select items
        Return returnValue
    End Function

     ' CreativeInCode's STuff starts here
    Protected Sub Me_Load(ByVal sender As Object, ByVal e As EventArgs) _ 
        Handles Me.Load 

    End Sub 

    Protected Sub BtnGetData_Click(ByVal sender As Object, ByVal e As EventArgs) _ 
        Handles BtnGetData.Click 

        Dim ddlFirstValue As Integer = DropDownListOne.SelectedValue 
        Dim ddlSecondValue As Integer = DropDownListSecond.SelectedValue                 
        MyGridView.DataSource = GetData(ddlFirstValue, ddlSecondValue) 
        myGridView.DataBind()

    End Sub 

    ''' <summary>
    ''' Made some modifications to CreativeInCode's function to 
    ''' dump the output to the user
    ''' </summary>                
    Protected Sub BtnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) _ 
        Handles BtnSubmit.Click 

        Dim outputToUser As String = _
            "The user put the following values in the GridView:<br />" 
        For Each row As GridViewRow In MyGridView.Rows 'this returning 0 rows 
            'Do something 

            ' This is just here to make a good place to set a 
            ' breakpoint            
            Dim weightStr As String = row.Cells(0).Text
            Dim costStr As String = row.Cells(1).Text
            Dim tbDescription As TextBox = row.FindControl("descId")
            Dim description As String = tbDescription.Text

            outputToUser &= 
                String.Format(
                    "weight={0}, cost={1}, description={2}<br />", _
                    weightStr, _ 
                    costStr, _
                    description
                )           
        Next 

        ' Show the user the changes she/he made to the GridView
        theLiteral.Text = outputToUser
    End Sub 
End Class
于 2012-09-29T04:54:49.723 に答える