开发者

ASP.Net C# Gridview CheckBox Questiom

开发者 https://www.devze.com 2023-03-02 15:28 出处:网络
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

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:BoundFi开发者_如何学Goeld 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?


I think problem is in page load

try this

public void Page_Load(object sender,EventArgs e)
{

if(!IsPostBack)
{
fillStaffChoiceList();

}

}


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号