2014-01-08 37 views
2

我目前有兩行代碼影響GridView中的一行點擊。如果我讓任何一個到位,他們自己工作。如果我把這兩個都放在GridView1_RowDataBound的作品中。單擊時使用GridView1_RowDataBound方法更改GridView中的選定行。 GridView1_RowCreated代碼用於進行點擊,使頁面上元素的ID變爲可見。我真的需要兩個。有沒有辦法將它們結合起來?在gridview上獲取兩個click事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex); 
} 

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
     e.Row.Attributes.Add("onclick", "setVisible('cfilterpopup')"); 
} 

回答

1

我會做這樣的:

編輯:OnClientClick不會與GridViewRow工作。你可以嘗試以下方法:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex); 
    e.Row.Attributes.Add("onclick", "setVisible('cfilterpopup');__doPostBack('" + GridView1.ClientID + "', 'Select$" + e.Row.RowIndex + "');"); 
} 

說明:在secod行,我們正在改變注入我們setVisible()方法屬性和調用__doPostBack()

編輯2:有兩個問題,現在:

  1. cfilterpopup會無形再次呼籲回發。

  2. Onclick屬性被消滅了上回發

如何修復#1:

添加標記中的隱藏字段。在setVisible()裏面設置隱藏字段值爲「1」。在正文onload上添加一個方法來掃描該值並使該部分可見/不可見。

您使用JavaScript標記可能是這樣的:

<head runat="server"> 
    <title></title> 
    <script type="text/javascript"> 
     function setVisible(itm) { 
      document.getElementById('<%=hdnStatus.ClientID %>').value="1"; 
     } 

     function showcfilterpopup() { 
      var status = document.getElementById('<%=hdnStatus.ClientID %>').value; 
      document.getElementById('cfilterpopup').style.visibility = status == "1" ? "visible" : "hidden";   
     } 
    </script> 
</head> 
<body onload="showcfilterpopup();"> 
    <form id="form1" runat="server"> 
    <div> 
     <div id="cfilterpopup" style="display:none"> 
      <p>cfilterpopup</p> 
     </div> 
     <asp:HiddenField ID="hdnStatus" Value="0" runat="server" /> 
     <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView> 
    </div> 
    </form> 
</body> 
</html> 
+0

我嘗試的第一個。當我第一次嘗試它時我的錯誤我的語法錯誤。現在點擊它時,它不會設置cfilterpopup Visible,但它會爲GridView設置selectedrow。代碼'e.Row.Attributes.Add(「OnClientClick」,「setVisible('cfilterpopup');」);'即使在我註釋掉另一個時也不起作用。 –

+0

我編輯了我的答案。 'OnClientClick'不適用於GridViewRow。 – afzalulh

+0

太近了。這兩者都執行,但不是我所期望的。 cfilterpopup在瞬間變得可見,然後再次不可見。 –