更新时间: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 occuredprotected 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 meIn GridView an event is there OnRowDataBound .Inside that event writeIf(COmboBox.Selected == 'DR') { TextBox1.Enable = false; } else { TextBox1.Enable = true; }
In agridview
when we use controls and want to perform any action then firstly we have to find that control.
So preferably usefindcontrol
to find thedropdown
andtextbox
in agridview
because you have to useselectedindexchange
for execution of the conditon and also find yourtextbox
in agridview
..
please look this simple exampleprotected 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;} "); } }