如何在运行时在GridView中启用文本框 [英] How to enabled textbox at runtime in gridview

查看:71
本文介绍了如何在运行时在GridView中启用文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Combobox的Gridview,使用Templete Field的文本框是

当我选择组合框"值为"DR"时,文本框将启用False,而组合框"值为"CR"则将启用True.

怎么做?请帮帮我...

我正在使用此代码,但未发生任何操作

 受保护的 无效 GridView1_RowDataBound(对象发​​件人,GridViewRowEventArgs e)
    {
            int  i = e.Row.RowIndex;
            字符串 _SelectedValue =((DropDownList)GridView1.Rows [i] .FindControl(" )).SelectedValue.ToLower();
            如果(_SelectedValue == "  .降低())
            {
                (((TextBox)GridView1.Rows [i] .FindControl(" )).Enabled =  false ;
                (((TextBox)GridView1.Rows [i] .FindControl(" )).Enabled =  true ;
            }
            其他
            {
                (((TextBox)GridView1.Rows [i] .FindControl(" )).Enabled =  true ;
                (((TextBox)GridView1.Rows [i] .FindControl(" )).Enabled =  false ;
            }


    } 



在创建行之前调用此事件.因此,在RowDatabound之后调用哪个事件,请帮助我

解决方案

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


这篇关于如何在运行时在GridView中启用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆