如何在gridview中的占位符内查找动态控件(单选按钮)

时间:2013-01-25 08:11:57

标签: asp.net gridview radio-button

我有4列gridview;在第1列中,我添加了一个占位符,其他3列是boundfields。在第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>");
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

目前尚不清楚为什么需要创建动态RadioButton。在这种情况下,如果没有任何好处,这会使一切变得更加困难(即使嵌套的RepeaterGridView会更容易)。

然而,

您不应在RowDataBound中创建动态控件,因为仅当GridView是数据绑定时才会触发。但是,由于默认ViewState已启用,因此您不会在回发时DataBind。另一方面,动态控件必须在每次回发时重新

因此,在每个回发时触发的RowCreated中创建它们。但请注意,您(当然)没有DataItem,因为在此阶段它是null(即使网格是数据绑定的)。

因此,您应该在RowCreated中创建动态控件,但可以从RowDataBound加载它们,您也可以在其中访问它们(例如,通过FindControl)。

但是,您应该创建并添加带有ID的<input type='radio',而不是添加像RadioButton这样的html控件。否则您将无法在以后访问它,因此holder.FindControl("rb")null,因为它不是服务器控件。

以下是完整的修改代码:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        PlaceHolder holder = (PlaceHolder)e.Row.FindControl("ph");
        var rb = new RadioButton();
        rb.ID = "RbSample";
        rb.Text = "rb";
        holder.Controls.Add(rb);
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var row = ((DataRowView)e.Row.DataItem).Row;
        var rb = (RadioButton)e.Row.FindControl("RbSample");
        rb.Checked = row.Field<bool>("SampleActive");
    }
}


protected void btnSave_Click1(object sender, EventArgs e)
{
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        RadioButton rbtn = (RadioButton)GridView1.Rows[i].FindControl("RbSample");
        if (rbtn.Checked)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Radiocheck');", true);
        }
    }
}

答案 1 :(得分:0)

看起来你正在使用输入的“值”进行FindControl,但是没有使用控件的ASP.NET ID,因为它没有。因为它不是ASP.NET控件,所以使用FindControl无法找到它。

我也不知道你是否可以遍历holder的Controls属性,因为它是一个简单的html控件。但是你可以尝试在FindControl行上设置断点并探索holder的Controls属性。

相关问题