我的页面十分简单,在一个页面Test.aspx上放一个UpdatePanel1,里面放了一个PlaceHolder1控件和一个Button1按钮,点击Button时动态加载一个UserControl(WebUserControl.ascx),UserControl里有一个LinkButton1和一个TextBox1,点击LinkButton1使得TextBox1的Text加上等于“ABC”。Test.aspx代码:
public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["ControlID"] != null)
        {
            AddControl();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ViewState["ControlID"] = "WebUserControl";
        AddControl();
    }       private void AddControl()
    {
        UserControl uc = (UserControl)LoadControl("~/WebUserControl.ascx");
        uc.ID = "WebUserControl";
        this.PlaceHolder1.Controls.Clear();
        this.PlaceHolder1.Controls.Add(uc);
    }
}WebUserControl.ascx代码:
public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.TextBox1.Text += "ABC";
    }
}前置页代码我就不贴了,很简单,控件托上去就有了。
我的UpdatePanel的设置都没问题,Atlas的版本也最新。我用Button代替LinkButton就没问题,但用LinkButton就会报Javascript的错误。不知哪位高手可以解决?小弟在这里多谢了!

解决方案 »

  1.   

    估计是placeholder的问题,换成其他的试试
      

  2.   

    @zhuyt0828(假扮的猛男) 
    换成Panel也一样。应该不是这个问题。
      

  3.   

    我查了一下,LinkButton会自己生成js的,会不会由于这个原因而报js错误?MSDN:LinkButton 控件在客户端浏览器上呈现 JavaScript。客户端浏览器必须启用了 JavaScript,此控件才能正常运行。
      

  4.   

    @feixiangdg() 
    启示我做这个例子是因为在GridView中SelectCommand是linkButton类型
    正是因为碰到了才做这个例子尝试的,结果与我的预料完全相同。所以法帖求助帮忙!
      

  5.   

    不知道这个是Atlas的Bug还是有其他设置的问题,还请各位同僚提供帮助。
    谢谢!
      

  6.   

    我找到了解决办法,但是我不知道什么原因
    我在test页面加了个linkbutton然后dispaly:none,这样就能用了。
    大家揣摩一下到底啥道理啊
      

  7.   

    之前报错是因为页面上没有找到__doPostBack('WebUserControl$LinkButton1','')
    我在页面加了一个linkbutton之后,他会自动给页面添加<script type="text/javascript">
    <!--
    var theForm = document.forms['form1'];
    if (!theForm) {
        theForm = document.form1;
    }
    function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
        }
    }
    // -->
    </script>
    这样就找到调用的方法了
    linkbutton如果visible=false他也不会加这个方法
      

  8.   

    首先,如果单纯把UpdatePanel去掉(也就是删除<altas:UpdatePanel>和<ItemTemplate>这两层标记),页面正常吗?也有可能Atlas在页面生命周期的某一阶段后就不再接受其内部动态添加的控件。
      

  9.   

    @zhuyt0828(假扮的猛男) 
    照你的说法试了一下,果然可以。多谢
    不过我也不知道原因。请高手赐教!
      

  10.   

    那是因为 AltasProxy 无法直接在页面上注册 __doPostBack() javascript LinkButton 触发服务端事件,需要这个方法:<a id="WebUserControl_LinkButton1" href="javascript:__doPostBack('WebUserControl$LinkButton1','')">LinkButton</a>直接的解决方法是:Test.aspx 上加入:<script type="text/javascript">
    <!--
    var theForm = document.forms['form1'];
    if (!theForm) {
        theForm = document.form1;
    }
    function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
        }
    }
    // -->
    </script>
      

  11.   

    fangxinggood(JustACoder) 的方法比较好,也给出解释了
      

  12.   

    @zhuyt0828(假扮的猛男) 
    但这样用好像linkbutton的样式都没了
    我现在在GridView中的点击CommondField的“选择”后,该行的样式就没了,不知道什么原因,请执教!
      

  13.   

    那是因为这行变成selectedrow了,你要在<SelectedRowStyle />中设定他的样式
    是这个问题嘛?