0

I'm just learning Asp.Net & VB so please be gentle :-) The code was written for me which displays a message thread. It shows the messages from oldest at the top to recently received at the bottom where the reply box is. Is there any way that I can add to this code to make it automatically scroll down to the bottom of the thread when the thread is opened? Many thanks for any help...

        <div style="width:78%; float:right; overflow:auto; height:500px;" id="messagesWindow">
            <asp:SqlDataSource ID="DSSelectMessages" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
                SelectCommand="SelectMessages" SelectCommandType="StoredProcedure">
                <SelectParameters>
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:Literal ID="Literal1" runat="server"></asp:Literal>

            <asp:HiddenField ID="hdnButtonFrom" runat="server" />
            <div style="font-size:1.3em">
                <asp:GridView ID="gdvMessages" runat="server" CssClass="mGrid" AutoGenerateColumns="False" DataKeyNames="messageID" ShowHeader="False" ShowFooter="True">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Label ID="lblContactFrom" runat="server" Text='<%# Eval("contactFrom") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Label ID="lblDateSent" runat="server"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Label ID="lblItem" runat="server" Text='<%# Eval("item") %>'></asp:Label>
                                <asp:HyperLink ID="hypItem" Visible="false" runat="server" CssClass="nyroModalMsg" NavigateUrl='<%# "~/Account/itemSold.aspx?br=messageList&fileID=" & Eval("fileID") %>'>Mark as Sold</asp:HyperLink>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False" SortExpression="messageText">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("messageText") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <FooterTemplate>
                                <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Rows="5" Columns="32" MaxLength="2000"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="<br />Add Reply" CssClass="error" ValidationGroup="msgSubmit" ControlToValidate="txtMessage"></asp:RequiredFieldValidator>
                                <br />
                                <asp:Button ID="btnSend" runat="server" Text="Reply" onclick="btnSend_Click" ValidationGroup="msgSubmit" />
                            </FooterTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("messageText") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>            
            </div>
        </div>
Eggybread
  • 161
  • 1
  • 12

1 Answers1

0

Try

Textbox1.SelectionStart = Textbox1.Text.Length - 1  
Textbox1.ScrollToCaret()
soohoonigan
  • 2,342
  • 2
  • 10
  • 18
  • This answer came up in the Low Quality Posts queue because of its length and lack of text other than code. It is almost always better to provide an explanation of how the code helps the user. See [answer]. – Heretic Monkey Sep 29 '16 at 00:17
  • @soohoonigan Hi, thanks. Do I just put this in the VB code as you have it? – Eggybread Sep 29 '16 at 10:03
  • Yes, that should work. From the code you posted, it looks like your messageText is going into Textbox1 so you should be able to basically copy/paste it in, you would want to add the code above in your code-behind immediately after you fill the textbox. Once the textbox has been filled, the code above will place the cursor at the end of the text and then scroll the textbox to it – soohoonigan Sep 29 '16 at 12:43
  • @soohoonigan Thanks again. I'm getting error BC30456: 'SelectionStart' is not a member of 'System.Web.UI.WebControls.TextBox'. – Eggybread Sep 29 '16 at 17:55
  • Oh, my apologies...I was thinking of the system.windows.forms textboxes, not the web controls. Give this [answer](http://stackoverflow.com/a/1388170/6664878) a try – soohoonigan Sep 29 '16 at 18:14
  • @soohoonigan Thank you. I'm a bit out of my depth with it all now but cheers :-) I tried the javascript answer on that page but it didn't work. I'm also using VB so unsure what code to put where. Don't worry about it.. I really appreciate you trying to help.. I'm just not that clever yet. – Eggybread Sep 29 '16 at 19:11