【问题标题】:ASP.Net C# Gridview CheckBox QuestiomASP.Net C# Gridview CheckBox 问题
【发布时间】:2011-05-04 23:47:36
【问题描述】:

我正在使用 asp.net 和 c# 创建一个 Web 应用程序。我正在使用 gridview 来显示数据库中的员工列表。我添加了一个复选框,以便用户可以选择他们想要选择的员工。但是,当我去检查用户单击的位置时,所有复选框都未单击。 这是我的代码:

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

我发现问题所在的代码:

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

        }
    }

我这样填充gridview:

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

谁能看出我哪里出错了?

【问题讨论】:

  • 一开始你的所有复选框都没有选中吗?你什么时候打电话给fillStaffChoiceList()?如果您在 Page_Load 中调用它而不使用 if (!IsPostBack),那么在您点击按钮单击处理程序之前,它将重新绑定并丢失您的复选框状态。

标签: c# asp.net gridview


【解决方案1】:

我认为问题在于页面加载

试试这个

public void Page_Load(object sender,EventArgs e)
{

if(!IsPostBack)
{
fillStaffChoiceList();

}

}

【讨论】:

  • 这就是问题所在。谢谢一百万
  • +1 - 我不确定所有其他答案都是关于什么的! :)
【解决方案2】:
(rowitem.Cells[6].FindControl("cbxSelect")`

我猜你在错误的单元格中寻找复选框。你没有得到复选框的例外吗?它不应该在单元格 0 中可用。

【讨论】:

  • 不,我根本没有收到错误消息。我尝试将其更改为您的建议,但仍然每个复选框都返回错误。还有其他建议吗?
【解决方案3】:

这是可以使用的示例。

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

我希望这也能有所帮助,因为它明确地变成了一个复选框,从而能够按预期返回。

【讨论】:

  • 这和 chk = (CheckBox)(rowitem.Cells[6].FindControl("cbxSelect")); 不是一回事吗?我把它作为一个复选框放在那里,但问题是它们都回来了,因为没有选中
  • 如果您已经在使用 FindControl 方法,为什么还要使用单元格
【解决方案4】:

在复选框中使用它:

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

当你想要被选中的 ids 时,使用:

request("chk1");

它将返回以逗号(,)分隔的所有数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    相关资源
    最近更新 更多