2012-02-10 60 views
1

我有一個gridview複選框asp:TemplateField。我希望選擇多個複選框,然後在按鈕單擊時,我想將這些選定行添加到由Repeater組成的另一個頁面中。同時,我想讓用戶在單擊任何行時選擇單個行。如何停止在gridview中執行rowcommand事件?

我的問題是,當我點擊任何複選框,rowcommand被解僱,一個記錄被添加到中繼器。 我希望阻止此rowcommand事件在複選框被選中/取消選中時被觸發。

請在這個問題上指導我。 謝謝。

+1

將autopostback屬性設置爲false爲該複選框/ – rahularyansharma 2012-02-10 08:15:27

+0

我曾嘗試過,但它不起作用。rowcommand仍然被解僱。是否有任何其他解決方法? – Warrior 2012-02-10 08:23:00

+0

是你試圖添加行復選框檢查事件??? – Madhu 2012-02-10 08:33:37

回答

0

我認爲,這樣當用戶點擊該行的行被選中,你已經添加的整行的JavaScript。該複選框是該行的一部分,是什麼原因的立即回發。因此,您需要將Javascript添加到沒有複選框單元格的所有單元格。根據您的評論

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) { 
    if (e.Row.RowType == DataControlRowType.DataRow) { 
     var chechBoxColumnIndex=1; 
     var highlightColor = "yellow"; 
     var cells=(e.Row.Cells 
        .Cast<TableCell>() 
        .Where((r,index) => index != chechBoxColumnIndex)); 

     foreach(var c in cells.Cast<TableCell>()){ 
      //note: you need to set EnableEventValidation to false in this page 
      c.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink((GridView)sender, "Select$" + e.Row.RowIndex); 
      c.ToolTip = "Click to select row"; 
      c.Attributes["onmouseover"] = "this.style.cursor='pointer';bgColor=this.parentNode.style.backgroundColor;this.parentNode.style.backgroundColor= '" + highlightColor + "';"; 
      c.Attributes["onmouseout"] = "this.parentNode.style.backgroundColor=bgColor;"; 
     } 
    } 
} 

編輯

你可以使用RowDataBound

+0

當前我在RowDataBound中有這樣的代碼:if(e.RowTypeRowType == DataControlRowType.DataRow) {e.Row.Attributes.Add(「onmouseover」, 「bgCol = this.style.backgroundColor; This.style .backgroundColor ='「+ HighlightColor + 」'; this.style.cursor ='hand'「); e.Row.Attributes.Add(「onmouseout」,「this.style.backgroundColor = bgCol;」); (「onClick」,「javascript:SelectRow('」+ e.Row.RowIndex +「')」); } – Warrior 2012-02-10 10:52:38

+0

那麼爲什麼你沒有在第一個地方顯示這個代碼呢?稍等片刻...... – 2012-02-10 11:22:12

+0

@戰士:更新了我的答案。 – 2012-02-10 11:50:12

相關問題