【问题标题】:JavaScript method not working in ASP.NET User ControlJavaScript 方法在 ASP.NET 用户控件中不起作用
【发布时间】:2012-02-09 02:29:54
【问题描述】:

我之前问过一个关于 JavaScript 代码的问题,您可以在这里看到它:How can I scroll down to a multiline TextBox's bottom line, Javascript's scrollIntoView is not working for this。好吧,它已经解决了,并且接受的答案正在起作用。

之后,我不得不将您在该问题中看到的代码部分移动到最终看起来像这样的用户控件:

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LiveChatPart.ascx.cs" Inherits="BeyzamComArayuz.LiveChatUserControl.LiveChatPart" %>
 <%@ Register src="/LiveChatUserControl/GenelOdaFlashPart.ascx" tagname="GenelOdaFlash" tagprefix="gOF" %>
 <script language="javascript" type="text/javascript">
 function buttonClicked() {
    //            var el = document.getElementById("TxtBxOdaMesajlari");
    var textBox = $get("TxtBxOdaMesajlari");
    textBox.scrollTop = textBox.scrollHeight;
    //            $get("TxtBxOdaMesajlari").scrollIntoView("false");
}
</script>
<asp:ScriptManager ID="ScrptMngr" runat="server"></asp:ScriptManager>
<tr>  
                          <gOF:GenelOdaFlash runat="server"></gOF:GenelOdaFlash>
                            <td valign="top" align="left">
                                <table>


                                </table>
                            </td>
                            <td valign="top" align="left">
                                <table>
                                    <tr>
                                        <td>
                                            buraya imajlar gelecek - chip vs.
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:ListBox ID="LstBxOdadakiKullanicilar" runat="server"
                                             Width="175" Height="281" Enabled="false">
                                                <asp:ListItem Value="1" Text="Bir"></asp:ListItem>
                                            </asp:ListBox>
                                        </td>
                                    </tr>
                                    <tr>

                                        <td>
                                            <asp:UpdatePanel ID="UpdtPnlButtonlar" runat="server">
                                                <ContentTemplate>
                                                    <table>
                                                        <tr>
                                                            <td>


                                                    <asp:Button ID="ButtonOdadanCik" runat="server" Text="Odadan Çık" Width="175" Height="22" />
                                                    <asp:Button ID="ButtonHediyeGonder" runat="server" Text="Hediye Gönder" Width="175"
                                                        Height="22" />
                                                    <asp:Button ID="ButtonFullEkran" runat="server" Text="Full Ekran" Width="175" Height="22" />

                                                        <table>
                                                            <tr>
                                                                <td>
                                                                    <asp:Panel ID="PnlKontroller" runat="server" GroupingText="Kontroller" 
                                                                    Visible="false">
                                                                        <table>
                                                                            <tr>
                                                                                <td>
                                                                                    <asp:Label ID="LblChatTuru" runat="server" Text="Ücretsiz!"></asp:Label>
                                                                                </td>
                                                                            </tr>
                                                                            <tr>
                                                                                <td>
                                                                                    <asp:Button ID="BttnSohbetTuruDegistir" runat="server" 
                                                                                    Width="175" Text="Sohbet Türü Değiştir"
                                                                                     OnClick="BttnSohbetTuruDegistir_click" />

                                                                                    <asp:Button ID="BttnMolaVer" runat="server" 
                                                                                    Width="175" Text="Mola Ver" OnClick="BttnMolaVer_click" />
                                                                                </td>
                                                                            </tr>
                                                                        </table>

                                                        </asp:Panel>
                                                                </td>
                                                            </tr>
                                                        </table>


                                                    </td>

                                                        </tr>

                                                    </table>
                                                </ContentTemplate>
                                            </asp:UpdatePanel>

                                                <asp:Button ID="ButtonOzelSohbet" runat="server" Text="Özel Sohbet" Height="108"
                                                        Width="175" OnClick="ButtonOzelSohbet_click" />
                                                    <asp:Panel ID="PnlOzelSohbetKamera" runat="server" Visible="false">                                                           
                                                <asp:Label ID="asd" runat="server" Text="zsa"></asp:Label>
                                                    <div id="ozelOdaKamera" style="height: auto; float: left;">
                                                <script type="text/javascript" src="/jwplayer/swfobject.js"></script>
                                                <script type="text/javascript">
                                                    //            For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. 
                                                    var swfVersionStr = "11.1.0";
                                                    //            <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
                                                    var xiSwfUrlStr = "playerProductInstall.swf";
                                                    var flashvars = {};
                                                    var params = {};
                                                    params.quality = "high";
                                                    params.bgcolor = "#FFFFFF";
                                                    params.allowscriptaccess = "sameDomain";
                                                    params.allowfullscreen = "true";
                                                    var attributes = {};
                                                    attributes.id = "uyeOzelOdaKameraFlash";
                                                    attributes.name = "uyeOzelOdaKameraFlash";
                                                    attributes.align = "middle";
                                                    swfobject.embedSWF(
                                                                        "/Images/uyeOzelOdaKameraFlash.swf", "ozelOdaKamera",
                                                                        "220", "150",
                                                                        swfVersionStr, xiSwfUrlStr,
                                                                        flashvars, params, attributes);
                                                    //          <!-- JavaScript enabled so display the flashContent div in case it is not replaced with a swf object. -->
                                                    swfobject.createCSS("#flashContent", "display:block;text-align:left;");
                                                    </script>                                                    
                                            </div>
                                                    </asp:Panel>

                                        </td>

                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                                        <td style="padding-left:8px;">
                                            <asp:UpdatePanel ID="UpdtPnlMesajlar" runat="server" EnableViewState="true">
                                                <ContentTemplate>
                                                    <table>
                                                        <tr>
                                                            <td>
                                                                <table>


                                                        <tr>
                                                            <td>
                                                                <%--<div id="satir">--%>

                                                                <asp:TextBox ID="TxtBxOdaMesajlari" runat="server" 
                                                                ReadOnly="true" TextMode="MultiLine"
                                                                Height="100" Width="350">                                                                        
                                                                </asp:TextBox>
                                                                <%--</div>--%>
                                                            </td>
                                                        </tr>

                                                        <tr>
                                                            <td>
                                                                <asp:TextBox ID="TxtBxMesaj" runat="server" Width="285"></asp:TextBox>
                      <%--this is the button which should run the javascript function--%>
                                                                <asp:Button ID="BttnGonder" runat="server" Text="Gönder" 
                                                                Width="58" OnClick="BttnGonder_click"/>
                                                            </td>
                                                        </tr>  

                                                                     </table>    
                                                            </td>
                                                            <td valign="top">

                                                            <asp:Panel ID="PnlAktiviteKayitlari" runat="server" 
                                                            Visible="false" GroupingText="Aktivite Kayıtları">
                                                                <table>
                                                                    <tr>
                                                                        <td valign="top">
                                                                            <asp:ListBox ID="LstBxAktiviteKayitlari" runat="server"
                                                                            Enabled="false" Width="128">
                                                                    <asp:ListItem Value="1" Text="Bir giriş yaptı"></asp:ListItem>
                                                                    </asp:ListBox>
                                                                        </td>
                                                                    </tr>

                                                                </table>

                                                            </asp:Panel>

                                                            </td>
                                                        </tr>                                         
                                                    </table>
                                                </ContentTemplate>
                                            </asp:UpdatePanel>
                                        </td>
                                    </tr>

在 asp.cs 文件中我有

protected void BttnGonder_click(object sender, EventArgs e)
        {
            string cariId = "";
            if (KullaniciTuru == 1)
            {
                cariId = Session["ModelId"].ToString();

            }
            else
            {
                cariId = Session["UyeId"].ToString();
            }
            string uyeHesapAdi = BAL.Cari.Cari.GetCariHesapAdi(int.Parse(cariId));

            string mesaj = uyeHesapAdi + " : " + TxtBxMesaj.Text;
            TxtBxOdaMesajlari.Text = TxtBxOdaMesajlari.Text + Environment.NewLine + mesaj;
            //TxtBxOdaMesajlari.Focus();

            ScriptManager.RegisterStartupScript(this, this.GetType(), "txtbxmesajlarslide", "buttonClicked();", true);
            TxtBxMesaj.Text = string.Empty;
        }

我的问题是buttonClicked() 功能不再起作用,我尝试使用ScottS's answer 中的Page.ClientScriptManager.RegisterClientScriptInclude 和这个Page.ClientScript.RegisterClientScriptBlock,它们都不起作用。我该怎么办?

编辑:我尝试使用 IE9 的内置调试器调试网页,并在 buttonClicked() 函数中设置断点,但从未命中。然后我使用了按钮的 OnClientClick 事件并为其添加了 buttonClicked 函数,它适用于每次点击,但随后文本框会跳回到 buttonClicked() 函数之前的位置。

【问题讨论】:

    标签: c# javascript asp.net user-controls updatepanel


    【解决方案1】:

    问题是用户控件上的控件的 id 有点混乱,因为它们包括用户控件的 id 加上实际控件的 id。因此,您的控件的 id 可能类似于:ucLiveChatPart_TxtBxOdaMesajlari

    处理这个问题的简单方法是改变这一行:

    var textBox = $get("TxtBxOdaMesajlari");
    

    到:

    var textBox = document.getElementById('<%= TxtBxOdaMesajlari.ClientID %>');
    

    现在,如果您将 javascript 提取到单独的文件中,或者您想使用不同的方法,您可以将控件 id 从代码隐藏发送到 javascript 变量中,然后改用它。

    在代码隐藏中:

    ScriptManager.RegisterStartupScript(this, this.GetType(), "TxtBxOdaMesajlariId", "var m_TxtBxOdaMesajlariId = '" + TxtBxOdaMesajlari.ClientID + "'";, true);
    

    在javascript中:

    var textBox = $get(m_TxtBxOdaMesajlariId);
    

    【讨论】:

    • 哦,先生,谢谢您,但这并没有改变任何东西。那是更新之前的,我现在正在检查更新。
    • @RoboLover:您可能只使用document.getElementById 而不是$get(我已在答案中修改)。如果失败,您还可以尝试我添加到答案中的代码隐藏方法。
    • 好吧,我将不得不尝试其他人暂时无法使用的代码隐藏方法。谢谢,非常感谢您的帮助。
    • 我尝试使用 IE9 的内置调试器调试网页,在 buttonClicked() 函数中设置了一个断点,但它甚至没有命中。先生,您认为这指向另一个问题吗?
    • 您也可以将ClientIDMode 设置为静态,但坚持使用 ClientID 可能更安全。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多