我正在尝试添加GridView,并且在最后一列中添加了一个按钮。它什么也没做,我做了一个新标签,看看它是否返回了任何数据,但它似乎只在最后一行工作。你看到我的代码有什么问题吗?提前感谢!
HTML
<asp:Button runat="server" Text="Submit" OnClick="ButtonClick8" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
C#
protected void ButtonClick8(object sender, System.EventArgs e)
{
Button button2 = (Button)sender;
GridViewRow row2 = (GridViewRow)button2.NamingContainer;
int i = 0;
int string1;
foreach (GridViewRow gvr in GridView2.Rows)
{
if (gvr == row2)
{
string1 = Convert.ToInt32(gvr.Cells[0].Text);
Label1.Text = string1.ToString();
}
else
{
Label1.Text = "no";
}
}
再次感谢!
foreach循环一直执行到最后一列并覆盖标签文本。gvr==行2:时退出循环
foreach (GridViewRow gvr in GridView2.Rows)
{
if (gvr == row2)
{
string1 = Convert.ToInt32(gvr.Cells[0].Text);
Label1.Text = string1.ToString();
break;
}
else
{
Label1.Text = "no";
}
}