【问题标题】:How to find a dynamic control (radio button) inside a placeholder in gridview如何在gridview的占位符内找到动态控件(单选按钮)
【发布时间】:2013-01-09 04:20:49
【问题描述】:

我有 4 列的 gridview;在第 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>");
            }
        }
    }

【问题讨论】:

    标签: asp.net gridview radio-button


    【解决方案1】:

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

    然而,

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

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

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

    但不要像&lt;input type='radio' 这样添加html-control,您应该创建并添加一个带有id 的RadioButton。否则您以后将无法访问它,因此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);
            }
        }
    }
    

    【讨论】:

    • 如果你想发表评论,不要试图编辑我的答案;)当然我的代码只是一个例子,我不知道你使用什么数据源(如果有一个字段名称SampleActive)。
    • 你能解释一下行 "rb.Checked = row.Field("SampleActive");" 的用途是什么我在这一行出现错误
    • @sree:如果您在 SO 上提及 errors,请始终说出您遇到的 what 错误。如果您没有使用我在RowCreated 中的代码来创建ID="RbSample" 的RadioButtonrb 可能为空。
    • 列'Select'不属于表Table。如果我删除上面提到的行(rb.Checked = row.Field("SampleActive");) iam gettin error at "if(rbtn.Checked)" Object reference,我只想要单选按钮列而不绑定数据库中的任何数据未设置为对象的实例。
    • @sree:正如我在第一条评论中提到的,这只是一个例子。我不知道您的数据库架构(列、类型)。我假设它是一个bool 列(sql-server 中的位)和列名“SampleActive”。
    【解决方案2】:

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

    我也不知道你是否可以循环通过 holder 的 Controls 属性,因为它是一个普通的 html 控件。但是您可以尝试在 FindControl 行上放置一个断点并探索 holder 的 Controls 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多