第1步:http://www.nnllok.cn/Read.aspx?id=466
第2步:http://www.nnllok.cn/Read.aspx?id=467
again:http://www.nnllok.cn/Read.aspx?id=468
第3步:http://www.nnllok.cn/Read.aspx?id=469
第4步:http://www.nnllok.cn/Read.aspx?id=470
------------------------------------------------------
第3步得到select的用户控件,现在,我取到input的用户控件了
现在还缺的问题是:
1.包含select的div,我要根据input在window里的(x,y)跑,这个问题,难点只在谷歌和firefox的兼容。。。如果从一开始谷歌和firefox支持activeElement,我会简单好多啊,最少少,我不用给所有控件添加onfocus事件,记录activeObjId
2.当两个input互相切换焦点时,select里的所有项,要和新得到焦点的input里的值符合,现在新得到焦点的input如果value="",select还是保持和原来的相同的
3.可能会出现的bug:如果页面上有别的控件也需要用到onfocus事件,我的脚本会把原来的onfocus事件覆盖,添加事件的时候,要换成addLoadEvent一样子的方法
记录input的用户控件
PhonicsInput.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PhonicsInput.ascx.cs" Inherits="PhonicsInput" %>
<asp:Literal ID="LtDeclareArray" runat="server"></asp:Literal>
<asp:TextBox ID="TbWord" runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:HiddenField ID="HfWord" runat="server" Value="0" />
<asp:Literal ID="LtAssignment" runat="server"></asp:Literal>
PhonicsInput.ascx.cs
using System;
using System.Web.UI.WebControls;
using System.Web.UI;
public partial class PhonicsInput : System.Web.UI.UserControl
{
/// <summary>
/// 显示的文本(Example:球队名称TeamName)
/// </summary>
public string Text
{
set { TbWord.Text = value; }
get { return TbWord.Text; }
}
/// <summary>
/// 文本对应的编号(Example:球队编号TeamId)
/// </summary>
public string Value
{
set { HfWord.Value = value; }
get { return HfWord.Value; }
}
#region 设置Js脚本
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetPhonicsJs();
}
}
private void SetPhonicsJs()
{
int inputIndex = -1;
foreach (Control cc in Parent.Controls)
{
if (cc is PhonicsInput)
{
inputIndex++;
if (cc.ID != this.ID) continue;
if (inputIndex == 0)
{
//页面中的第一个input控件,声明数组
LtDeclareArray.Text = "<script type=\"text/javascript\">var wordInputArray = new Array(); var wordValueArray = new Array();</script>";
}
//赋值
LtAssignment.Text = String.Format("<script type=\"text/javascript\">wordInputArray[{0}] = document.getElementById(\"{1}\"); wordValueArray[{0}] = document.getElementById(\"{2}\");</script>", inputIndex.ToString(), TbWord.ClientID, HfWord.ClientID);
}
}
}
#endregion
}
调用现在好简单了:
<%@ Register Src="PhonicsInput.ascx" TagName="PhonicsInput" TagPrefix="uc2" %>
<%@ Register Src="PhonicsSelect.ascx" TagName="PhonicsSelect" TagPrefix="uc1" %>
主队:<uc2:PhonicsInput ID="PhonicsInputHome" runat="server" />
客队:<uc2:PhonicsInput ID="PhonicsInputAway" runat="server" />
<asp:Button ID="Button2" runat="server" Text="获取选中的值" OnClick="Button2_Click" />
<uc1:PhonicsSelect ID="PhonicsSelect1" runat="server" />
取值:
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("<br>" + PhonicsInputHome.Value + "," + PhonicsInputHome.Text);
Response.Write("<br>" + PhonicsInputAway.Value + "," + PhonicsInputAway.Text);
}