2012-07-25 67 views
0

時使用Request.QueryString傳遞一個值我有一個gridview,我想將行單元格中的值傳遞給另一個頁面。根據字符串的值,我可以運行一個特定的查詢並填充另一個gridview。 我的問題是,當我雙擊該行時,它不會拾取該值,但是,當我更改行單元格索引時,它將用於行中的其他列。 讓我知道是否需要更多信息,謝謝。當我雙擊

protected void grdCowCard_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     string querystring = string.Empty; 
     string id = e.Row.Cells[1].Text; 

     //Session["selectedLactationEvent"] = "MMR"; 
     e.Row.Attributes["ondblclick"] = string.Format("doubleClick({0})", id); 
     //won't pick up the value("MMR") at Row.Cells[1] 
     //but will pick up the value("1") at Row.Cells[0] 
    } 
} 

<script type="text/javascript"> 
    function doubleClick(queryString) { 
     window.location = ('<%=ResolveUrl("LactationDetails.aspx?b=") %>' + queryString); 
    } 
</script> 

的價值應該得到基於該會話,然後用來確定使用哪種方法來填充GridView控件。

Session["selectedLactationEvent"] = Request.QueryString["b"].ToString(); 

//string test = (string)(Session["selectedLactationEvent"]); 
if ((string)(Session["selectedLactationEvent"]) == "MMR") 
    GetExtraMMRdetails(); 
else if ((string)(Session["selectedLactationEvent"]) == "LAC") 
    GetExtraLACdetails(); 
else 
    GetExtraEBIdetails(); 
+0

無論如何,用'開關((字符串)會議[ 「selectedLactationEvent」])的情況下「MMR」:{} ...' – abatishchev 2012-07-25 11:39:40

+0

ASP.Net頁面無法捕獲雙擊事件。你將不得不在JavaScript中通過調用你的代碼從客戶端 – Ahmad 2012-07-25 11:40:00

+0

我在JavaScript中這樣做 – 2012-07-25 16:18:45

回答

0

而不是通過doubleclick事件,我剛剛添加了一個buttomfield到gridview並創建了一個OnRowCommand事件。我做了EventID和EventType(gridview中的兩列,我需要從DataKeyNames中獲取值)。 在後面的代碼中,在OnRowCommand事件中,我得到所選行的索引並獲取該行上的EventID和EventType的值。我使用url中的QueryString傳遞了值。在LactationDetails頁面,然後我請求的查詢字符串,並使用值...

<asp:GridView ID="grdCowCard" runat="server" Width="100%" 
     DataKeyNames="EventID,EventType" HeaderStyle-BackColor="#008B00" 
     OnRowCreated="grdCowCard_RowCreated" Caption="Lactation Records" 
     OnRowCommand="grdCowCard_RowSelected"> 
     <Columns> 
      <asp:ButtonField Text="Select" CommandName="Select"/> 
     </Columns> 
    </asp:GridView> 

protected void grdCowCard_RowSelected(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Select") 
    { 
     int RowIndex = int.Parse(e.CommandArgument.ToString());// Current row 

     string id = grdCowCard.DataKeys[RowIndex]["EventID"].ToString(); 
     string eventType1 = grdCowCard.DataKeys[RowIndex]["EventType"].ToString(); 

     //grdCowCard.Attributes["ondblclick"] = string.Format("doubleClick({0})", id, eventType1); 

     //id and eventType are passed to LactationDetails page, and used to determine which 
     //method to use and what data to retrieve 
     Response.Redirect("LactationDetails.aspx?b=" + id + "&c=" + eventType1); 
    } 
} 

LactationDetails頁

Session["selectedMilkid"] = Request.QueryString["b"].ToString(); 
    string selectedEventType = Request.QueryString["c"].ToString();