2012-09-25 29 views
1

我在遍歷GridView的行後,在回發後填充GridView行時遇到困難。.NET GridView行在回發中爲空

總之,有兩個下拉列表確定應該在GridView中顯示的數據。該頁面最初僅加載兩個下拉列表,在此階段沒有GridView。從下拉菜單中選擇值後,用戶通過按鈕提交頁面。後面的代碼調用數據庫來檢索數據並將其綁定到GridView。我已經設法得到這個工作!

問題是,GridView有一列,用戶可以在項目模板中的文本框中編輯值。如果用戶更改文本框中的值並提交頁面,當我嘗試遍歷GridView的行時,它告訴我沒有要迭代的行。我很困惑,爲什麼會發生這種情況,因爲我可以在前端查看行和數據。

但是,如果我們刪除涉及用戶選擇下拉列表的步驟,並且在初始page_load上填充了GridView,我實際上可以遍歷行。

因此,就好像ViewState不會持續回發之間的行數(如果GridView綁定在按鈕單擊事件中)。

有人可以向我解釋,發生了什麼事。

下面的代碼在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 
+1

不是100%確定你在問什麼。所以你點擊一個按鈕來填充你的gridview,然後點擊另一個來提交它的任何更改?但是當你提交它時,沒有行? – Cruril

+0

是的,這是沒有行。當我嘗試迭代GridView.Rows時,它說0行正在返回。 – creativeincode

回答

0

認爲我們可能需要更多的數據來說明爲什麼該行爲在特定情況下發生。例如,您可能會考慮發佈您的ASPX(XHTML)文件,以便我們可以更好地瞭解您的確切情況。

雖然我可能不完全瞭解您的需求,它似乎,你想要的是可能的。下面是一個人爲的例子,我放在一起寬鬆地模擬我的瞭解你的情況。

我提交的代碼允許用戶:一個空的GridView

  • 選擇

    1. 打開一個頁面從下拉權重過濾器從另一個下拉列表中選擇(無回傳然而)
    2. 選擇成本過濾器(無回發)
    3. 推送獲取數據按鈕以填充GridView
    4. 在每個GridView行的描述字段的文本框中鍵入信息
    5. 點擊通過服務器端的修改提交按鈕
    6. 觀察用戶的選擇,文字控制

    由於處理在服務器上時,我覺得這個代碼演示,它應該是可以修改一個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