0

如果有人可以帮助我,我在GridView上有问题,当我使用on Roweding和OnrowDeleting和Inside ASP:TemplateField,ItemTemplate内部时,我正在使用复选框,当在复选框中选中复选框时,Checkecked Value从来都不正确,下面是我的C#脚本

<asp: Button ID = "Button1" runat = "server" BackColor = "# EFF1F1" BorderStyle = "None"
onclick = "Button1_Click" Text = "Submit Work Program" />
<asp: GridView ID = "GV_program" runat = "server" AutoGenerateColumns = "False"
Its DataKeyNames = "KD_BIDANG" Width = "100%" AutoGenerateEditButton = "True"
AutoGenerateDeleteButton = "true" OnRowEditing = "GV_program_RowEditing" 
OnRowDeleting = "GV_program_RowDeleting" OnRowDataBound = "GV_kegiatan_RowDataBound" 
BackColor = "White" EmptyDataText = "No Data Activity" AllowPaging = "True">
<EmptyDataRowStyle BackColor="#FF9900" />
<Columns> <asp:TemplateField HeaderStyle-Width="20px"> <ItemTemplate> 
<asp:CheckBox ID="cbSelect" runat="server" /> </ItemTemplate> </asp:TemplateField>
</ Columns> </ asp: GridView>

C #
protected void Button1_Click (object sender, EventArgs e)
{
  foreach (GridViewRow row in GV_program.Rows)
  {
     if (((CheckBox) row.FindControl ("cbSelect")). Checked)
     {
        // Delete something (never get here)
     }
  }
}
4

1 回答 1

1

一步一步,直到你让它工作......

尝试这个:

/* put this value at the "class level" */
    public static readonly int GRID_VIEW_COLUMN_ORDINAL_chkBoxSELECT = 3; /* Your number may be different, its the ordinal column number.  A static readonly or const value makes your code more readable IMHO */

/*Then in your method */

if (null != this.GV_program)
{
    Control cntl = null;

    foreach (GridViewRow gvr in this.GV_program.Rows)
    {
        cntl = gvr.Cells[GRID_VIEW_COLUMN_ORDINAL_chkBoxSELECT].FindControl("cbSelect");
        CheckBox cbIsApproved = cntl as CheckBox;
        if (null != cbIsApproved)
        {
            bool myValue = cbIsApproved.Checked;
        }
    } 
}

我刚刚运行了这段代码,它对我有用。

    protected void imgbutSave_Click(object sender, ImageClickEventArgs e)
    {

    if (null != this.gvMain)
    {
        Control cntl = null;
        string finalMsg = string.Empty;
        int counter = 0;
        StringBuilder sb = new StringBuilder();
        foreach (GridViewRow gvr in this.gvMain.Rows)
        {
            counter++;
            cntl = gvr.Cells[GRID_VIEW_COLUMN_ORDINAL_chkBoxIsApproved].FindControl("chkBoxIsApproved");
            CheckBox cbIsApproved = cntl as CheckBox;
            if (null != cbIsApproved)
            {
                sb.Append(string.Format("Row '{0}' (chkBoxIsApproved.Checked) = '{1}'", counter, cbIsApproved.Checked) + System.Environment.NewLine);
            }
        }

        finalMsg = sb.ToString();
    }
}

和我的 aspx 代码

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkBoxIsApproved" runat="server" Checked='<%#Eval("IsApproved")%>'>
                        </asp:CheckBox>
                    </ItemTemplate>

                </asp:TemplateField>

我的 StringBuilder 具有我页面上的所有复选框,并且具有正确的 .Checked 值。

于 2013-04-10T13:48:54.627 回答