得到隐藏的BoundField价值? ASP.NET [英] Get value from hidden boundfield? ASP.NET

查看:335
本文介绍了得到隐藏的BoundField价值? ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题我要问的是已经问其他人,但这些问题的答案是我的问题没有解决。

I know the question I'm going to ask is already asked for by other people, but those answers are no solution for my problem.

我有一个gridview含2绑定列,2 ButtonFields和复选框字段(这是一个TemplateField)。

I have a gridview containing 2 BoundFields, 2 ButtonFields and a checkbox field (which is a TemplateField).

我也有一个数据表,装满了来自数据库的数据。

I also have a datatable, filled with data from the database.

在ASPX代码创建我的领域gridview的最后设置的绑定列可见=假

In the aspx code I create my gridview with the fields set the last BoundField Visible = false.

在我隐藏,我添加了colums并绑定数据源到我的数据表。

In my codebehind, I add the colums and bind the datasource to my datatable.

但是,当我尝试读取从隐藏绑定列中的数据,该字段为空。为什么我不能使用类似的问题中提到的解决方案,这个问题是因为人们认为该数据被一个充满于一体,而不是通过结合一个DataTable到GridView的数据源。

But when I try to read the data from the hidden boundfield, that field is empty. The problem why I can't use the solutions mentioned with similar questions, is because the people assume the data gets filled in one by one, and not by binding a datatable to the datasource of the gridview.

所以我的问题是:是他们的方式来从隐藏的BoundField获取数据和还保留在数据表中绑定到GridView的数据源添加数据的可能性?
如果是这样,它是更多钞票从该领域?

So my question is: Is their a way to get the data from the hidden boundfield and also retain the possibility to add the data by binding the datatable to the datasource of the gridview? And if so, is it posible to get the value from that field?

P.S获得的价值。我使用asp.net/c#在Visual Studio 2010

p.s. I'm using asp.net/c# in visual studio 2010

ASPX:

<asp:GridView ID="gvSelect" runat="server" AutoGenerateColumns="False" BorderStyle="None" onrowcommand="gvTestSelect_RowCommand">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox runat="server" ID="cbHeader" OnPreRender="cbHeader_PreRender" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="cbItems" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="field" HeaderText="Veld" SortExpression="field" />
        <asp:ButtonField DataTextField="up" HeaderText="Omhoog" SortExpression="up" CommandName="up" Text="&uarr;" />
        <asp:ButtonField DataTextField="down" HeaderText="Omlaag" SortExpression="down" CommandName="down" Text="&darr;" />
        <asp:BoundField DataField="hidden" SortExpression="hidden" />
    </Columns>
</asp:GridView>

代码背后(这里我填的是GridView控件):

Code Behind (where I fill the gridview):

//create array list and fill it with all columns
Dictionary<string, string> dict = FillLists.getColumnsByTable(loader, ddlInfoTableI.SelectedItem.Value.ToString());

//loop trough dictionary
foreach (var val in dict)
{
    //create new dtSelect datarow
    DataRow dr = dtSelect.NewRow();

    //set row values for column values
    dr["select"] = false;
    dr["field"] = val.Value.ToString();
    dr["up"] = new ButtonField { CommandName = "up", Text = loader.LoadResourceString(1024), HeaderText = "&uarr;", ButtonType = ButtonType.Button, Visible = true };
    dr["down"] = new ButtonField { CommandName = "down", Text = loader.LoadResourceString(1025), HeaderText = "&darr;", ButtonType = ButtonType.Button, Visible = true };
    dr["hidden"] = val.Key.ToString();

    //add the datarow
    dtSelect.Rows.Add(dr);

    //set datatable session to datatable
    Session["dtSelect"] = dtSelect;

    //set datasource of the gridview to datatable
    gvSelect.DataSource = dtSelect;

    //bind data to gridview
    gvSelect.DataBind();
}



所以,现在我需要从隐藏在GridView获取数据(escpecialy绑定列),因为他们可以编辑修改除了隐藏绑定列在GridView,这样是要知道这是哪个原始行的唯一方式。

So now I need to get the data from the gridview (escpecialy from the hidden boundfield), because they can edit the gridview except the hidden boundfield, so that is the only way to know which original row it was.

推荐答案

添加一个数据键,你需要得到列:

Add a data key for the column you need to get:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="hidden" ...>



一旦你添加了datakey,您可以用行索引访问它的值:

Once you've added the datakey, you can access its value with the row index:

var rowIndex = 0;
var hiddenValue = (string)GridView1.DataKeys[rowIndex]["hidden"];

这篇关于得到隐藏的BoundField价值? ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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