2010-02-17 81 views
0

在我的應用我想獲得的複選框這是我使用foreach控制在GridView但shoing空,這是我code./ ..在網格視圖中查找控件?

「> '可見= 「假」> '> ' 的TextMode = 「多」> '> ' 的TextMode = 「多」>
'> '/>

公共無效getPlaylist()//我寫的方法中的發現控制 { MyplalistBL clsMyplalstBl =新MyplalistBL(); clsMyplalstBl.Userid = Session [「userid」]。ToString(); DataSet ds = clsMyplalstBl.getPlaylistBl(); 如果(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(); 

    } 
} 
+0

我會清楚地在我的gridview的講解複選框是存在的。當它是「P」時應該檢查複選框並且當它是來自數據庫的「PV」時不檢查。我怎麼能夠。如何可以聲明的複選框中網格我聲明這樣 的 是它正確 – 2010-02-17 06:37:00

回答

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

是正好Mr.Lakhalani PRASHANT。 – 2010-02-17 06:41:49

+0

你需要在數據綁定事件來檢查ROWTYPE否則它可能會拋出了一個異常,如果ü嘗試找到在頁眉或頁腳中的複選框(如果將GridView有一個) – 2010-02-17 06:45:42

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

0

將開始工作

+0

確定我會嘗試,並告知您 – 2010-02-17 06:44:40

+0

這裏有一個體面的樣本有一個複選框,可能是類似於您的情況:http://highoncoding.com/Articles/81_Selecting_Checkboxes_inside_GridView_Control.aspx – 2010-02-17 06:55:57

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