【问题标题】:the usercontrol inside updatepanel problemupdatepanel里面的usercontrol问题
【发布时间】:2011-04-01 00:24:22
【问题描述】:

我在更新面板中有一个用户控件。当我单击某个按钮时,此用户控件将是 加载。并且用户控件本身有另一个按钮。问题是 后面的用户控件代码永远不会执行,当单击按钮时,用户控件 消失。

我知道这是一个常见问题。但我还没有找到一个很好的详细解决方案。

我很感激。

【问题讨论】:

  • 您是否在用户控件的页面加载时设置了断点以查看会发生什么?

标签: c# asp.net


【解决方案1】:

在您的用户控件中,只需使用这样的标准 HTML 按钮:

<input type="button" id="myButton" onclick="clickTheButton();" value="Click Me"/>

这将调用 javascript 方法“clickTheButton”,如下所示:

<script type="text/javascript">
function clickTheButton() {

    var Sender = window.event.srcElement;

    //here you can go gather any other values that you need to support the post back

    var PostBackData = textboxCity.value + "|" + selectState.value
    if(confirm("are you sure?"))
    {
            __doPostBack(Sender.ID, PostBackData)
    }
}
</script>

所以现在您要从 Javascript 调用回发,识别 Sender Control 和命令参数。这些值作为 __EventTarget 和 __EventArgument 与 post 一起传递,并且可以从页面加载中的 httpRequest 获得,例如:

protected void Page_Load(object sender, EventArgs e)
{
  string controlName = Request.Params.Get("__EVENTTARGET");
  string[] commandArguments = Request.Params.Get("__EVENTARGUMENT").split('|')
}

一旦您使用累积到 commandArguments 中的任何值进行页面加载,您应该能够调用您想要的任何其他方法。

这应该会让你指向正确的方向。

干杯,

CEC

【讨论】:

    【解决方案2】:

    在更新面板中动态加载用户控件很棘手。

    最好的解决办法是将其保存在 ViewState 中,Here is a tutorial and sample application

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多