双击时,使用Request.QueryString传递值

时间:2012-07-25 11:37:14

标签: c# asp.net gridview request.querystring rowdatabound

我有一个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();

1 个答案:

答案 0 :(得分:0)

我没有通过双击事件,只是在gridview中添加了一个buttomfield并创建了一个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();