禁用gridview中的复选框

时间:2010-07-08 17:39:19

标签: c# asp.net .net-3.5 gridview

我有一个gridview,其中复选框中的第一列。网格显示用户搜索时从数据库返回的一个或多个记录(网格填充在名为“ protected void DisplayGridData()”的过程中)。如果该记录的“非活动”列为“1”,我希望禁用该复选框。我怎么能做到这一点?

html代码是:

            <asp:GridView ID="gvCanadaOrders" runat="server" AutoGenerateColumns="False" CellPadding="2" CellSpacing="2" GridLines="None" Width="100%" AllowPaging="True" PageSize="30"
                onpageindexchanging="gvCanadaOrders_PageIndexChanging" ForeColor="#333333" DataKeyNames="RecID">
                <Columns>
                    <asp:TemplateField HeaderText="Disable" >
                        <ItemTemplate>
                            <asp:CheckBox ID="cbPONumber" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <%--<asp:BoundField DataField="Rec_ID" HeaderText="Rec_ID" HtmlEncode="False"></asp:BoundField>--%>
                    <asp:BoundField DataField="Inactive" HeaderText="Inactive" HtmlEncode="False" ></asp:BoundField>
                    <asp:BoundField DataField="PO_Number" HeaderText="PO Number" HtmlEncode="False" ></asp:BoundField>
                    <asp:BoundField DataField="VENDOR_NAME" HeaderText="Vendor Name"></asp:BoundField>
                    <asp:BoundField DataField="ITEM_DESC" HeaderText="Item Description"></asp:BoundField>
                    <asp:BoundField DataField="MFG_PART_NO" HeaderText="MFG Part Number"></asp:BoundField>
                    <asp:BoundField DataField="System_DATE" HeaderText="System Date"></asp:BoundField>
                    <asp:BoundField DataField="PRINT_DATE" HeaderText="Print Date"></asp:BoundField>
                </Columns>
                <FooterStyle CssClass="GridFooter" BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle CssClass="GridPager" ForeColor="#333333" BackColor="#FFCC66" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <HeaderStyle CssClass="GridHeader" BackColor="#990000" Font-Bold="True" ForeColor="White"  />
                <RowStyle CssClass="GridItem" BackColor="#FFFBD6" ForeColor="#333333" />
                <AlternatingRowStyle  CssClass="GridAltItem" BackColor="White" />
            </asp:GridView>

加载gridview的代码是:

protected void btnDisable_Click(object sender, EventArgs e)    
{
    try
    {
        string strPONumber = "";
        foreach (GridViewRow gvr in gvCanadaOrders.Rows)
        {
            if (((CheckBox)gvr.FindControl("cbPONumber")).Checked == true)
            {
                string strRec_ID = gvCanadaOrders.DataKeys[gvr.RowIndex].Value.ToString();

                //Update table here, diable inactive field;
                SqlConnection con = new SqlConnection(strConn);
                string sqlCanadaOrdersUpdate = "usp_CanadaOrders_Close";
                SqlCommand cmdCanadaOrdersUpdate = new SqlCommand(sqlCanadaOrdersUpdate, con);

                cmdCanadaOrdersUpdate.CommandType = CommandType.StoredProcedure;

                cmdCanadaOrdersUpdate.Parameters.Add(new SqlParameter("@rec_ID", SqlDbType.VarChar, 50));
                cmdCanadaOrdersUpdate.Parameters["@rec_ID"].Value = strRec_ID;

                cmdCanadaOrdersUpdate.Parameters.Add(new SqlParameter("@usr_id", SqlDbType.VarChar, 50));
                cmdCanadaOrdersUpdate.Parameters["@usr_id"].Value = User.Identity.Name.Substring(User.Identity.Name.Length - 7);

                con.Open();

                SqlDataAdapter sqladaCanadaOrdersUpdate = new SqlDataAdapter(cmdCanadaOrdersUpdate);
                cmdCanadaOrdersUpdate.ExecuteNonQuery();
            }
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Purchase Order " + strPONumber + " has been disabled.');", true);
        }
    }

gridview databoundrow的代码是:

void gvCanadaOrders_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Display the company name in italics.
        e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
    }
}

3 个答案:

答案 0 :(得分:3)

挂钩到GridView的RowDataBound事件。从那里,您可以将DataItem强制转换为您的记录类型。然后,对该标志的测试可以修改行控件。

我在VB.net中有这个代码并根据你的C#标签进行了快速转换,所以请原谅任何错误。

void myGridView_RowDataBound(Object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.DataRow) {
        MyObject myObj = (myObj)e.Row.DataItem;
        if (myObj.flag) {
            CheckBox cb = (CheckBox)e.Row.FindControl("myCheckBox");
            cb.Enabled=false;
        }
    }
}

编辑:根据您对该问题的编辑,您的标记中没有OnRowDataBound。您需要添加OnRowDataBound =“gvCanadaOrders_RowDataBound”。它的工作方式与OnPageIndexChanging一样。

答案 1 :(得分:0)

处理RowDataBound事件并使用e.Row参数找到您的复选框。

答案 2 :(得分:0)

它不适用于所有情况,如果您使用HTML复选框,则应通过禁用CSS中的属性继续执行其他操作:

e.Row.Cells[0].Attributes["disabled"] = "disabled";

Reference link