简体   繁体   English

如何将ID和字段值分配给隐藏字段

[英]How to assign Id and field value to Hidden field

I have a gridview with few rows and columns , with ListBox Controls in columns. 我有一个只有几rows和几rowsgridviewcolumns中有ListBox控件。

Markup : 标记

    <asp:GridView ID="gvDataEntry" runat="server" AutoGenerateColumns="False"
    <Columns>
<ItemTemplate>
     <asp:ListBox ID="lstBox1" ondblclick="ListBox1_DoubleClick()" runat="server" CssClass="listBox"></asp:ListBox>
</ItemTemplate>
<ItemTemplate>
     <asp:ListBox ID="lstBox2" ondblclick="ListBox1_DoubleClick()" runat="server" CssClass="listBox"></asp:ListBox>
</ItemTemplate>
<ItemTemplate>
     <asp:ListBox ID="lstBox3" ondblclick="ListBox1_DoubleClick()" runat="server" CssClass="listBox"></asp:ListBox>
</ItemTemplate>
    </Columns>
    </asp:GridView>

Displayed as 显示为

-----------------------------------------------------
Name         |  Column1  |  Column2   | Column3
-----------------------------------------------------
FieldName    |  LB1      |   LB2      |   LB3      
----------------------------------------------------
FieldName 1  |  LB1      |   LB2      |   LB3
-----------------------------------------------------     

When I double click the Listbox I want to do some functionalities, so I have added the hidden field outside the gridview as 当我双击Listbox我想做一些功能,所以我在gridview外部添加了隐藏字段,如下所示:

<asp:HiddenField ID="ListBox1Hidden"
            runat="server" />

Javascript 使用Javascript

<script lang="javascript">
        function ListBox1_DoubleClick() {
            document.forms[0].submit();
        }
    </script>

On Page Load 页面加载

       if (Request.Params["ListBox1Hidden"] != null)
        {          
            //This means It was double click            
        }

Now when I double click the ListBox this event fires. 现在,当我双击ListBox将触发此事件。 Now I want o get the Name , RowIndex and Id of the ListBox Clicked 现在我想获取RowIndex的ListBox的NameRowIndexId

For Ex: 例如:

If I double clicked the LB1 of firs row I want to get Name : FieldName , Id: lstBox1 and RowIndex. 如果我双击第一行的LB1 ,我想获得Name: FieldName ,ID: lstBox1和RowIndex。

My doubt is, Can it can be achieved by the only one hidden field as I did. 我的疑问是,能否通过像我一样的唯一隐藏领域来实现? If so how to do it? 如果可以,该怎么办?

Else, should I have to include every HiddenField in the ItemTemplate to achieve this. 否则,我是否必须在ItemTemplate包括每个HiddenField才能实现此目的。

Which one helps me to achieve this, and provide me code to do this? 哪一个可以帮助我实现这一目标,并为我提供执行此目标的代码?

Try to pass the columns value in your javascript function. 尝试在javascript函数中传递column值。 Like this: 像这样:

'<%# Eval("ColumnName") %>'

For exmaple we have this double click event: 例如,我们有以下双击事件:

ondblclick="ListBox1_DoubleClick()"

Try to change this to: 尝试将其更改为:

ondblclick="ListBox1_DoubleClick('<%# Eval(\"ColumnName\") %>')"

So in this way you can pass any column's value as a parameter to javascript function. 因此,您可以通过这种方式将任何列的值作为参数传递给javascript函数。

Note: Within the double quotes "ColumnName" we have to use the proper escape character. 注意:在双引号“ ColumnName”中,我们必须使用适当的转义符。

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

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