1

I have a repeater and inside of it is a textbox that contains my data from the database. Of course at run-time it will generate lots of data meaning lots of textboxes too.

<asp:Repeater ID="rpter" runat="server">
  <ItemTemplate>
    <fieldset>
      <legend>
        <asp:Label ID="lblLegend" runat="server" Text='<%#DataBinder.Eval(Container,"DataItem.Name")%>' Font-Bold="true" />
      </legend>
      <div style="border: single, 1px;">
        <div>
          <asp:TextBox ID="txtMessage" runat="server" Text='<%#DataBinder.Eval(Container,"DataItem.Message")%>' Width="100%" TextMode="MultiLine"></asp:TextBox>
        </div>
       <div>
         <asp:Button ID="btnCopy" runat="server" Text="Copy" CommandName="Copy" OnClientClick="copyClipboard()" />
         </div>
      </div>
    </fieldset>
  </ItemTemplate>
</asp:Repeater>

What I want is to select the text from the textbox besides the copy button when I clicked the button so that I can copy it on clipboard..

function copyClipboard() {
    CopiedTxt = document.selection.createRange();
    CopiedTxt.execCommand("Copy");
}
j.f.
  • 3,908
  • 2
  • 29
  • 42
Chinx
  • 15
  • 1
  • 7
  • Please can you post the relevant html generated by the repeater, otherwise we only have half the information to work with. – Rhumborl Sep 24 '14 at 19:12
  • I couldnt add my code on my question above. How Can I add it? – Chinx Sep 24 '14 at 19:20
  • Just copy code snippets from your Visual Studio solution and paste it in your question, i. e. "edit" your question... – Hauns TM Sep 24 '14 at 19:23
  • Be sure to read this question and answer: [how-to-copy-to-the-clipboard-in-javascript](http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript). – j.f. Sep 24 '14 at 21:18

3 Answers3

0

you need to pass this to your function copyClipboard. so your button code will look like this

<asp:Button ID="btnCopy" runat="server" Text="Copy" OnClientClick="copyClipboard(this)"/>

and a class to your div wrapping your textbox,

<div class="texdiv">
  <asp:TextBox ID="txtMessage" runat="server" Text='<%#DataBinder.Eval(Container,"DataItem.Message")%>' Width="100%" TextMode="MultiLine"></asp:TextBox>
</div>

and your js function would be

function copyClipboard(th) {
            alert($(th).parents().prevAll('.texdiv').find('*[id^=rpter_txtMessage]').val());
        }

on client click of button, you need to find it's previous textbox then you can get it's value it will give you corresponding textbox value

Amit Kumar
  • 5,888
  • 11
  • 47
  • 85
  • I tried it but still the same. Actually what I really want to do is to click the button and the text from the textbox will be selected. – Chinx Sep 25 '14 at 14:40
0
$(function () {
    $(".copybtn").click (function (e) {
        Var txtMsgVal = $(this).parent().parent().find(".txtMsg").text();
        // Do something with the txtMsgVal here
        alert(txtMsgVal);
    });
});

You just need a class attribute on both the button and the input (the message).

Note: I have used jQuery in this answer so you have to include the jQuery library in your source code.

Good luck ;)

MosabJ
  • 80
  • 1
  • 9
0

I have to edit your ASP code and add CssClass="txtMessage" to the TextBox.
Also CssClass="btnCopy" to the Button
And removed the OnClientClick="copyClipboard()" because we will handle that in our jQuery as below:

ASP:-

<asp:Repeater ID="rpter" runat="server">
    <ItemTemplate>
    <fieldset>
        <legend>
        <asp:Label ID="lblLegend" runat="server" Text='<%#DataBinder.Eval(Container,"DataItem.Name")%>' Font-Bold="true" />
        </legend>
        <div style="border: single, 1px;">
            <div>
                <asp:TextBox ID="txtMessage" runat="server" CssClass="txtMessage" Text='<%#DataBinder.Eval(Container,"DataItem.Message")%>' Width="100%" TextMode="MultiLine"></asp:TextBox>
            </div>
            <div>
                <asp:Button ID="btnCopy" runat="server" CssClass="btnCopy" Text="Copy" CommandName="Copy" />
            </div>
        </div>
    </fieldset>
    </ItemTemplate>
</asp:Repeater>

Don't forget to include jQuery library:

<script type="text/javascript" src="jquery-1.10.2.min.js"></script>

And the jQuery will handle all clicks to any item with class of btnCopy and look for .txtMessage item inside the parent of the parent of the button
And select the text inside that TextBox
And return false so your click won't take you any where...

jQuery:-

$(function () {
    $(".btnCopy").click(function (e) {
        $(this).parent().parent().find(".txtMessage").select();
        return false;
    });
});

I wish that will work for you and this is the screenshot:
http://i59.tinypic.com/5ujvc9.jpg
http://i62.tinypic.com/35d4y7l.jpg

Good luck ;)

MosabJ
  • 80
  • 1
  • 9
  • What do you get: Page refresh. Or what have you included the jquery library. Are the onclientclick there, remove it. Do you want me to post a screenshot with the full code. – MosabJ Sep 25 '14 at 15:49
  • Can you send me the screenshot on my email? I couldnt open it. – Chinx Sep 25 '14 at 18:20