@@ -4,7 +4,7 @@ description: Learn how to allow users to copy text from the ItemTemplate in the
44type : how-to
55page_title : How to Enable Text Copy in AutoComplete Dropdown Blazor
66slug : autocomplete-kb-copy-text-itemtemplate
7- tags : autocomplete, blazor , itemtemplate, clipboard, text , copy, paste
7+ tags : blazor, autocomplete , itemtemplate, clipboard, copy, paste
88res_type : kb
99ticketid : 1689288
1010---
@@ -35,8 +35,11 @@ To enable text copying from the dropdown list in the AutoComplete, use the `Item
3535@inject IJSRuntime JS
3636
3737<style>
38- .copy-button {
39- margin-left: 8px;
38+ .autocomplete-item {
39+ display: flex;
40+ align-items: center;
41+ justify-content: space-between;
42+ gap: 8px;
4043 }
4144</style>
4245
@@ -45,17 +48,22 @@ To enable text copying from the dropdown list in the AutoComplete, use the `Item
4548 @{
4649 var text = context.ToString();
4750 }
48- <div style="display: flex; align-items: center; justify-content: space-between; ">
51+ <div class="autocomplete-item ">
4952 <strong>@text</strong>
50- <TelerikButton OnClick="@(() => CopyToClipboard(text))" Class="copy-button" Icon="@SvgIcon.Copy" FillMode="@ThemeConstants.Button.FillMode.Outline" />
53+ <TelerikButton OnClick="@(() => CopyToClipboard(text))"
54+ Icon="@SvgIcon.Copy"
55+ FillMode="@ThemeConstants.Button.FillMode.Outline" />
5156 </div>
5257 </ItemTemplate>
5358</TelerikAutoComplete>
5459
5560@code {
5661 private string Role { get; set; }
5762 private List<string> AutoCompleteData { get; set; }
58- private List<string> SourceData { get; set; } = new List<string> { "Manager", "Developer", "QA", "Technical Writer", "Support Engineer", "Sales Agent", "Architect", "Designer" };
63+ private List<string> SourceData { get; set; } = new()
64+ {
65+ "Manager", "Developer", "QA", "Technical Writer", "Support Engineer", "Sales Agent", "Architect", "Designer"
66+ };
5967
6068 protected override void OnInitialized()
6169 {
@@ -69,7 +77,24 @@ To enable text copying from the dropdown list in the AutoComplete, use the `Item
6977
7078 private async Task CopyToClipboard(string text)
7179 {
72- await JS.InvokeVoidAsync("navigator.clipboard.writeText", text);
80+ try
81+ {
82+ var isSupported = await JS.InvokeAsync<bool>("eval", "!!(navigator.clipboard && navigator.clipboard.writeText)");
83+ if (isSupported)
84+ {
85+ await JS.InvokeVoidAsync("navigator.clipboard.writeText", text);
86+ }
87+ else
88+ {
89+ Console.WriteLine("Clipboard API not supported in this browser.");
90+ // Optionally show a notification to the user
91+ }
92+ }
93+ catch (Exception ex)
94+ {
95+ Console.Error.WriteLine($"Clipboard copy failed: {ex.Message}");
96+ // Optionally show a fallback UI message to the user
97+ }
7398 }
7499}
75100````
0 commit comments