Click here to Skip to main content
15,902,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Gridview which has Combobox,Textbox using Templete Field

When I select the Combobox value is 'DR' the Textbox will be Enabled False and Combobox Value is 'CR' It will be Enabled True.

How to do it? Please help me...

I am using this code but no action occured
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
           int i=e.Row.RowIndex;
            string _SelectedValue = ((DropDownList)GridView1.Rows[i].FindControl("cmbtype")).SelectedValue.ToLower();
            if (_SelectedValue == "DR".ToLower())
            {
                ((TextBox)GridView1.Rows[i].FindControl("txtdebit")).Enabled = false;
                ((TextBox)GridView1.Rows[i].FindControl("txtcredit")).Enabled = true;
            }
            else
            {
                ((TextBox)GridView1.Rows[i].FindControl("txtdebit")).Enabled = true;
                ((TextBox)GridView1.Rows[i].FindControl("txtcredit")).Enabled = false;
            }


    }


This event is calling before rowcreated. So Which event is calling after RowDatabound please help me
Posted
Updated 19-Jun-12 22:43pm
v4
Comments
Prosan 20-Jun-12 4:57am    
you add js on combox on change event than it will work.
devausha 20-Jun-12 4:59am    
I didn't know how to do it? Please help me

It may be attributes.add() funtion to give rowdatabound event. Then how to write js
Prosan 20-Jun-12 5:22am    
look my answer i have give one simple example convert it to your requirement.

In GridView an event is there OnRowDataBound .Inside that event write
C#
If(COmboBox.Selected == 'DR')
{
TextBox1.Enable = false;
}
else
{
TextBox1.Enable = true;
}
 
Share this answer
 
In a gridview when we use controls and want to perform any action then firstly we have to find that control.

So preferably use findcontrol to find the dropdown and textbox in a gridview because you have to use selectedindexchange for execution of the conditon and also find your textbox in a gridview..
 
Share this answer
 
v3
please look this simple example

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
       {
            int i = e.Row.RowIndex;
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
            TextBox tx = (TextBox)e.Row.FindControl("txt");

// add these line than it will solve your current problem 

            if (ddl.SelectedValue == "DR".ToLower())
                tx.Enabled = false;
            else
                tx.Enabled = true;
//----------------------------------------------
            ddl.Attributes.Add("onchange", "if (" + ddl.ClientID + ".value=='dr'){" + tx.ClientID + ".disabled=true;}else{" + tx.ClientID + ".disabled=false;} "); 
        }
    }
 
Share this answer
 
v3
Comments
devausha 20-Jun-12 5:32am    
Thank u. I try this coding It is Working. But I am Creating GridviewRow Dynamically And Get the Values using ViewState["CurrentTable"] method . So it is disabled But it is automaticcally changed again enabled

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900