1

I am creating a web app using asp.net and c#. I am using gridview to display a list of staff from the database. I have added a checkbox so that the user can choose the staff they wish to pick. However when I go to check where the user clicked all the checkboxes show up unclicked. This is my code:

    <asp:GridView runat="server" id="gvPickStaff" GridLines="Horizontal" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="idusers" HeaderText="ID  " ReadOnly="true" />
                        <asp:BoundField DataField="first_name" HeaderText="Name " ReadOnly="true" />
                        <asp:BoundField DataField="job_title" HeaderText="Job Title     " ReadOnly="true" />
                        <asp:BoundField DataField="code_quality" HeaderText="Code Quality" ReadOnly="true" />
                        <asp:BoundField DataField="time_bonus" HeaderText="Time Bonus" ReadOnly="true" />
                        <asp:BoundField DataField="analysis_of_requirements" HeaderText="Analysis Of Requierements" ReadOnly="true" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="cbxSelect" runat="server" Checked="true" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

The Code Behind where I found the problem:

    protected void btnAddStaff_OnClick(object sender, EventArgs e)
    {

        foreach(GridViewRow rowitem in gvPickStaff.Rows)
        {
            chk = (CheckBox)(rowitem.Cells[0].FindControl("cbxSelect"));

            if(chk.Checked)
            {
                int staffid = Convert.ToInt32(gvPickStaff.DataKeys[rowitem.RowIndex]["idusers"]);
                staffChosen.Add(staffid);
            }

        }
    }

I fill the gridview like this:

    protected void fillStaffChoiceList()
    {
        string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
        MySqlConnection connection = new MySqlConnection(strConnection);
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader reader;
        command.CommandText = "SELECT idusers, first_name, job_title, code_quality, time_bonus,analysis_of_requirements FROM `test`.`users` WHERE security_level > 1";
        //SELECT idusers, first_name, last_name, job_title, code_quality, time_bonus,analysis_of_requirements FROM `test`.`users` WHERE security_level > 1;
        connection.Open();

        reader = command.ExecuteReader();

        gvPickStaff.DataSource = reader;
        gvPickStaff.DataBind();

        connection.Close();
    }

Can anyone perhaps see where I am going wrong?

1
  • Are all your checkboxes unchecked to start with, and at what point do you call fillStaffChoiceList()? If you're calling it in Page_Load and not using if (!IsPostBack) then it'll be rebinding and losing your checkbox states before you hit the button click handler. Commented May 4, 2011 at 15:40

4 Answers 4

2

I think problem is in page load

try this

public void Page_Load(object sender,EventArgs e)
{

if(!IsPostBack)
{
fillStaffChoiceList();

}

}
Sign up to request clarification or add additional context in comments.

2 Comments

That was the problem. Thanks a million
+1 - I'm not sure what all the other answers are on about! :)
0
(rowitem.Cells[6].FindControl("cbxSelect")`

Your looking for the checkbox in the wrong cell I guess. Don't you get an exeption on the checkbox? It shouldn't be available in cell 0.

1 Comment

No I didn't get an error at all. I tried changing it to what you suggested but still every check box is coming back false. Any other suggestions?
0

This is an example of what can be used.

((CheckBox)gvPickStaff.Rows[i].FindControl("cbxSelect")).Checked

I hope that this can help also this is making it explicitly into a Check-box thus being able to return as is supposed to.

2 Comments

Is this not the same thing as chk = (CheckBox)(rowitem.Cells[6].FindControl("cbxSelect")); I'm putting it as a checkbox there but the problem is that they are all coming back as not checked
Why are you using cell if you're already using the FindControl Method
0

Use this in the check box:

<input type="checkbox" name="chk1" id="chk1" value='<%# Eval("idusers") %>' />

And when you want ids which is selected, use:

request("chk1");

It will return all data which is checked with comma(,) separated

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.