Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET GridView行在回发时为空_C#_.net_Vb.net_Gridview_Viewstate - Fatal编程技术网

C# .NET GridView行在回发时为空

C# .NET GridView行在回发时为空,c#,.net,vb.net,gridview,viewstate,C#,.net,Vb.net,Gridview,Viewstate,在回发后填充GridView后,我很难对其行进行迭代 总之,有两个下拉列表可确定应在GridView中显示的数据。页面最初只加载了两个下拉列表,在此阶段没有GridView。从下拉列表中选择值后,用户通过按钮提交页面。然后,代码将调用db来检索数据并将其绑定到GridView。我已经设法让它工作了 问题在于GridView有一列,用户可以在其中编辑itemtemplate中保存的文本框中的值。如果用户更改文本框中的值并提交页面,当我尝试迭代GridView的行时,它会告诉我没有要迭代的行。当我可

在回发后填充GridView后,我很难对其行进行迭代

总之,有两个下拉列表可确定应在GridView中显示的数据。页面最初只加载了两个下拉列表,在此阶段没有GridView。从下拉列表中选择值后,用户通过按钮提交页面。然后,代码将调用db来检索数据并将其绑定到GridView。我已经设法让它工作了

问题在于GridView有一列,用户可以在其中编辑itemtemplate中保存的文本框中的值。如果用户更改文本框中的值并提交页面,当我尝试迭代GridView的行时,它会告诉我没有要迭代的行。当我可以看到前端中可用的行和数据时,我不明白为什么会发生这种情况

但是,如果我们删除了用户选择下拉列表的步骤,并在初始页面加载时填充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
我认为我们可能需要更多的数据来描述为什么在你的特殊情况下会发生这种行为。例如,您可能会考虑发布您的ASPX XHTML文件,以便我们可以更好地查看您的确切情况。 虽然我可能不完全理解你的需求,但你想要的似乎是可能的。下面是一个人为的例子,我放在一起,松散地模拟我对你的情况的理解

我提交的代码允许用户:

打开带有空GridView的页面 从下拉列表中选择权重过滤器,但尚未回发 从另一个下拉列表中选择尚未回发的成本筛选器 按下“获取数据”按钮以填充GridView 在文本框中为每个GridView行上的描述字段键入信息 单击提交按钮 通过服务器端对文本控件的修改来观察用户的选择 由于处理是在服务器上进行的,我认为这段代码演示了应该可以按照规定的方式修改GridView并读取GridView更改

如果此代码没有描述您的情况,请添加评论,以便社区能够为您提供更好的帮助:

ASPX模拟文件

代码隐藏模拟文件


不是100%确定你在问什么。所以你点击一个按钮来填充你的gridview,然后点击另一个按钮来提交对它的任何更改?但是当你提交它时,没有行?是的,没错,没有行。当我尝试迭代GridView.Rows时,它表示返回0行。
<%@ 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