Home > ASP.Net, Javascript > Disable and Enable TextBox via DropdowList Selection in Gridbiew

Disable and Enable TextBox via DropdowList Selection in Gridbiew


Hi all,

try this Example to Disable and Enable TextBox via DropdowList Selection in Gridbiew using Javascript

In this example i will use Category Table in Northwind Database

so let’s start this Example following this Steps :-

Step1 :- Create a new Website and add new web page and from Toolbox Add Gridview Control and Bind it to Category table using SqlDataSource control and add two Colomns to Gridview where the firs one will have a DropdownList Control and the second colomn has a TextBox which we will Enable/Disable it according DropDownList Selection so your page will be like this


    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
            DataKeyNames="CategoryID" DataSourceID="SqlDataSource1" ForeColor="#333333" OnRowDataBound="GridView1_RowDataBound">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="CategoryID" HeaderText="Category ID" InsertVisible="False"
                    ReadOnly="True" SortExpression="CategoryID" />
                <asp:BoundField DataField="CategoryName" HeaderText="Category Name" SortExpression="CategoryName" />
                <asp:TemplateField HeaderText="Payment">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddl_PaymentMethod" runat="server">
                            <asp:ListItem Value="-1">----</asp:ListItem>
                            <asp:ListItem Value="0">Month</asp:ListItem>
                            <asp:ListItem>At End</asp:ListItem>
                            <asp:ListItem Value="2">At Travel</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Value">
                    <ItemTemplate>
                        <asp:TextBox ID="txt_Value" runat="server" Width="58px" Text="0"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:SqlDataSource>

    </div>

 

Step2 :- In the Hrader of Web Page we will add Javascript function which will Enable and Disable TextBox according to Value of Selected DropdownList

    <script type="text/javascript" language="javascript">
    function EnableTextbox(ddl,txt)
    {
        var ddl_PaymentMethod= document.getElementById(ddl);
        var txt_Value=document.getElementById(txt);
        var ddl_Value=ddl_PaymentMethod.value;
        if(ddl_Value == "0")
            {
                txt_Value.disabled= false;
            }
         else
            {
                txt_Value.disabled= true;
            }  
    }
    </script>

Step3 :- In Code Behind and in RowDataBound of Gridview Control we will pass the Javascript function so your code will be like this :-

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox txt_Value = (TextBox)e.Row.FindControl("txt_Value");
            DropDownList ddl_PaymentMethod =(DropDownList) e.Row.FindControl("ddl_PaymentMethod");
            txt_Value.Attributes.Add("disabled", "disabled");
            //txt_Value.Attributes.Add("readonly", "readonly");
            ddl_PaymentMethod.Attributes.Add("onchange","javascript:EnableTextbox('"+ddl_PaymentMethod.ClientID+"','"+txt_Value.ClientID+"')");
        }
    }

So after that run your website and try the example

Hope this helps you …  Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. Suba
    February 1, 2012 at 2:31 pm

    Very nice example. It saved my hours.

  2. yasserzaid
    February 14, 2012 at 2:20 pm

    @Suba : you are welcome … hope my post helps you

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: