开发者

disable a checkbox in gridview

开发者 https://www.devze.com 2023-01-06 16:37 出处:网络
I have a gridview in which the first column in a checkbox. The grid displays one or more records that are returned from the database upon user search (the grid is populated in a proc called \"protecte

I have a gridview in which the first column in a checkbox. The grid displays one or more records that are returned from the database upon user search (the grid is populated in a proc called "protected void DisplayGridData()"). If the "inactive" column of that record is "1", I would like to have the checkbox disabled. How can I accomplish that?

The html code is:

            <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:Bou开发者_Python百科ndField>
                </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>

The code that loads the gridview is:

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

The code for gridview databoundrow is:

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


Hook in to the RowDataBound event of the GridView. From there, you can cast the DataItem to your record type. A test for the flag then lets you modify the row controls.

I have this code in VB.net and did a quick conversion per your C# tag, so please forgive any errors.

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

EDIT: Per your edit to the question, you do not have OnRowDataBound in your markup. You would need to add OnRowDataBound="gvCanadaOrders_RowDataBound". It works the same way you've gon OnPageIndexChanging.


Handle the RowDataBound event and use e.Row parameter to find your checkbox.


It won't work on all cases, if you are using the HTML checkbox you should proceed by other by disabling property from CSS:

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

Reference link

0

精彩评论

暂无评论...
验证码 换一张
取 消