【问题标题】:How to update a control outside of an updatepanel?如何更新更新面板之外的控件?
【发布时间】:2010-05-19 10:36:46
【问题描述】:

在检查 CheckBox 后,我将在位于更新面板外部的 TextBox 中显示一些文本,但我无法使其工作。请帮帮我?

这是我的代码:

<asp:UpdatePanel runat="server" ID="uplMaster">
    <ContentTemplate>
        <asp:CheckBox ID="cbShowText" runat="server" Text="Show Some Text" AutoPostBack="true"
            OnCheckedChanged="cbShowText_CheckedChanged" />
    </ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="txtBox" Text="Empty" runat="server" />

代码背后:

    protected void cbShowText_CheckedChanged(object sender, EventArgs e)
    {
        txtBox.Text = "Some Text";
    }

提前致谢:D

附:正如您可能已经猜到的那样,我的问题与我的问题相似,这就是为什么我不想将 TextBox 放在 UpdatePanel 中

【问题讨论】:

    标签: asp.net updatepanel


    【解决方案1】:

    我将TextBox放在另一个UpdatePanel中,然后调用Update方法:

    这是我的新代码:

        <asp:UpdatePanel runat="server" ID="uplMaster" UpdateMode="Always">
        <ContentTemplate>
            <asp:CheckBox ID="cbShowText" runat="server" Text="Show Some Text" AutoPostBack="true"
                OnCheckedChanged="cbShowText_CheckedChanged" />
        </ContentTemplate>
       </asp:UpdatePanel>
       <asp:UpdatePanel runat="server" ID="uplDetail" UpdateMode="Conditional">
           <ContentTemplate>
               <asp:TextBox ID="txtBox" Text="Empty" runat="server" />
           </ContentTemplate>
       </asp:UpdatePanel>
    

    代码背后:

            protected void cbShowText_CheckedChanged(object sender, EventArgs e)
            {
               txtBox.Text = "Some Text";
               uplDetail.Update();
            }
    

    希望对你有帮助

    【讨论】:

    • 好方法。如果另一个控件离更新面板很远,这可以节省我们的时间。谢谢。
    • 这对我不起作用:/我仍然需要刷新页面。请帮忙
    【解决方案2】:

    文本框也必须在更新面板中。

    *编辑:

    很抱歉,我没有正确阅读您的问题。也许写一个 javascript 函数,然后从代码隐藏中调用该函数?

    【讨论】:

      【解决方案3】:

      我知道这个问题已经有一段时间了,但这就是我所做的。就像@bla 说的写一个javascript函数并从后面的代码中调用它。

      所以在你检查过的更改中调用这个。 changeText 是页面上页眉或脚本文件中的一个 javascript 函数。

      protected void cbShowText_CheckedChanged(object sender, EventArgs e)
      {
          ScriptManager.RegisterStartupScript(this, GetType(), "Show Different Text", "changeText();", true);
      }
      

      示例 Javascript。只是在检查更改事件从后面的代码触发时被调用。

      <script type="text/javascript">
          function changeText() {
              var txt= document.getElementById('<%= txtBox.ClientID %>');
              var chk = document.getElementById('<%= cbShowText.ClientID %>');
              if (chk.checked === true) {
                  txt.Text = "Something";
              } else {
                  txt.Text = "Somethingelse";
              }
          }
      </script>
      

      希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多