How to update a control outside of an updatepanel?

前端 未结 3 774
心在旅途
心在旅途 2021-01-06 02:25

I am going to show some text in a TextBox, which is located outside of an updatepanel, after checking a CheckBox but I cannot make it work. please help me out ?

Here

相关标签:
3条回答
  • 2021-01-06 02:37

    The textbox has to be in update panel also.

    *Edit:

    I am sorry I didn't read your question properly. Perhaps write a javascript function, and call the function from codebehind?

    0 讨论(0)
  • 2021-01-06 02:47

    I put the TextBox in another UpdatePanel and then called the Update method:

    Here is my new code:

        <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>
    

    Code Behind:

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

    Hope this helps

    0 讨论(0)
  • 2021-01-06 02:49

    I know its been a while since this was asked, but here is what I did. Like @bla said write a javascript function and call it from code behind.

    So in your checked changed call this. The changeText is a javascript function on your page in the header or in a script file.

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

    Sample Javascript. Just gets called when checked changed event fires from code behind.

    <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>
    

    Hope this helps someone.

    0 讨论(0)
提交回复
热议问题