0

I have a gridview and I want to pass a value from a row cell to another page. Based on the value which is a string, I can run a specific query and fill another gridview. My problem is, that when I double click the row, it won't pick up the value, however, it will for the other columns in the row when I change the row cell index. Let me know if more info is needed, thanks.

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>

The value should get based to this session and then used to determine which method to use to fill the 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();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Colin Roe
  • 774
  • 1
  • 18
  • 35

1 Answers1

0

Instead of going by a doubleclick event, I just added a buttomfield to the gridview and created an OnRowCommand event. I made EventID and EventType (two columns in the gridview that I need to get my values from) DataKeyNames. In the code behind, in the OnRowCommand event, I get the index of the row selected and get the values for EventID and EventType on that row. I passed the values using a QueryString in the url. On the LactationDetails page, I then request the query strings and use the values...

<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 page

Session["selectedMilkid"] = Request.QueryString["b"].ToString();
    string selectedEventType = Request.QueryString["c"].ToString();
Colin Roe
  • 774
  • 1
  • 18
  • 35