我有 4 列的网格视图;在第 1 列中,我添加了一个占位符,其他 3 列是绑定字段。在第 1 列中,我使用 html 代码动态添加单选按钮,通过该代码我只能选择一个单选按钮。它运行良好,但问题是当单击gridview 之外的按钮时,我无法找到单选按钮控件。
请帮忙,我已经被这个问题困了4天了。
先感谢您。
我使用了以下代码
.aspx 文件
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" ForeColor="Black" GridLines="Vertical"
onrowdatabound="GridView1_RowDataBound"
>
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="FIRST NAME" DataField="FNAME"/>
<asp:BoundField HeaderText="LAST NAME" DataField="LNAME"/>
<asp:BoundField HeaderText="EMAIL" DataField="EMAIL"/>
<asp:BoundField HeaderText="AGE" DataField="AGE"/>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:Button ID="btnSave" Text="Save" runat="server" onclick="btnSave_Click1" />
</form>
代码隐藏文件
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1 && e.Row.DataItem != null)
{
PlaceHolder holder = (PlaceHolder)e.Row.FindControl("ph");
RadioButton rb = new RadioButton();
rb.ID = "rbSelect";
holder.Controls.Add(rb);
}
}
protected void btnSave_Click1(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
PlaceHolder holder = (PlaceHolder)GridView1.Rows[i].Cells[0].FindControl("ph");
RadioButton rbtn = holder.FindControl("rb") as RadioButton;
if (rbtn.Checked == true)
{
Response.Write("<Script>alert('Radiocheck')</Script>");
}
}
}