【问题标题】:How to get selected value of cell on mouse click in asp.net c#如何在asp.net c#中单击鼠标时获取单元格的选定值
【发布时间】:2014-02-22 20:06:16
【问题描述】:

Asp.net C#。我有一个gridview 控件。

col1 - 一个 -乙 - C

我想要什么,当我点击 A 时,我应该重定向到第二页 我将执行查询并提供'A' 作为查询中的参数并检索值 来自基于'A'的数据库
例如:

string query="select * from table1 where name='A'";

Q1.在这种情况下,除了gridview,我还有其他方法或控制吗?

Q2.如果我使用Gridview,那么我如何获得价值,将其保存在某个字段中,然后在下一页提供并在查询中使用它。

我找到了诸如 templatefields,linkbutton,hyperlink 之类的东西,但它们似乎无法解决我的问题。

【问题讨论】:

  • 渲染一个类似secondpage.aspx?id=4的链接并让用户点击它。
  • 请告诉我如何做到这一点,因为在 asp.net 的 gridview 中没有行或单元格单击事件
  • if(e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onClick"] = "location.href='secondpage.aspx?id=" + DataBinder.Eval (e.Row.DataItem, "id") + "'";如果我使用它,那么用户将被重定向到其他页面,但我如何在我的查询中提供“A”
  • 我假设 Aristos 的意思是您可以在链接中提供所需的参数。根据您的回答评论,第一个单元格 e.Row.Cells[0].Text 具有您要传递的值。出于好奇,为什么要使用 javascript 对单元格使用 onclick,而不是在单元格内使用纯链接?

标签: c# asp.net gridview controls


【解决方案1】:

我发现从 cmets 转移这个答案很有用,所以这对其他人有帮助。

gridview_rowDataBound事件上写这段代码

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
                e.Row.Attributes.Add("style", "cursor:pointer;");
                e.Row.Attributes.Add("onclick", "location='WebForm3.aspx?id=" + e.Row.Cells[0].Text + "'");
            }

然后在下一页获取id的值

Id = Request.QueryString["ID"].ToString();

【讨论】:

    【解决方案2】:

    非常简单的方法是使 A 成为导航到第二页并传递查询字符串的 HyperLinkField。您可以通过附加到表格单元格单击事件来使用 javascript 执行您所说的操作,然后在单击时通过 cell.innerHTML 获取单元格的内容并将其传递到第二页,并通过查询字符串传递值。非常简单的方法是使用链接。添加到您的网格中:

    <asp:HyperLinkColumn DataNavigateUrlFormatString="SecondPage?value={0}"
       DataNavigateUrlFields="ColumnNameOfWhateverAIs" 
       DataTextField="ColumnNameOfWhateverAIs" />
    

    【讨论】:

    • 感谢所有贡献者,我找到了答案,但由于我无权发布我的分析器,所以这就是为什么我在 cmets 中发布我的答案,以便其他人可以从中受益。
    • 感谢大家,我终于得到了关于 gridview_rowDataBound 事件的答案 如果 (e.Row.RowType == DataControlRowType.DataRow) {e.Row.Attributes.Add("onmouseover", " this.style.backgroundColor='#ceedfc'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''"); e.Row.Attributes.Add("style", "cursor:pointer;"); e.Row.Attributes.Add("onclick", "location='WebForm3.aspx?id=" + e.Row.Cells[0].Text + "'");然后在下一页通过 Id = Request.QueryString["ID"].ToString(); 得到 id 的值;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多