2011-11-24 50 views
1

我想網格視圖單擊某一行時重定向,所以我有OnRowCreated網格視圖,我不能夠重定向到我想如何從網格視圖重定向與DataKey

頁面
<asp:GridView ID="Grid_Messagetable" runat="server" OnRowCreated="Grid_Messagetable_RowCreated" AllowPaging="False" SelectedIndex="0" 
       DataKeyNames="MsgID" ShowHeaderWhenEmpty="true" 
       OnRowDataBound="MyGrid_RowDataBound" AutoGenerateColumns="False" AllowSorting="true" 
       OnSorting="gridView_Sorting" Height="16px" Width="647px">  protected void Grid_Messagetable_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     e.Row.Attributes.Add("onClick", "this.style.background='#eeff00'"); 
    } 

這裏我試圖設置背景顏色,當一個行被點擊和它的工作,但我如何重定向頁面,我必須重定向到ResponseMetrci.aspx頁面的msgID,正如我在下面做的。因此,我在URL中傳遞了這個msgid,以便在響應度量標準頁面中檢索。

Eval("MsgID", "ResponseMetric.aspx?MsgID={0}") %>' 

我想這

e.Row.Attributes["onClick"] = "location.href= 
'ResponseMetric.aspx?MsgID=" + DataBinder.Eval(e.Row.DataItem, "MsgID") + "'"; 

但我得到下面的錯誤

Uncaught ReferenceError: redirect is not defined 
(anonymous function)Messages.aspx:774 
onclick 
+0

你可以發佈gridview的標記嗎? – Pleun

+0

ru在gridview中點擊一行或任何特定的控件時,重定向? – Karthik

+0

這可能會幫助你http://stackoverflow.com/questions/331231/c-sharp-gridview-row-click – Karthik

回答

0

您可以在新窗口打開和關閉現有的一個,如果你需要這樣。試試這個e.Row.Attributes.Add(「onClick」,「javascript:window.open('ResponseMetric.aspx?MsgID =」+ your value +「')」)。

2

簡單的修復,只是改變你的RowDataBound方法,我可以看到你已經實施,包括下面的代碼片段:

protected void MyGrid_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes["onClick"] = string.Format(
      "window.location = 'ResponseMetric.aspx?MsgID={0}';", 
      DataBinder.Eval(e.Row.DataItem, "MsgID")); 
    } 
} 

下面是一個基本工作示例,只是去谷歌:

protected void grdTest_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Attributes["onClick"] = 
      "window.location = 'http://www.google.com/';"; 
    } 
} 
+0

這應該是接受的答案,偉大解 – prospector