在ASP.NET GridView中添加复选框列

13

涉及在asp.net中将CheckBox列添加到gridview并获取多个值时,我有几个问题。首先,我看到每个人都在他们的aspx页面中添加OnCheckedChanged="chkview_CheckedChanged",但是当您单击CheckBox以设置其操作时,它不会打开OnCheckedChanged="chkview_CheckedChanged",而是打开SelectedIndexChanged事件。我的目标是当用户选择一个CheckBox时,它将相应的行信息添加到TextBox中。这是我当前使用的代码来设置这些值。如何改用选定的CheckBox?

protected void dropGridView_SelectedIndexChanged1(object sender, EventArgs e)
{
    GridViewRow row = dropdeadGridView.SelectedRow;
    IDTextBox.Text = row.Cells[1].Text;
    loadnumTextBox.Text = row.Cells[2].Text;
}

完成后,如何使其获取所有选中的行而不是只获取一个?这是我的当前问题。我正在寻找一种方法来选择多个行并添加选择按钮。我已经做了很多搜索,但没有找到相关内容,因此我尝试使用CheckBoxes实现。有什么想法可以让我添加并选择多行吗?谢谢。

以下是我的编辑*,发布包含CheckBox列的asp代码:

<asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="SelectCheckBox" runat="server" OnCheckedChanged="SelectCheckBox_OnCheckedChanged"/>
            </ItemTemplate>
        </asp:TemplateField>
2个回答

15

首先,您必须将autopostback属性设置为true:

<asp:CheckBox ID="SelectCheckBox" runat="server" AutoPostBack="true" 
              OnCheckedChanged="SelectCheckBox_OnCheckedChanged"/>

根据你的情况,SelectedIndexChanged 是由网格视图发送的。对于复选框事件,您必须使用 OnCheckedChanged 事件:

protected void SelectCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = sender as CheckBox ;

    if(chk.Checked)
    {
        GridViewRow row = (GridViewRow)chk.NamingContainer;
        IDTextBox.Text = row.Cells[1].Text;
        loadnumTextBox.Text = row.Cells[2].Text;
    }
}

如果您想遍历所有选中的复选框:
var rows = dropdeadGridView.Rows;
int count = dropdeadGridView.Rows.Count;
for (int i = 0; i < count; i++)
{
    bool isChecked = ((CheckBox)rows[i].FindControl("chkBox")).Checked;
    if(isChecked)
    {
        //Do what you want
    }
}

它没有填充文本框。 - Kpt.Khaos
我本来要问的 =) - Plue
你进入了SelectCheckBox_OnCheckedChanged方法吗? - Plue
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/49250/discussion-between-plue-and-kpt-khaos - Plue
2
在您的循环建议上...它可以用一行代码完成。使用foreach更容易。例如:foreach (GridViewRow gvr in GridView1.Rows),而不是var rows = dropdeadGridView.Rows; int count = dropdeadGridView.Rows.Count; for (int i = 0; i < count; i++) - M H
显示剩余3条评论

0

HTML Gridview 示例

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Class="table table-striped table-bordered" ShowHeaderWhenEmpty="true" HeaderStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" HorizontalAlign="Center" Height="40px" Width="80%" EmptyDataText="商店没有库存"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="chkAllSelect" runat="server" onclick="checkAll(this);" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkSelect" onclick="Check_Click(this);EnableBTN(this);" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="SHOP_CODE" HeaderText="商店代码" /> <asp:BoundField DataField="ITEM_CODE" HeaderText="商品代码" /> <asp:BoundField DataField="ITEM_NAME" HeaderText="商品名称" /> <asp:BoundField DataField="COLOR_CODE" HeaderText="颜色代码" /> <asp:BoundField DataField="COLOR_NAME" HeaderText="颜色名称" /> <asp:BoundField DataField="STOCK_NO" HeaderText="库存数量" /> <asp:BoundField DataField="STOCK_IN_HAND" HeaderText="实际库存" /> <asp:BoundField DataField="LOCATION_CD" HeaderText="位置" /> <asp:TemplateField HeaderText="数量"> <ItemTemplate> <asp:TextBox CssClass="form-control" onkeyup="this.value=this.value.replace(/[^0-9]/g,'');" Placeholder="输入正确的数量" ID="ADJqty" runat="server" AutoCompleteType="Disabled" Text=""></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

使用此函数来检查所有网格视图复选框。
        function checkAll(objRef) {
            var GridView = objRef.parentNode.parentNode.parentNode;
            var inputList = GridView.getElementsByTagName("input");
            for (var i = 0; i < inputList.length; i++) {
                //Get the Cell To find out ColumnIndex
                var row = inputList[i].parentNode.parentNode;
                if (inputList[i].type == "checkbox" && objRef != inputList[i]) {
                    if (objRef.checked) {
                        //If the header checkbox is checked
                        //check all checkboxes
                        //and highlight all rows
                        row.style.backgroundColor = "#e3f1ff";
                        inputList[i].checked = true;
                        $('#DummyBTNAUTHLeave').prop('disabled', false);
                        $('#DummyBTNRJTLeave').prop('disabled', false);
                    }
                    else {
                        //If the header checkbox is checked
                        //uncheck all checkboxes
                        //and change rowcolor back to original
                        if (row.rowIndex % 2 == 0) {
                            //Alternating Row Color
                            row.style.backgroundColor = "rgba(0,0,0,.05)";
                        }
                        else {
                            row.style.backgroundColor = "white";
                        }
                        inputList[i].checked = false;
                        $('#DummyBTNAUTHLeave').prop('disabled', true);
                        $('#DummyBTNRJTLeave').prop('disabled', true);
                    }
                }
            }
        }

在GridView中勾选指定的复选框

        function Check_Click(objRef) {
            //Get the Row based on checkbox
            var row = objRef.parentNode.parentNode;
            if (objRef.checked) {
                //If checked change color to Aqua
                row.style.backgroundColor = "#e3f1ff";
            }
            else {
                //If not checked change back to original color
                if (row.rowIndex % 2 == 0) {
                    //Alternating Row Color
                    row.style.backgroundColor = "rgba(0,0,0,.05)";
                }
                else {
                    row.style.backgroundColor = "white";
                }
            }
            //Get the reference of GridView
            var GridView = row.parentNode;
            //Get all input elements in Gridview
            var inputList = GridView.getElementsByTagName("input");
            for (var i = 0; i < inputList.length; i++) {
                //The First element is the Header Checkbox
                var headerCheckBox = inputList[0];
                //Based on all or none checkboxes
                //are checked check/uncheck Header Checkbox
                var checked = true;
                if (inputList[i].type == "checkbox" && inputList[i] != headerCheckBox) {
                    if (!inputList[i].checked) {
                        checked = false;
                        break;
                    }
                }
            }
            headerCheckBox.checked = checked;
        }

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接