日期:2014-05-18  浏览次数:20415 次

dropdownlist 中的 js代码
在gridview模板中添加了dropdownlist,实现用javascrpt脚本在第一行的dropdownlist改变以后,其他几行的dropdownlist的值和第一行的dropdownlist值一样

<script language="javascript" type="text/javascript">
  function onfirstTxtChange()
  {
  var gv = document.getElementById('<%= GridView1.ClientID %>');
  var txt = document.getElementById('<%= FirstTextBoxClientID %>');
  var selects = gv.getElementsByTagName("select");
  for(var i = 0; i < selects.length; i++)
  {selects[i].value = txt.value;}
  }
</script>
private int i = 0;
  private string _firstDropDownlistClientID;
  protected string FirstDropDownlistClientID
  {
  get { return _firstDropDownlistClientID; }
  set { _firstDropDownlistClientID = value; }
  }

  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  if (i++ > 0)
  return;
  else
  {
  DropDownList tb = (DropDownList)e.Row.FindControl("week"); tb.Attributes.Add("onchange", "onfirstTxtChange()");
  FirstDropDownListID = tb.ClientID;
  }

  }
  }
}

运行结果:没有可用于当前位置的源代码
该怎么修改


------解决方案--------------------
不需要写怎么复杂吧,

你试下这个可以实现不



DropDownList1.DataSource = 数据源 (select * from tab ) 
DropDownList1.DataTextField = “数据中的字段” // 显示的 
DropDownList1.DataValueField=“ 数据中的字段” // 主要是绑定 ID 之类的 用来获取 
DropDownList1.DataBind(); 


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //当DropDownList1 数据改变时 

ID = DropDownList1.SelectedValue 
DropDownList2.DataSource = 数据源 (select * from tab where tabid = 'Id') 
DropDownList2.DataTextField = “数据中的字段” // 显示的 
DropDownList2.DataValueField=“ 数据中的字段” // 主要是绑定 ID 之类的 用来获取 
DropDownList2.DataBind(); 


原理就是这样了,我没试过,写起太麻烦了,你改下就好了,我以前用过
------解决方案--------------------
JScript code

function onSelectChange()
{
            var array = ('<%=ToChangeSelectIDs %>').split(',');
            var gv = document.getElementById('<%= GridView1.ClientID %>');
            var mySelect = document.getElementById('<%= FirstSelectClientID %>');
            
            var selIndex = mySelect.selectedIndex;
           
            
            var inputs = gv.getElementsByTagName("select");
                      
            for(var i = 0; i < inputs.length; i++)
            {
                    for(var j = 0; j < array.length; j++)
                    {   
                        if(inputs[i].id == array[j]) 
                        {

                            inputs[i].options[selIndex].selected = true;
                            break;
                        }
                   }
            }
}