2012-07-25 66 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();