最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - JavaScript method not working in ASP.NET User Control - Stack Overflow

programmeradmin0浏览0评论

I asked a question before about a JavaScript code you can see it here : How can I scroll down to a multiline TextBox's bottom line, Javascript's scrollIntoView is not working for this. Well, it was solved and the accepted answer was working.

Afterwards I had to move the code part you can see in that question to a User Control which finally looked like 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>

And in asp.cs file I have

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

My problem is that buttonClicked() function is not working anymore, I tried to use Page.ClientScriptManager.RegisterClientScriptInclude from ScottS's answer and also this Page.ClientScript.RegisterClientScriptBlock, neither of them worked.What can I do?

EDIT: I tried to debug the webpage with IE9's built in debugger and put a breakpoint in the buttonClicked() function but it was never hit.Then I used the OnClientClick event of the button and added buttonClicked function to it it works for each click but then textbox just jumps back to where it was before buttonClicked() function.

I asked a question before about a JavaScript code you can see it here : How can I scroll down to a multiline TextBox's bottom line, Javascript's scrollIntoView is not working for this. Well, it was solved and the accepted answer was working.

Afterwards I had to move the code part you can see in that question to a User Control which finally looked like 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>

And in asp.cs file I have

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

My problem is that buttonClicked() function is not working anymore, I tried to use Page.ClientScriptManager.RegisterClientScriptInclude from ScottS's answer and also this Page.ClientScript.RegisterClientScriptBlock, neither of them worked.What can I do?

EDIT: I tried to debug the webpage with IE9's built in debugger and put a breakpoint in the buttonClicked() function but it was never hit.Then I used the OnClientClick event of the button and added buttonClicked function to it it works for each click but then textbox just jumps back to where it was before buttonClicked() function.

Share Improve this question edited May 23, 2017 at 10:24 CommunityBot 11 silver badge asked Jan 12, 2012 at 19:38 BastardoBastardo 4,1529 gold badges43 silver badges60 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The issue is that ids for controls on a user control are somewhat mangled in that they include the user control's id plus the actual control's id. So your control's id probably looks something like: ucLiveChatPart_TxtBxOdaMesajlari.

The easy way to handle this is to change this line:

var textBox = $get("TxtBxOdaMesajlari");

to:

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

Now, if you extract your javascript into a separate file or you want to use a different approach, you can emit the control id into a javascript variable from codebehind and use that instead.

In codebehind:

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

and in javascript:

var textBox = $get(m_TxtBxOdaMesajlariId);
发布评论

评论列表(0)

  1. 暂无评论