How To Enable Disable Textbox in Footer

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    public void Page_Load()
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = _sampleData;
            GridView1.DataBind();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            (e.Row.FindControl("txtDiscountOnTotal") as WebControl).Enabled = (comboDiscountType.SelectedValue == "2");
        }
        else if (e.Row.RowType == DataControlRowType.DataRow)
        {
            (e.Row.FindControl("txtDiscountOnItem") as WebControl).Enabled = (comboDiscountType.SelectedValue == "1");
        }
    }

    protected void comboDiscountType_SelectedIndexChanged(object sender, EventArgs e)
    {
        (GridView1.FooterRow.FindControl("txtDiscountOnTotal") as WebControl).Enabled = (comboDiscountType.SelectedValue == "2");
        foreach (GridViewRow row in GridView1.Rows)
        {
            (row.FindControl("txtDiscountOnItem") as WebControl).Enabled = (comboDiscountType.SelectedValue == "1");
        }
    }
    public DataTable _sampleData
    {
        get
        {
            DataTable dt = (DataTable)Session["DataTable"];
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add(new DataColumn("ID", typeof(string)));
                dt.Columns.Add(new DataColumn("Item", typeof(string)));
                dt.Columns.Add(new DataColumn("Price", typeof(string)));


                dt.Rows.Add(new object[] { "1", "XX", "12.43", });
                dt.Rows.Add(new object[] { "2", "XX1", "11.43", });
                dt.Rows.Add(new object[] { "3", "XX2", "13.43", });
                Session["DataTable"] = dt;
            }
            return dt;
        }
        set
        {
            Session["DataTable"] = value;

        }
    }
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
                ShowFooter="true" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="Item" HeaderText="Item " />
                    <asp:BoundField DataField="Price" HeaderText="Price" />
                    <asp:TemplateField HeaderText="Discount">
                        <ItemTemplate>
                            <asp:TextBox ID="txtDiscountOnItem" Width="50px" runat="Server" />
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtDiscountOnTotal" Width="50px" runat="Server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:DropDownList ID="comboDiscountType" runat="server" AutoPostBack="True" OnSelectedIndexChanged="comboDiscountType_SelectedIndexChanged">
                <asp:ListItem Value="1">Discount on Items</asp:ListItem>
                <asp:ListItem Value="2">Discount on Total</asp:ListItem>
            </asp:DropDownList>
        </div>
    </form>
</body>
</html>

No comments:

Post a Comment