views:

2997

answers:

3

This seems like something simple, but I can't seem to figure it out! I'm trying to get 2-way data-binding to work on an ASP.net page with a check box as one of the columns. How do I get the updated values (from check boxes) back from the gridview ?????

Here is my data type:

[Serializable]
public class UserRequirements 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserId { get; set; }
    public string Email { get; set; }

    public bool ThingRequired { get; set; }
}

My markup looks something like this:

<form id="form1" method="post" runat="server" >
    <asp:GridView ID="UserTable" runat="server" AutoGenerateColumns="false" >
    <Columns>
        ...
        <asp:TemplateField HeaderText="Required ?">
            <ItemTemplate>
                <asp:CheckBox id="chkBox1" runat="server"  on
                Text ="Required"
                checked='<%# DataBinder.Eval(Container.DataItem,"ThingRequired")  %>'>
                </asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <asp:Button id="thebutton" Text="Save Changes" OnClick="UpdateRequirements" runat="server"  CausesValidation=false  />  
</form>

My code behind looks something like this:

    List<UserRequirements > _userList = new List<UserRequirements >();

    protected void Page_Load(object sender, EventArgs e)
    {
        _userList = data_layer.GetUserRequirments();


        this.UserTable.DataSource = _userList;
        this.UserTable.DataBind();      
    }

Eventually, I will call something like this, but I don't know where this should go or how to get the values back from the gridview:

void UpdateRequirements(object sender, EventArgs e)
{
    _userList = ???????????? // How do I get the data?
    data_layer.UpdateUserRequirements( _userList );
}
+2  A: 

hi, check this sample:

    foreach (GridViewRow di in GridView1.Rows)
    {
        HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("chkBox1");

        if (chkBx != null && chkBx.Checked)
        {
             /// put your code here
        }
    }
MysticSlayer
+1  A: 

try something like this to get the value on change:

protected void OnCheckedChanged(object sender, EventArgs e)
{    
   CheckBox c = (CheckBox)sender as CheckBox;    
   string checkBoxId = c.ID;    
   bool checkBoxValue = c.Checked;
   //update database
}

[EDIT]

If you want to get all the values from the rows in the grid in one go, you will need to bind the checkboxes using the Id for the row or item in your list of UserRequirements, so in your grid do something like this:

<asp:CheckBox ID="<%# Eval('Id') %>" />

then on postback, iterate through the items in the UserRequirements list matching the object/item Id with the Ids of the checkboxes in the grid .. something like this:

        foreach (UserRequirement item in Requirements)
        {
            Control c = grid.FindControl(item.Id);
            CheckBox cbx = c as CheckBox;
            if (cbx != null)
            {
                bool value = cbx.Checked;
                //update db
            }
        }

Note: you may need to use FindControl recursively to search child controls, or do a foreach on each GridViewRow object in the grid to pickup the checkbox you are looking for.

flesh
I think this way works best. Also remember to set the AutoPostBack property to "True" for the checkbox if you want the value to update immediately upon checking.
Dillie-O
A: 

@manwood, can you give me some more details on the "then on postback" part of your answer?

Keith G