且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行时在GridView中启用文本框

更新时间:2023-01-15 20:21:19

在GridView中有一个OnRowDataBound事件.在该事件中写入
 If(COmboBox.Selected == '  DR')
{
TextBox1.Enable =  false ;
}
其他
{
TextBox1.Enable =  true ;
} 


gridview 中,当我们使用控件并要执行任何操作时,首先必须找到该控件.

因此,***使用findcontrol gridview 中找到dropdown textbox ,因为您必须使用selectedindexchange 来执行条件,并且还要在gridview中找到您的textbox . blockquote>
请看这个简单的例子

 受保护的 无效 GridView1_RowDataBound(对象发​​件人,GridViewRowEventArgs e)
    {
        如果(例如,Row.RowType == DataControlRowType.DataRow)
       {
             int  i = e.Row.RowIndex;
            DropDownList ddl =(DropDownList)e.Row.FindControl(" );
            TextBox tx =(TextBox)e.Row.FindControl(" );

// 添加这些行,它将解决您当前的问题

            如果(ddl.SelectedValue == " 跨度> .ToLower())
                tx.Enabled =  false ;
            其他
                tx.Enabled =  true ;
//  ------------------------ ---------------------- 
            ddl.Attributes.Add(" " 如果(" + ddl.ClientID +   .value =='dr'){" + tx.ClientID +  .disabled = true;} else {" + tx.ClientID +  .disabled = false;}");
        }
    } 


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

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

In GridView an event is there OnRowDataBound .Inside that event write
If(COmboBox.Selected == 'DR')
{
TextBox1.Enable = false;
}
else
{
TextBox1.Enable = true;
}


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..


please look this simple example

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