开发者

finding control in grid view?

开发者 https://www.devze.com 2022-12-20 15:25 出处:网络
in my application i am trying to get the checkbox which is in the gridview i use the foreach control but it is shoing null this is my code./..

in my application i am trying to get the checkbox which is in the gridview i use the foreach control but it is shoing null this is my code./..

source

'> ' Visible ="false" > '> ' TextMode="multiLine" > '> ' TextMode="multiLine" >

'> ' />

public void getPlaylist()//i write the finding control in a method { 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]["settin开发者_JAVA技巧gs"].ToString();

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


        }


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

    }
}


Well, this is interesting... It looks that you want to load your check box status from database, so, what you should do is, shift your code to grid view's databound event and it would start working


if you want to check the checkbox based on value in table you can use the Row_DataBound event

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


That looks correct you should have something like:

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

For markup:

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

So I would try to make sure your naming is correct. Make sure you have it named "chksett" indeed.

If that doesn't work move it to an RowBound or ItemBound event.


you can also do this :

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

精彩评论

暂无评论...
验证码 换一张
取 消