Get value from hidden boundfield? ASP.NET

心不动则不痛 提交于 2019-12-06 21:49:27

问题


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.

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.

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

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

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.

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. 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>

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();
}

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.


回答1:


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

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

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"];



回答2:


Another option is to keep the BoundField (or any other type of column) visible in the markup (remove Visible=False), and use the RowDataBound event to hide the columns:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[COLUMN_USERID].Visible = false;
}



回答3:


you can use this code. i tested it.

TextBox1.Text = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();



来源:https://stackoverflow.com/questions/10028009/get-value-from-hidden-boundfield-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!