返回GridView的复选框布尔 [英] Return Gridview Checkbox boolean

查看:143
本文介绍了返回GridView的复选框布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绞尽在试图访问一个GridView的ID列,其中用户选择一个复选框,我的脑子:

I've racked my brains on trying to access the ID column of a gridview where a user selects a checkbox:

<asp:GridView ID="gvUserFiles" runat="server">
            <Columns>
            <asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center" >
                <ItemTemplate>
                    <asp:CheckBox  ID="chkSelect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:GridView>

在GridView列chkSelect(复选框),ID,文件名,CREATEDATE

The gridview columns are chkSelect (checkbox), Id, fileName, CreateDate

当用户签一个复选框,presses一个按钮,我想收到的ID列的值。

When a user checks a checkbox, and presses a button, i want to receive the "id" column value.

下面是我的code的按钮:

Here is my code for the button:

foreach (GridViewRow row in gvUserFiles.Rows)
            {
                var test1 = row.Cells[0].FindControl("chkSelect");
                CheckBox cb = (CheckBox)(row.Cells[0].FindControl("chkSelect"));
                //chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1"));  
                if (cb != null && cb.Checked)
                {
                    bool test = true;

                }
            }

的始终cb.checked返回false。

The cb.checked always is returned false.

推荐答案

复选框被选中总是能够数据绑定的问题。确保你是不是绑定的 GridView控件之前被称为按钮单击事件。有时,人们贴在的Page_Load 事件中的数据绑定,然后将其保持在每回传数据绑定。由于它是按一下按钮之前调用,它可以使直接的影响。
GridView控件是数据绑定你失去所有从页面传来的状态。

The Checkboxes being always unchecked can be a problem of DataBinding. Be sure you are not DataBinding the GridView before the button click event is called. Sometimes people stick the DataBinding on the Page_load event and then it keeps DataBinding on every PostBack. As it is called before the button click, it can make direct influence. When the GridView is DataBound you lose all the state that came from the page.

如果您数据绑定的 GridView控件的Page_Load ,把它包核实!的IsPostBack

If you Databind the GridView on the Page_load, wrap it verifying !IsPostBack:

if (!IsPostBack)
{
    gvUserFiles.DataSource = data;
    gvUserFiles.DataBind();
}

如果这不是你的情况,你可以尝试验证根据的Request.Form 值checked属性:

If that is not your case, you can try verifying the checked property based on the Request.Form values:

protected void button_OnClick(object sender, EventArgs e)
{
    foreach (GridViewRow row in gvUserFiles.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("chkSelect");

        if (cb != null)
        {
            bool ckbChecked = Request.Form[cb.UniqueID] == "on";

            if (ckbChecked)
            {
                //do stuff
            }
        }
    }
}

经过复选框是由浏览器发送的值。而且,如果它没有在浏览器检查,没有继续请求。

The Checked value of a CheckBox is sent by the browser as the value "on". And if it is not checked in the browser, nothing goes on the request.

这篇关于返回GridView的复选框布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆