2012-01-23 61 views
0

我有一個Web應用程序並使用gridview顯示一些SQL數據。在GV中,我在第一列中有一個複選框。正如標題中所建議的那樣,我想在用戶點擊該行的任何地方時勾選複選框。我怎麼能做到這一點。謝謝。asp:GridView - 當點擊行中的任意位置時更改複選框狀態

這是我的GV;

<asp:GridView ID="myGV" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" 
      OnRowDataBound="myGV_OnRowDataBound"> 
      <Columns> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:CheckBox ID="myCB" runat="server" /> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" /> 

我使用此代碼以突出顯示的行當鼠標指針懸停它;

protected void myGV_OnRowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow) 
    { 
     e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#D9ECFB'"); 
     e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;"); 
    } 
} 

編輯:

我補充說:「OnSelectedIndexChanging」和「OnSelectedIndexChanged」到GridView,然後嘗試這種(由拉維的建議),但不能得到它的工作。

protected void myGV_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e) 
{ 
    GridViewRow row = myGV.Rows[e.NewSelectedIndex]; 

    CheckBox chk = (CheckBox)myGV.FindControl("cbIzpis"); 

    if (chk.Checked == true) 
    { 
     chk.Checked = false; 
    } 
    else if (chk.Checked == false) 
    { 
     chk.Checked = true; 
    } 
} 

protected void myGV_SelectedIndexChanged(Object sender, EventArgs e) 
{ 
    GridViewRow row = nyGV.SelectedRow; 
} 

回答

2

可以使用GridView.SelectedRow財產,與SelectedIndexChanged事件,Gridview Selected Row

void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e) 
    { 

    // Get the currently selected row using the SelectedRow property. 
    GridViewRow row = CustomersGridView.SelectedRow; 
    MessageLabel.Text = "You selected " + row.Cells[2].Text + "."; 

    } 

    void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e) 
    { 

    // SelectedIndexChanging event occurs before the select operation in the GridView control, the 
    // SelectedRow property cannot be used. Instead, use the Rows collection 
    // and the NewSelectedIndex property of the e argument passed to this 
    // event handler. 
    GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex]; 
    // here you can check the checkbox, by accessing it in template column using findControl method 
    CheckBox chk = (CheckBox) CustomersGridview.FindControl("chkboxID"); 
    chk.checked;  


    if (row.Cells[1].Text == "SomeCondition") 
    { 

     e.Cancel = true; 
     MessageLabel.Text = "You cannot select " + row.Cells[2].Text + "."; 

    } 

    } 
+0

日Thnx,將嘗試:) – user1080533

+0

沒有奏效。 :(我用我試過的代碼編輯了這個問題,你能檢查一下你是否看到了這個問題嗎? – user1080533

+0

什麼不起作用/?任何錯誤?或者什麼? –

相關問題