Wesley Lalieu
Wesley Lalieu

Reputation: 487

Get value from hidden 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.

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.

Upvotes: 11

Views: 16673

Answers (4)

buckshot
buckshot

Reputation: 315

This is a very old post, but for anyone who's still interested, there's a much simpler way. Replace this:

Visible = false

with this:

ItemStyle-CssClass="c_hidden"

Then create a class with 'display:none'. The effect is the same, but your application can see the value.

Upvotes: 0

drizin
drizin

Reputation: 1965

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

Upvotes: 2

مسافریان
مسافریان

Reputation: 11

you can use this code. i tested it.

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

Upvotes: 1

James Johnson
James Johnson

Reputation: 46047

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

Upvotes: 17

Related Questions