2012-08-30 24 views
2

我有一個Gridview和rowDatabound我創建點擊該行和 顯示模式彈出。我想要的是當點擊行時,該行的ID應該被傳遞。如何從客戶端腳本訪問參數

protected void grdOrderProduct_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'"); 
     e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''"); 
     e.Row.Attributes.Add("style", "cursor:pointer;"); 
     e.Row.Attributes["onClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnPop, "12"); 
    } 
} 

如何在服務器控件上獲取此「12」。我已經把12作爲靜態演示。但它會改變。

回答

0

要知道被點擊哪一行,你必須使用的GridViewRowEventArgs

protected void grdOrderProduct_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'"); 
      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''"); 
      e.Row.Attributes.Add("style", "cursor:pointer;"); 
      e.Row.Attributes["onClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnPop, e.Row.RowIndex.ToString()); 

     } 
    } 

的rowIndex將作爲參數傳遞給btnPop傳遞的財產。爲了接收它btnPop應該實施IPostBackEventHandler

像這樣:

public class MyControl : Button, IPostBackEventHandler 
    { 

    // Use the constructor to defined default label text. 
    public MyControl() 
    { 
    } 

    // Implement the RaisePostBackEvent method from the 
    // IPostBackEventHandler interface. 
    public void RaisePostBackEvent(string eventArgument) 
    { 
     //You receive the row number here. 
    } 
    } 

參考ClientScriptManager.GetPostBackClientHyperlink

+0

如何從功能訪問這個值? – Moiz

+0

我明白了你的觀點。但我想知道我是否需要創建此類?並在哪裏創建? – Moiz

+0

如果你想接收自定義事件,那麼你需要創建這個類。您可以在解決方案中的任何位置的標準cs文件中創建此文件。像這些控件被稱爲自定義控件或服務器控件。 – nunespascal

2

你也可以做這樣的

e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink 
(this.GridView1, "Select$" + e.Row.RowIndex); 
+1

有關此代碼的描述,以及它爲何如此工作的說明可能會有所幫助。 – Boeckm