2012-01-24 47 views
0

我目前在ASP.Net/C#中工作。我試圖通過數據網格中的行使服務器重定向到另一個頁面,但是,我的代碼看起來並不是 。任何幫助,將不勝感激。通過datagridView重定向

protected void GridView1_RowCreated() 
{ 
    foreach (GridViewRow row in GridView1.Rows) 
    { 
     Color color = row.BackColor; 
     string color2 = System.Drawing.ColorTranslator.ToHtml(color); 
     Session["" + row.Cells[0].Text + ""] = row.Cells[0].Text; 
     //row.Attributes.Add("onclick", "zz(); return false;"); 
     /*if (row.RowState == DataControlRowState.Alternate) 
     { 
      row.Attributes.Add("onclick", "redirectFunction(); return false;"); 
      //row.Attributes.Add("onmouseover", "this.style.backgroundColor=' #FFFFFF';"); 
      //row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + color2 + "';"); 
      //row.Attributes.Add("onclick", cookie.Value = row. GridView1.SelectedRow.Cells[0].Text); 
      //row.Attributes.Add("onclick", "zz(); return false;"); 
     } 
     else 
     { 
      //row.Attributes.Add("onmouseover", "this.style.backgroundColor=' #FFFFFF';"); 
      //row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + color2 + "';"); 
      //row.Attributes.Add("onclick", "zz(); return false;"); 
     }*/ 
    } 
} 

     public void redirectFunction() 
     { 
    Response.RedirectPermanent("View.aspx"); 
     } 

     Other page 
     if(Session["session"].ToString() != null) 
     { 
      // do something 
     } 

回答

0

它看起來像你在點擊事件添加客戶端,然後試圖調用ac#方法,這將無法正常工作。

此代碼:

foreach (GridViewRow row in GridView1.Rows) 
{ 
    row.Attributes.Add("onclick", "redirectFunction()"); 
    // Or if you want to pas in an id 
    row.Attributes.Add("onclick", "redirectFunction(" + rowId + ")"); 
} 

是增加一個JavaScript單擊事件的行,你告訴它來調用JavaScript方法。然後,您需要將JavaScript方法添加到您的aspx頁面:

<script type="text/javascript"> 
    function redirectFunction() { 
     alert("do you see this??"); 
     window.location = "View.aspx"; 
    } 
</script> 

我增加了一個警告語句,你可以擺脫它,但你看到這個彈出?如果沒有檢查頁面上的任何JavaScript錯誤。

你也可以使用jQuery做到這一點(它更容易):

$('.yourrowclassname).click(function() { 
    window.location = "View.aspx"; 
}); 

這意味着你不需要添加屬性的代碼,只需使用/添加一個類的行。

+0

只是從gridview的selectedIndexChanged調用javascript函數什麼也不做。 – MasterP

+0

我已經更新了上面的代碼。如果你仍然陷入困境,你能否提供更多信息。 – Renae

+0

另外,SelectedIndexChanged將在選中該行時觸發,而不是單擊。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onselectedindexchanged.aspx – Renae

0

你應該實現此功能通過使用GridView的SelectedIndexChanged事件。從那裏,您可以獲取您需要在View.aspx頁面中顯示的對象的所有屬性和信息。

+0

我試過了,但簡單的代碼不起作用。 foreach(GridView1中的GridViewRow行) { \t row.Attributes.Add(「onclick」,「redirectFunction(); return false;」); } 公共無效redirectFunction() { \t \t \t會話[ 「會話」] = Gridvidew1.SelectedIndex.Cell [0]。文本; Response.RedirectPermanent(「View.aspx」); – MasterP

0

入住這裏這個帖子,這將幫助你實現你試圖做 https://stackoverflow.com/a/331662/125551

只需添加會話[「變種」] = VAL或URL發送價值,並獲得其他在Page_Load中該值從查詢字符串而不是會話的頁面