Asp.net 如何循环浏览所有页面中gridview的所有行?
我使用的是vb.net,我有一个gridview,它包含四列,其中包括作为Itemtemplate字段的Textbox。此gridview包含学生信息,文本框是学生每天的出勤状态。因此,要求将gridview中所有文本框中的输入作为每个学生出勤率的输入。在这里,分页是启用的,因为数字或学生有时可能超过80。但问题是,当我在gridview行中循环以获取文本框输入时,它只获取第一页的值,其余的都保留了。我真的需要帮助。感谢您的帮助Asp.net 如何循环浏览所有页面中gridview的所有行?,asp.net,vb.net,gridview,Asp.net,Vb.net,Gridview,我使用的是vb.net,我有一个gridview,它包含四列,其中包括作为Itemtemplate字段的Textbox。此gridview包含学生信息,文本框是学生每天的出勤状态。因此,要求将gridview中所有文本框中的输入作为每个学生出勤率的输入。在这里,分页是启用的,因为数字或学生有时可能超过80。但问题是,当我在gridview行中循环以获取文本框输入时,它只获取第一页的值,其余的都保留了。我真的需要帮助。感谢您的帮助 //Get Current Page Index so You c
//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);
这是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>
//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);
提前感谢。您永远不应该尝试直接使用网格中的数据(好的,几乎永远不应该)。您应该使用绑定到网格的数据。这是您在页面加载或某些数据绑定事件中设置为GridView1.Datasource=?
的内容
//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);
假设这是一个数据表,您可能希望执行以下操作:
For each dr as datarow in ctype(GridView1.Datasource,DataTable).Rows
attendSts = dr("Attend")
studntID = dr("studntID")
..Your insert code here
Next
//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);
您可以使用下面的命令,我正在项目中使用这些命令。它的逻辑很简单,你浏览所有的页面,在每一页你浏览所有的行。您还可以在执行此操作之前获取当前页面,并在循环所有内容之后返回该页面;)
//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);
//获取当前页面索引,以便在执行命令后返回此处
int a=GridView1.PageIndex;
//循环浏览所有页面
对于(int i=0;i
尝试在每个页面循环后绑定gridview谢谢您的回复。我试过你的建议,但还是一样。还有其他想法吗?非常感谢你的回答。但是你能不能让我更清楚一点,我如何在网格内的模板字段文本框中获得用户输入。我在这方面很新,所以我真的需要帮助。非常感谢。