2012-08-30 3 views
2

J'ai un Gridview et sur rowDatabound je crée en cliquant sur cette ligne et montrant le popup modal. ce que je veux c'est quand on clique sur la ligne, l'ID de cette ligne devrait être passé.Comment accéder au paramètre du script client

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

Comment puis-je obtenir ce "12" sur mon contrôle serveur. J'ai mis 12 comme statique pour la démo. mais ça va changer.

Répondre

0

Pour savoir quelle ligne a été cliqué, vous devez utiliser la propriété du 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 serait passé comme argument à btnPop. Afin de le recevoir btnPop devrait mettre en œuvre IPostBackEventHandler.

Comme ceci:

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

Référez ClientScriptManager.GetPostBackClientHyperlink

+0

Comment accéder à cette valeur de la fonction? – Moiz

+0

J'ai compris votre point. mais je veux savoir ai-je besoin de créer cette classe? et où créer? – Moiz

+0

Si vous souhaitez recevoir des événements personnalisés, vous devez créer cette classe. Vous pouvez créer ceci dans un fichier cs standard n'importe où dans votre solution. Les contrôles de ce type sont appelés contrôles personnalisés ou contrôles de serveur. – nunespascal

2

vous pouvez aussi faire comme ce

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

Une description pour aller avec ce code, pour savoir pourquoi cela fonctionne comme il le fait, pourrait être bénéfique. – Boeckm

Questions connexes