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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

分类:
NET
, JavaScript
标签:
小Q点滴
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端