2013-10-18 85 views
1

我正在使用vb.net,我有一個gridview,其中包含四個列,包括Textbox作爲Itemtemplate字段。這個gridview包含了學生的信息,textbox是每個學生的出勤狀態。因此,要求將gridview中存在的所有文本框的輸入作爲每個學生出勤的輸入。在這裏,啓用尋呼功能是因爲有時候數字或學生可能超過80。但問題是,當我通過gridview行循環獲取文本框輸入時,它只是取第一頁的值,其餘的都保留下來。我真的需要幫助。任何幫助表示讚賞。如何在所有頁面中遍歷gridview的所有行?

這是GridView控件代碼:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
     AutoGenerateColumns="False" BorderColor="Black" BorderStyle="Solid" 
     CellPadding="4" Font-Bold="True" Font-Size="Small" ForeColor="#333333" 
     OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="20"> 
     <Columns> 
      <asp:TemplateField HeaderText="No."> 
       <ItemTemplate> 
        <%# Container.DataItemIndex + 1 %> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="crkod" HeaderText="Student ID" /> 
      <asp:BoundField DataField="crnama" HeaderText="Student Name" /> 
      <asp:TemplateField HeaderText="Attendance Status" 
       ItemStyle-HorizontalAlign="Center"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtAttend" runat="server" BackColor="Control" MaxLength="1" 
         Width="12px"></asp:TextBox> 
       </ItemTemplate> 
       <ItemStyle HorizontalAlign="Center" /> 
      </asp:TemplateField> 
     </Columns> 
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle Font-Bold="True" /> 
    </asp:GridView> 

這裏是保存按鈕背後的代碼:提前

For k As Integer = 0 To Me.GridView1.PageCount - 1 
     Me.GridView1.PageIndex = k 

     Dim rowNum As Integer = Me.GridView1.Rows.Count 

     For i As Integer = 0 To rowNum - 1 
      Dim tb As TextBox = DirectCast(GridView1.Rows(i).FindControl("txtAttend"), TextBox) 
      attendSts = tb.Text 


      studntID = GridView1.Rows(i).Cells(1).Text 


      Sql = "INSERT INTO attendance (studentID,attendStatus,attendDate,courseID,yearsem,monthsem)" _ 
      & " VALUES (" _ 
      & "'" & studntID & "'," _ 
      & "'" & attendSts & "'," _ 
      & "'" & attnDate & "'," _ 
      & "'" & courseCode & "'," _ 
      & yearsem & "," _ 
      & monthsem & ")" 

      CreateCommand(Sql, strConn) 
     Next 
    Next 

感謝。

+0

嘗試 – rsmith

+0

感謝每一頁循環後綁定你的GridView的回覆。我嘗試了你的建議,但仍然一樣。任何其他想法? – barsan

回答

0

你永遠不應該永遠(這是新程序員的一個流行問題)嘗試直接使用網格中的數據(幾乎不會)。您應該使用您綁定網格的數據。這是你在page_load或DataBinding事件中最有可能設置的GridView1.Datasource = ?

假設這是一個數據表,你會希望做這樣的事情:

For each dr as datarow in ctype(GridView1.Datasource,DataTable).Rows 
    attendSts = dr("Attend") 
    studntID = dr("studntID") 
    ..Your insert code here 
Next 
+0

非常感謝您的幫助。但是,請你讓我更清楚一點,我可以如何在網格內的模板字段文本框中獲取用戶輸入。我很新,所以我真的需要幫助。謝謝。 – barsan

1

你可以用我用它在我的項目低於該命令。它的邏輯非常簡單,你瀏覽所有頁面並瀏覽所有行的每一頁。您也可以在此之前和循環所有你能回到那裏之後讓你的當前頁;)

//Get Current Page Index so You can get back here after commands 
       int a = GridView1.PageIndex; 
    //Loop through All Pages 
       for (int i = 0; i < GridView1.PageCount; i++) 
       { 
    //Set Page Index 
        GridView1.SetPageIndex(i); 
    //After Setting Page Index Loop through its Rows 
        foreach (GridViewRow row in GridView1.Rows) 
        { 
         //Do Your Commands Here 
        } 
       } 
    //Getting Back to the First State 
       GridView1.SetPageIndex(a);