双击GridView行和获取Id值

时间:2014-02-25 19:30:59

标签: c# asp.net gridview

我有一个Gridview,我已经做到了这样,用户可以按照以下说明双击一行。 http://www.codeproject.com/Articles/15677/Clickable-and-Double-Clickable-Rows-with-GridView

这很容易。现在我需要能够使用查询字符串将用户重定向到另一个页面。 querstring将是我需要从双击的GridView行获取的id。我不确定如何从GridView行获取id。我已经尝试了很多不同的东西,它总是出现空的。这是我的代码。

<asp:GridView ID="gvAllDOL" runat="server" Visible="False" PageSize="25" AutoGenerateColumns="False" OnDataBound="gvAllDOL_DataBound" DataSourceID="odsDOAll" OnRowDataBound="gvAllDOL_RowDataBound" DataKeyNames="sintDistrictOfficeID" OnRowCommand="gvAllDOL_RowCommand" OnSelectedIndexChanged="gvAllDOL_SelectedIndexChanged">
                <Columns>
                    <asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" Visible="false" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="lblid" runat="server" Text='<%# Bind("sintDistrictOfficeID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="sintDistrictOfficeID" HeaderText="id" SortExpression="sintDistrictOfficeID" />     
                    <asp:BoundField DataField="vcharDOLOfficeName" HeaderText="DOL Office Name" SortExpression="vcharDOLOfficeName" />     
                    <asp:BoundField DataField="vcharDOLCity" HeaderText="City" SortExpression="vcharDOLCity" />
                    <asp:BoundField DataField="vcharDOLState" HeaderText="State" SortExpression="vcharDOLState" />
                    <asp:BoundField DataField="intBatchCount" HeaderText="Number Batches" SortExpression="intBatchCount" />
                    <asp:BoundField DataField="intCaseCount" HeaderText="Number Cases" SortExpression="intCaseCount" />
                    <asp:BoundField DataField="intExamCount" HeaderText="Number Examiners" SortExpression="intExamCount" />                        
                </Columns>
            </asp:GridView>

  protected void gvAllDOL_RowDataBound(object sender, GridViewRowEventArgs e)
    {            
            e.Row.Attributes.Add("onMouseOver", "Highlight(this);");
            e.Row.Attributes.Add("onMouseOut", "UnHighlight(this);");

            //doubleclick
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                  

                // Get the LinkButton control in the second cell
                LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
                // Get the javascript which is assigned to this LinkButton
                string _jsDouble =
                ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
                // Add this JavaScript to the ondblclick Attribute of the row
                e.Row.Attributes["ondblclick"] = _jsDouble;
            }

        }

 protected void gvAllDOL_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id;
        id = gvAllDOL.SelectedRow.Cells[1].Text;

        GridView _gridView = (GridView)sender;

        // Get the selected index and the command name
        int _selectedIndex = int.Parse(e.CommandArgument.ToString());
        string _commandName = e.CommandName;


        switch (_commandName)
        {                
            case ("DoubleClick"):
                Response.Redirect("~/DOL/View_dol.aspx?id=" + id);                  
                break;
        }

    }     

protected override void Render(HtmlTextWriter writer)
    {

        foreach (GridViewRow r in gvAllDOL.Rows)
        {

            if (r.RowType == DataControlRowType.DataRow)
            {
                Page.ClientScript.RegisterForEventValidation
                        (r.UniqueID + "$ctl00");
                Page.ClientScript.RegisterForEventValidation
                        (r.UniqueID + "$ctl01");
            }
        }

        base.Render(writer);
    }

1 个答案:

答案 0 :(得分:0)

在RowCommand方法中尝试:

int _selectedIndex = int.Parse(e.CommandArgument.ToString());
id = (string)this.grid.DataKeys[_selectedIndex]["myKey"];

OR

id = this.grid.DataKeys[_selectedIndex].Values[0]