2012-09-25 6 views
1

포스트 백 후에 채워지면 GridView의 행을 반복하는 데 어려움을 겪고 있습니다..NET GridView 행이 포스트 백에서 null입니다.

요약하면 GridView 내에 표시되어야하는 데이터를 결정하는 두 개의 드롭 다운 목록이 있습니다. 페이지는 처음에는이 단계에서는 GridView가없는 두 개의 드롭 다운 목록만으로로드됩니다. 드롭 다운에서 값을 선택한 후 사용자는 버튼을 통해 페이지를 제출합니다. 그런 다음 코드를 사용하여 db를 호출하여 데이터를 검색하고이를 GridView에 바인딩합니다. 나는이 일을 할 수 있었다!

문제는 GridView에 사용자가 itemtemplate에있는 텍스트 상자 내의 값을 편집 할 수있는 하나의 열이 있다는 것입니다. 사용자가 텍스트 상자의 값을 변경하고 페이지를 제출하면 GridView의 행을 반복 할 때 반복 할 행이 없다는 메시지가 나타납니다. 프론트 엔드에서 사용할 수있는 행과 데이터를 볼 때 왜 이런 일이 발생하는지 혼란 스럽습니다.

그러나 사용자가 드롭 다운을 선택하고 GridView가 초기 page_load에 채워지는 단계를 제거하면 실제로 행을 반복 할 수 있습니다.

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 
+1

당신이 무엇을 요구하고 있는지 100 % 확신하지 못합니다. 따라서 버튼을 클릭하여 그리드 뷰를 채운 다음 다른 버튼을 클릭하여 변경 사항을 제출하십시오. 하지만 제출할 때 행이 없습니다. – Cruril

+0

예, 바로 행이 없습니다. GridView.Rows를 반복 할 때 0 행이 반환됩니다. – creativeincode

답변

0

나는 우리가 행동이 특정 경우에 일어나는 이유를 설명하기 위해 더 많은 데이터를 필요로 할 수 생각 . 예를 들어, 은 정확한 상황을 더 잘 볼 수 있도록 ASPX (XHTML) 파일 게시를 고려할 수 있습니다. 내가 완전히 당신의 필요를 이해하지 수 있지만

, 그것은 당신이 원하는 가능하다는 것을을 보인다. 아래는 당신의 상황에 대한 나의 이해를 님이 느슨하게 시뮬레이션하기 위해 모은 고안된 예제입니다. 드롭 다운에서 무게 필터 다른 드롭 다운 A로부터 (다시 게시 그러나)

  • 선택 빈의 GridView
  • 선택과

    1. 를 열고 페이지 :

      내가 제출하고 코드는 사용자 수 비용 필터 (포스트 백이 없음)

    2. 데이터 단추를 눌러 GridView 채우기
    3. 각 GridView 행의 설명 필드에 대한 텍스트 정보 유형
    4. 은 문자 그대로 제어

    처리가 서버에서 발생 이후에 서버 측의 수정을 통해 사용자의 선택을

  • 버튼을 제출 관찰 클릭, 내 생각이 코드는에서의 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 
    
  • 관련 문제