【问题标题】:Double Clicking GridView Row and Getting Id Value双击 GridView 行并获取 Id 值
【发布时间】:2014-02-25 19:30:58
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    在你的 RowCommand 方法中试试这个:

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

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

    【讨论】:

    • 我可以再问一个问题吗?你将如何使用字符串?就像我需要用户双击并从 GridView 获取电子邮件地址。
    猜你喜欢
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2011-09-11
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多