使用ASP .Net 2.0发现一个很大问题是在Javascript无法直接使用getElementById。在ASP页面中的ID无法直接引用。
ASP.NET 2.0 name mangling
1:需要使用
var CtrlCust=document.getElementById(<%Ctrl.ClientID%>);
2:但是如果控件是在FormView GridView DetailView就不行了,需要使用FindControl
txBox=(TextBox)DataSourceItemView.FindControl("TextBox");
先找到这个控件。然后txBox.ClientID, 并且Javascript需要在Page_Load中加载
3:但是这里还有问题,方法2中只是适合Default View,如果View有了变化,例如原来是Readonly转换到EditTemplate的时候,EditTemplate里面的控件使用ItemView.Find无法找到。所以需要另外的方法。在Item_Created中加载Javascript
我这里实现的是弹出窗口,返回一个数值
  protected void FormView1_ItemCreated(object sender, EventArgs e)
    {
        if (FormView1.CurrentMode == FormViewMode.Edit)
        {
             TextBox CustTextBox = (TextBox)FormView1.FindControl("main_customerTextBox");
            if (CustTextBox == null) return;
            string js = "<script language='javascript'>";
            js = js + "function findcust(CustButton)";
            js = js + "{";
            js = js + "     var ttop = CustButton.offsetTop; ";
            js = js + "     var thei = CustButton.clientHeight;";
            js = js + "     var tleft = CustButton.offsetLeft;";
            js = js + "     while (CustButton = CustButton.offsetParent){ttop+=CustButton.offsetTop; tleft+=CustButton.offsetLeft;}";

            js = js + "     var returnValue=window.showModalDialog('../Common/CommonFindCustomer.aspx','Pass paramerter to Child Window','dialogWidth:600px;dialogHeight:500px;dialogLeft:'+tleft+';dialogTop:'+ttop+';resizable:yes ');";
            js = js + "     if (returnValue == null) return;";

            js = js + "     var CtrlCust=document.getElementById('" + CustTextBox.ClientID + "');";
            js = js + "     if (CtrlCust==null)";
            js = js + "        alert('Not Found Customer Control');";
            js = js + "     else";
            js = js + "        CtrlCust.value=returnValue;";
            js = js + "}</script>";

            ClientScript.RegisterStartupScript(this.GetType(),"FindCustScript", js);


        }

请参考以下
How to access control in FormView
http://www.dotnettaxi.com/ViewTopic137362.aspx
FormView - FindControl only works for default view
http://www.dotnettaxi.com/ViewTopic138828.aspx
http://west-wind.com/weblog/posts/5127.aspx