0

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

该值应基于此会话,然后用于确定使用哪种方法来填充网格视图。

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();
4

1 回答 1

0

我没有通过双击事件,而是在 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);
    }
}

哺乳详情页面

Session["selectedMilkid"] = Request.QueryString["b"].ToString();
    string selectedEventType = Request.QueryString["c"].ToString();
于 2012-07-27T16:09:58.427 回答