在aspx页上放置了如下一个GridView 控件和一个button控件,假定已经为GridView绑定了5行的数据。<asp:GridView ID="gridQA" runat="server" AutoGenerateColumns="False"
onrowcreated="gridQA_RowCreated" ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
字段名:
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate> </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView><asp:Button ID="Button1" runat="server" Text="提交" CssClass="btn" OnClick="btnCommit_Click" />
部分后台代码: protected void gridQA_RowCreated(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow)//在数据行中寻找Panel
{
Panel pl = new Panel();
pl.ID = "pl" + e.Row.RowIndex.ToString();
e.Row.Cells[1].Controls.Add(pl); //在每一行的第二列中添加一个panel
RadioButton rb = new RadioButton();
rb.ID = "rb" + e.Row.RowIndex.ToString();
rb.Text = "rb" + e.Row.RowIndex.ToString(); pl.Controls.Add(rb); //在panel中加入一个RadioButton
}
}
我想要在 btnCommit_Click 事件中获取第2行的RadioButton,即ID为"rb1"的RadioButton 除了: for (int i = 0; i < this.gridQA.Rows.Count; i++)
{
RadioButton pl = (RadioButton)this.gridQA.Rows[i].FindControl("rb1");
}这样循环每一行去查找,还有什么更好的办法吗?
onrowcreated="gridQA_RowCreated" ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
字段名:
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate> </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView><asp:Button ID="Button1" runat="server" Text="提交" CssClass="btn" OnClick="btnCommit_Click" />
部分后台代码: protected void gridQA_RowCreated(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow)//在数据行中寻找Panel
{
Panel pl = new Panel();
pl.ID = "pl" + e.Row.RowIndex.ToString();
e.Row.Cells[1].Controls.Add(pl); //在每一行的第二列中添加一个panel
RadioButton rb = new RadioButton();
rb.ID = "rb" + e.Row.RowIndex.ToString();
rb.Text = "rb" + e.Row.RowIndex.ToString(); pl.Controls.Add(rb); //在panel中加入一个RadioButton
}
}
我想要在 btnCommit_Click 事件中获取第2行的RadioButton,即ID为"rb1"的RadioButton 除了: for (int i = 0; i < this.gridQA.Rows.Count; i++)
{
RadioButton pl = (RadioButton)this.gridQA.Rows[i].FindControl("rb1");
}这样循环每一行去查找,还有什么更好的办法吗?
直接在btnCommit_Click事件下获得!!
protected void btnCommit_Click(object sender, EventArgs e)
{
RadioButton Ra=(RadioButton)GridView1.Rows[1].FindControl("rb1");//Rows[1]是第二行
Response.Write(Ra.Checked.ToString()); }