如何将onclick gridview单元格的值插入到hiddenfield中 [英] How to insert value of onclick gridview cell into hiddenfield

查看:73
本文介绍了如何将onclick gridview单元格的值插入到hiddenfield中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Onclick Gridview单元格的值插入到Hiddenfield中



这里我想要检索GridView ColumnIndex [1]单元格值而不管行。排可以是任意一行



我尝试过:



< pre lang =Javascript> function fnGethdnVal(hdnCC){
document .getElementById(< span class =code-string> hdnCC)。value = hdnCC;
}



 <   input     id   =   hdnCC    type  < span class =code-keyword> =  hidden    runat   =  server  /  >  



  protected   void  TranGrid_RowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
eR ow.Attributes.Add( onclick return fnGethdnVal( + e.Row.Cells [ 1 ]。文字+ );

}
}

解决方案

试试这个

 e.Row.Attributes.Add(  onclick  fnGethdnVal(' + e.Row.Cells [ 1 ]。文字+  ')); 



  function  fnGethdnVal(hdnCC){
alert(hdnCC);
document .getElementById(' <% #hdnCC.ClientID%>')。value = hdnCC;
}







示例:

 <   html    < span class =code-attribute> xmlns   =  http://www.w3.org/ 1999 / xhtml >  
< head runat = 服务器 >
< title > < / title >
< ; 脚本 >
function fnGethdnVal(hdnCC){
alert(hdnCC);
document .getElementById(' <% = hdnCC.ClientID%>')。value = hdnCC;
}
< / 脚本 >
< / head >
< body >
< 表单 id = form1 runat < span class =code-keyword> = server >

< asp:GridView runat = 服务器 ID = TranGrid OnRowDataBound = TranGrid_RowDataBound AutoGenerateColumns = true >
< / asp:GridView >
< asp:HiddenField ID = hdnCC < span class =code-attribute> runat = server / > ;

< / form >
< / body >
< / html < span class =code-keyword>>







  public   partial   class  WebForm2 :System.Web.UI.Page 
{
受保护 void Page_Load( object sender,EventArgs e)
{
if (Page.IsPostBack) return ;
DataTable dt = new DataTable();
dt.Columns.Add( 品牌);
dt.Columns.Add( Model);
dt.Columns.Add( ID);
dt.Rows.Add( Moto Nexus 1 );
dt.Rows.Add( Apple Iphone 2 );
dt.Rows.Add( Sony Vaio 3 );
dt.Rows.Add( Samsung Edge 4 );
TranGrid.DataSource = dt;
TranGrid.DataBind();


}

受保护 void TranGrid_RowDataBound( object sender,GridViewRowEventArgs e)
{
if (e。 Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add( onclick fnGethdnVal(' + e.Row.Cells [ 1 ]。文本+ ')) ;

}
}


}


这是给你的一个例子。我添加了行功能的突出显示,以便您可以轻松区分您所在的行。它还将保留回发中的选择:



ASPX:



 <   head     runat   =  server >  
< title > < / title >
< script src = http: //ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js\" type = text / javascript > < / 脚本 >

< script type = text / javascript >
var prevRowIndex;
function HighlighRowAndGetValue(row,rowIndex,cellValue){
var parent = < span class =code-sdkkeyword> document .getElementById(row);
var currentRowIndex = parseInt (rowIndex)+ 1 ;

if (prevRowIndex == currentRowIndex)
return ;
else if (prevRowIndex!= null
parent.rows [prevRowIndex] .style.backgroundColor = #FFFFFF;

parent.rows [currentRowIndex] .style.backgroundColor = #FFFFD6;
prevRowIndex = currentRowIndex;


' #<%= Label1.ClientID%>')。text(cellValue);


How to insert Value of Onclick Gridview Cell into Hiddenfield

Here I want to retrieve the GridView ColumnIndex[1] Cell Value irrespective of row. Row can be any row

What I have tried:

function fnGethdnVal(hdnCC) {
               document.getElementById("hdnCC").value = hdnCC;                
           }


<input id="hdnCC" type="hidden" runat="server"/> 


protected void TranGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    e.Row.Attributes.Add("onclick", "return fnGethdnVal("  +e.Row.Cells[1].Text+ ")");           

  }
}

解决方案

try this

e.Row.Attributes.Add("onclick", "fnGethdnVal('" + e.Row.Cells[1].Text + "')");


function fnGethdnVal(hdnCC) {
             alert(hdnCC);
               document.getElementById('<%# hdnCC.ClientID%>').value = hdnCC;                
           }




Example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function fnGethdnVal(hdnCC) {
             alert(hdnCC);
               document.getElementById('<%= hdnCC.ClientID%>').value = hdnCC;                
           }
    </script>
</head>
<body>
    <form id="form1" runat="server">
  
        <asp:GridView runat="server" ID="TranGrid" OnRowDataBound="TranGrid_RowDataBound" AutoGenerateColumns="true" >
        </asp:GridView> 
        <asp:HiddenField ID="hdnCC" runat="server" />
        
    </form>
</body>
</html>




public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;
            DataTable dt = new DataTable();
            dt.Columns.Add("Brand");
            dt.Columns.Add("Model");
            dt.Columns.Add("ID");
            dt.Rows.Add("Moto", "Nexus",1);
            dt.Rows.Add("Apple", "Iphone",2);
            dt.Rows.Add("Sony", "Vaio",3);
            dt.Rows.Add("Samsung", "Edge",4);
            TranGrid.DataSource = dt;
            TranGrid.DataBind();
           

        }

        protected void TranGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "fnGethdnVal('" + e.Row.Cells[1].Text + "')");

            }
        }

        
    }


Here's an example for you. I've added the highlighting of row feature so you can easily distinguished which row you are on. It will also retain the selection across postbacks:

ASPX:

<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    var prevRowIndex;
    function HighlighRowAndGetValue(row, rowIndex, cellValue) {
        var parent = document.getElementById(row);
        var currentRowIndex = parseInt(rowIndex) + 1;

        if (prevRowIndex == currentRowIndex) 
            return;
        else if (prevRowIndex != null) 
            parent.rows[prevRowIndex].style.backgroundColor = "#FFFFFF";
        
        parent.rows[currentRowIndex].style.backgroundColor = "#FFFFD6";
        prevRowIndex = currentRowIndex;


('#<%= Label1.ClientID %>').text(cellValue);


这篇关于如何将onclick gridview单元格的值插入到hiddenfield中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆