您可以使用客戶端事件嗎? 你可以看一看JQuery的插入符插件: http://www.examplet.buss.hk/jquery/caret.php
// Get start position in textbox box with id="textbox1"
$("#textbox1").caret().start
// Get end position in textbox
$("#textbox1").caret().end
// Get selected text in textbox
$("#textbox1").caret().text
也有簡單的JavaScript解決方案:
<script language=javascript>
function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
document.aform.selectedtext.value = txt;
}
</script>
<form>
<input type="button" value="Get selection" onmousedown="getSelText()">
<form name=aform >
<textarea name="selectedtext" rows="5" cols="20"></textarea>
</form>
來源:http://www.codetoad.com/javascript_get_selected_text.asp
其實我需要在代碼變化背後並不能使用JavaScript。 – user2091061 2013-02-26 09:40:43
你可以使用textBox1.SelectionStart和textBox1.SelectionLength嗎? 您可以使用這些與LostFocus事件臨時存儲的值 – 2013-02-26 10:02:36
是的..我已經存儲選定的文本,但問題是在文本中進行更改,更改不反映在DataGrid中。 – user2091061 2013-02-26 10:14:49