ASP.NET 中在指定的位置处插入字符
这是论坛中的一个问题,很多人经常会遇到,就贴出来。
完整代码如下,实现在光标处插入上传后的文件名:
完整代码如下,实现在光标处插入上传后的文件名:
1<%@ Page Language="C#" AutoEventWireup="true" %>
2<script runat="server">
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 TextBox1.Attributes.Add("onclick", "getCursor('" + TextBox1.ClientID + "','" + Hidden1.ClientID + "');");
6 }
7 protected void Button1_Click(object sender, EventArgs e)
8 {
9 string fileName = FileUpload1.FileName;
10 //保存文件省略
11 int pos = 0;
12 Int32.TryParse(Hidden1.Value, out pos);
13 TextBox1.Text = TextBox1.Text.Insert(pos, fileName);
14 }
15</script>
16<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
17<html xmlns="http://www.w3.org/1999/xhtml">
18<head runat="server">
19<title>ASP.NET 中在指定的位置处插入字符 </title>
20<script type="text/javascript">
21 function getCursor(param1,param2)
22 {
23 var pos = 0;
24 var t = document.getElementById(param1);
25 if(document.selection )
26 {
27 t.focus();
28 var rng = document.selection.createRange();
29 rng.setEndPoint("EndToStart", t.createTextRange());
30 pos = rng.text.length;
31 }
32 else if(t.selectionStart)
33 {
34 pos = t.selectionStart;
35 }
36 document.getElementById(param2).value = pos;
37 }
38</script>
39
40</head>
41<body>
42<form id="form1" runat="server">
43<div>
44 <asp:TextBox ID="TextBox1" runat="server" Width="629px">123456789 </asp:TextBox>
45 <input id="Hidden1" type="hidden" runat="server" />
46</div>
47<asp:FileUpload ID="FileUpload1" runat="server" />
48<asp:Button ID="Button1" runat="server" Text="上载文件" OnClick="Button1_Click" />
49</form>
50</body>
51</html>
转自:http://blog.csdn.net/net_lover/archive/2008/05/25/2480585.aspx
2<script runat="server">
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 TextBox1.Attributes.Add("onclick", "getCursor('" + TextBox1.ClientID + "','" + Hidden1.ClientID + "');");
6 }
7 protected void Button1_Click(object sender, EventArgs e)
8 {
9 string fileName = FileUpload1.FileName;
10 //保存文件省略
11 int pos = 0;
12 Int32.TryParse(Hidden1.Value, out pos);
13 TextBox1.Text = TextBox1.Text.Insert(pos, fileName);
14 }
15</script>
16<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
17<html xmlns="http://www.w3.org/1999/xhtml">
18<head runat="server">
19<title>ASP.NET 中在指定的位置处插入字符 </title>
20<script type="text/javascript">
21 function getCursor(param1,param2)
22 {
23 var pos = 0;
24 var t = document.getElementById(param1);
25 if(document.selection )
26 {
27 t.focus();
28 var rng = document.selection.createRange();
29 rng.setEndPoint("EndToStart", t.createTextRange());
30 pos = rng.text.length;
31 }
32 else if(t.selectionStart)
33 {
34 pos = t.selectionStart;
35 }
36 document.getElementById(param2).value = pos;
37 }
38</script>
39
40</head>
41<body>
42<form id="form1" runat="server">
43<div>
44 <asp:TextBox ID="TextBox1" runat="server" Width="629px">123456789 </asp:TextBox>
45 <input id="Hidden1" type="hidden" runat="server" />
46</div>
47<asp:FileUpload ID="FileUpload1" runat="server" />
48<asp:Button ID="Button1" runat="server" Text="上载文件" OnClick="Button1_Click" />
49</form>
50</body>
51</html>