在网格视图中查找控件?

时间:2010-02-17 06:23:28

标签: asp.net

在我的应用程序中我试图获取gridview中的复选框我使用foreach控件但是它显示为null这是我的代码./..

                                                                        “>                      'Visible =“false”>                                                                                              “>                                                            'TextMode =“multiLine”>                                                                                              “>                                                            'TextMode =“multiLine”>                                    
                                                       “>                                                             '/>                                    
                                                                                                                                          

public void getPlaylist()//我在方法中编写查找控件     {         MyplalistBL clsMyplalstBl = new MyplalistBL();         clsMyplalstBl.Userid = Session [“userid”]。ToString();         DataSet ds = clsMyplalstBl.getPlaylistBl();         if(ds.Tables [0] .Rows.Count> 0)         {

        grdplaylist .DataSource =ds.Tables [0];
        grdplaylist.DataBind();

        foreach (GridViewRow gr in grdplaylist.Rows)
        {
            CheckBox ch = (CheckBox)gr.FindControl("chksett");
            string s = ds.Tables[0].Rows[0]["settings"].ToString();

            if (s == "P")
            {
                ch.Checked = true;
            }
            else if (s == "PV")
            {
                ch.Checked = false;
            }


        }


    }
    else
    {
        grdplaylist.DataSource = null;
        grdplaylist.DataBind();

    }
}

4 个答案:

答案 0 :(得分:2)

嗯,这很有趣...... 看起来您要从数据库加载复选框状态, 所以,你应该做的是,将你的代码转移到网格视图的数据绑定事件,它将开始工作

答案 1 :(得分:0)

如果要根据表中的值选中复选框,可以使用RowDataBound事件

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow & (e.Row.RowState == DataControlRowState.Normal | e.Row.RowState == DataControlRowState.Alternate)) {
        CheckBox cb = (CheckBox)e.Row.FindControl("CheckBoc1");
        string s= ((DataRowView)e.Row.DataItem).Row("settings");
        if (s== "P") {
            cb.Checked = true;
        }
        else if (s== "PV") {
            cb.Checked = false;
        }
    }
}

答案 2 :(得分:0)

看起来你应该有类似的东西:

foreach (GridViewRow row in GridView1.Rows)
{
    string dropDownListText = ((DropDownList)row.FindControl("DropDownList1")).SelectedItem.Value;
}

标记:

<ItemTemplate>
<asp:DropDownList ID="DropDownList1" DataTextField="Name" DataValueField = "Name" DataSource= '<%# BindDropDownList() %>' runat="server">
 </asp:DropDownList>
</ItemTemplate>

所以我会尽力确保你的命名是正确的。确保你确实将它命名为“chksett”。

如果不起作用,请将其移至RowBound或ItemBound事件。

答案 3 :(得分:0)

你也可以这样做:

foreach (GridViewRow gr in grdplaylist.Rows)
        {
            CheckBox ch = (CheckBox)gr[gr.RowIndex].FindControl("chksett");
            string s = ds.Tables[0].Rows[0]["settings"].ToString();

            if (s == "P")
            {
                ch.Checked = true;
            }
            else if (s == "PV")
            {
                ch.Checked = false;
            }
        }