How to pass client selected text to the server

M

mark_hanson_eng

How can I best pass user selected text to the server on a button click?
I could use javascript document.selected and add javascript to a
asp:button using Attributes.Add. Ideally I would fill the button's
CommandArgument with the client selected text, but I don't know how.
What is the best way to get client side document.selected to the server
code? Please provide sample code, thanks
 
G

Guest

"use selected text" ???
Is this an asp:TextBox? If so the info is there, what you do with it at the
server is up to you.
 
M

mark_hanson_eng

Do you see what I am doing wrong here? If I uncomment the alert I
don't see the alert, so there is a problem in the javascript setting
the hidden value. Below is the html and the code behind. Thanks

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="Test16.aspx.vb" Inherits="wave.Test16"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Test16</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script>
function aaa()
{
Hidden1.value = document.selection();
//alert("hi");
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<P></P>
<asp:Label id="Label1" runat="server">Label1</asp:Label>
<P></P>
<input type="hidden" id="Hidden1" runat="server">
</form>
</body>
</HTML>



Public Class Test16
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Hidden1 As
System.Web.UI.HtmlControls.HtmlInputHidden

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Button1.Attributes.Add("onclick", "aaa()")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Label1.Text = Hidden1.Value
End Sub
End Class
 
E

Eliyahu Goldin

In a hidden input control. Populate it on client side and read on server
side.

Eliyahu
 
L

Lucas Tam

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
Hidden1.value = document.selection();

Since Hidden1 is a server side control (with a runAT) tag... you need to
write out the javascript dynamically... replacing Hidden1 with the
controlname . The controlname can be retrieved server side using
Hidden1.ClientID(). Or you can just hardcode the name ... view the HTML
generated and note the controlname.

Take a look at some articles on using Javascript with ASP.NET
 
M

mark_hanson_eng

This worked...

document.Form1.Hidden1.value = document.selection.createRange().text;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,330
Latest member
AlvaStingl

Latest Threads

Top