简体   繁体   English

如何确定哪个列用户单击了ASP.NET GridView

[英]How to determine which column user clicked on ASP.NET GridView

I have a datagridview in asp.net using c#. 我在c.asp.net中有一个datagridview。 I am using this code to make the row selectable: 我正在使用此代码使行可选:

protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvTasks, "Select$" + e.Row.RowIndex);
        e.Row.ToolTip = "Click to select this row.";
    }
}

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
   foreach (GridViewRow row in gvTasks.Rows)
   {
       if (row.RowIndex == gvTasks.SelectedIndex)
       {
           DataTable mydt = new DataTable();
           mydt = (DataTable) Session["dt"];

           Response.Redirect("EditTask.aspx?taskID=" + mydt.Rows[gvTasks.SelectedIndex].ItemArray[0].ToString()
                    + "&agentID=" + mydt.Rows[gvTasks.SelectedIndex].ItemArray[1].ToString());
       }
       else
       {

       }
    }
}

My challenge is that I have added a new databound column which is a link I need the user to navigate to. 我的挑战是我添加了一个新的数据绑定列,这是我需要用户导航到的链接。 Since clicking on the row will automatically take you to a certain page (Edit Task in this case), it ends up taking you to the Edit Task page wether I click on the link or any where on the row. 由于单击该行会自动将您带到某个页面(在本例中为“编辑任务”),因此如果您单击链接或该行的任何位置,最终都会带您进入“编辑任务”页面。

I need to determine which column was clicked on so I can add extra logic that if its a specific column use one url or all others, use another url. 我需要确定单击了哪一列,以便添加额外的逻辑,如果特定列使用一个URL或所有其他URL,则使用另一个URL。 I have not found a selectedColumn property on the asp.net datagridview. 我没有在asp.net datagridview上找到selectedColumn属性。 What is the best way to determine which column was selected. 确定所选列的最佳方法是什么。

GridView doesn't have a built-in mechanism that allows to select a column, it won't be possible to only use GridView feature. GridView没有允许选择列的内置机制,因此无法仅使用GridView功能。

One way to do it is to set the column number in a hidden field when the user click on a field and then retrieve this hidden field value inside the OnSelectedIndexChanged event. 一种方法是在用户单击字段时在隐藏字段中设置列号,然后在OnSelectedIndexChanged事件中检索此隐藏字段的值。

Outside of the gridview, insert a new hiddenField 在gridview之外,插入一个新的hiddenField

<asp:hiddenField runat="server" id="hfColumnId" />

When you click on a row, set this value using JavaScript : 单击一行时,请使用JavaScript设置此值:

$('table#XXX td').click(function(){
    var columnId = $(this).parent().children().index($(this));
    $('#hfColumnId').val(columnId);
});

Then in your OnSelectedIndexChanged event, you can retrieve the columnId using 然后,在OnSelectedIndexChanged事件中,可以使用以下方法检索columnId

Int32 columnId = Int32.Parse(this.hfColumnId.Value); 

My first ever answer on Stack overflow: Maybe what you need to do is to use a slightly different approach. 我对堆栈溢出的第一个答案:也许您需要做的是使用稍微不同的方法。 Since your objective is to have the user go to a link they click from the datagrid you should instead create a column at runtime that will have that link. 由于您的目标是让用户转到他们从数据网格单击的链接,因此您应该在运行时创建一个具有该链接的列。 You can do it in 2 ways, Programmatically or asp. 您可以通过两种方式(以编程方式或asp)来实现。 Will answer Programmatically 将以编程方式回答

  1. Programmatically: This code starts at the time you bind your grid 以编程方式:该代码在绑定网格时开始

    GridView1.DataBind(); GridView1.DataBind(); //After you have bound your grid view do the following //绑定网格视图后,请执行以下操作

      for (int i = 0; i < GridView1.Rows.Count; i++) { HyperLink hlContro = new HyperLink(); hlContro.NavigateUrl = "./newPage.aspx?ID=" + GridView1.Rows[i].Cells[3].Text; //This is where your url is being kept in the grid hlContro.Text = "Click me to go to the link"; GridView1.Rows[i].Cells[3].Controls.Add(hlContro);//adds the control to the gridview } 

Many thanks to people who helped me on stack overflow and got me to where I can help you too today. 非常感谢那些在堆栈溢出时帮助我并使我今天也可以为您提供帮助的人。 hope I helped. 希望我能帮上忙。 - Samcima -Samcima

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM