【问题标题】:finding control in grid view?在网格视图中找到控件?
【发布时间】:2010-02-17 06:23:27
【问题描述】:

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

来源

'> ' 可见 =“假” > '> ' 文本模式="多行" > '> ' 文本模式="多行" >
'> ' />

public void getPlaylist()//我把查找控件写在一个方法里 { MyplalistBL clsMyplalstBl=new MyplalistBL (); clsMyplalstBl.Userid = Session["userid"].ToString(); 数据集 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();

    }
}

【问题讨论】:

  • 我会在我的gridview中清楚地解释一个复选框。当它是“P”时,复选框应该被选中,当它是来自数据库的“PV”时,它应该被取消选中。我怎样才能。如何在网格中声明复选框我这样声明 是否正确

标签: asp.net


【解决方案1】:

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

【讨论】:

  • 您需要检查数据绑定事件中的 rowType 否则如果您尝试在页眉或页脚中找到一个复选框(如果 gridview 有一个),它可能会引发异常
【解决方案2】:

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

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;
        }
    }
}

【讨论】:

    【解决方案3】:

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

    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 事件。

    【讨论】:

    【解决方案4】:

    你也可以这样做:

    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;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 2010-12-30
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      相关资源
      最近更新 更多