由于项目需要,实现点击textbox下拉数据,数据格式为“姓名 学号”,选择一项数据后,textbox里赋值为姓名,另外的textbox赋值学号,还要记录当前行所选择的学生ID,作为更新或新建学生的标识,如下图所示
至于属性的含义和该控件的使用方法不细说了,网上很多,我另外一个帖子也记录了vs2005里面使用该控件的方法,只说我遇到的问题和解决方法
问题一:下拉数据选中后,会把下拉数据完全填充到该textbox中,但我需要的是把下拉数据分隔,填充到两个文本框内
解决:从网上搜到一个帖子,有用,在服务器端绑定下拉数据的时候用键值对
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetTrainingClass(string prefixText, int count, string contextKey)
{
try
{
string[] result = null;
DataSet ds =。。。。。//调用取数据的方法
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
result = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = ds.Tables[0].Rows[i];
//result[i] = dr["VU_realname"].ToString()+" |ID:"+dr["ID"];//最初的绑定方法
result[i] = "{'First':'" + dr["VU_realname"].ToString() + " " + dr["VU_loginname"].ToString() + "','Second':" + dr["ID"].ToString() + "}";//使用键值对的方法,键值对格式如下:{'First':'zhangqianqian zq','second':15} 字符串用单引号括起来,数字则不用,虽然网上说是跟json有关,介于我不了解json,不细说了,管用就好
}
}
return result;
}
catch
{
// TODO: logging...
return null;
}
}
仅仅使用上述键值对的方式返回数据并不能实现选中的数据后,文本框内可以只赋值姓名,还需要在js里面控制,js里面可以获取到选中数据的value 和text
<asp:TextBox ID="txtUserRealName1" runat="server" AutoPostBack="true"
autocomplete="off" ontextchanged="txtUserRealName_TextChanged"></asp:TextBox>
<ajaxtoolkit:AutoCompleteExtender ID="AceUserRealName1" runat="server" BehaviorID="AceUserRealName1" CompletionInterval="2000" EnableCaching="true" OnClientItemSelected="SetSelectedValue" UseContextKey="true" MinimumPrefixLength="0" ServiceMethod="GetUserInfoByRealName" TargetControlID="txtUserRealName1">
</ajaxtoolkit:AutoCompleteExtender>
<asp:HiddenField ID="hidUserId1" runat="server" Value="0" />
如上述红色字体,在选中下拉数据的时候出发的js方法
function SetSelectedValue(source, eventArgs) {
document.getElementById('hidUserId1').value = eventArgs.get_value();//value获取学生id
document.getElementById('txtUserRealName1').value = eventArgs.get_text().split(' ')[0];//由于我显示的text是用空格分隔 姓名和学号的,所以拆分后取第一个数据赋值到文本框
}
赋值问题解决了,另外的问题又来了,由于我页面中有多行数据,但是姓名的文本框我调用的是相同的方法获取下拉数据,可选中后赋值的文本框又不一定是固定的,所以需要知道targetcontrolid,就是说从哪个控件触发的,所以只要判断上述js方法的source就可以了,百度搜了很多帖子,没有用到source的,郁闷啊,google吧尽管是英文的,但也可以看懂,有个帖子和我说的情况一模一样的
I am using the 1.0.10920.32880 version of the ajaxcontroltoolkit.dll and added the OnClientItemSelected to an extender.
My script function to handle the OnClientItemSelected event is:
function myItemSelectedHandler(source,eventArgs)
{}
The source looks like its the autocompleteextender and the eventArgs is the value and text that was selected. I would like to be able to obtain the targetControlID for the extender in this handler but cannot find it in either the source or eventArgs parameters. Is there a way to obtain the ID of the targetControl in this handler. I have several autocomplete extenders and would like to be able to have just one handler to process several autocompleteExtenders
回答的:
The "source" argument is the AutoCompleteBehavior,
You can use the following script to get the target Control ID ,
source.get_element().id
But , a word of caution , by doing this you are creating a dependency on the
internal implementation of the AutoCompleteExtender and is not guaranteed to remain constant.
所以问题就解决了,
source.get_element().id 可以获取指向的文本框哦,哇咔咔
http://forums.asp.net/p/1164179/1932181.aspx