展开全部
代码给你了 剩下的就交个你了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace Common_Core.CustomControl
{
[ToolboxData("<{0}:DropDownTextBox runat=\"server\" />")]
public class DropDownTextBox : System.Web.UI.WebControls.TextBox
{
private DropDownList _DropDownList;
public DropDownTextBox()
{
this._DropDownList = new DropDownList();
this.DataTextField = "sName";
this.DataValueField = "PK_ID";
}
/// <summary>
/// text
/// </summary>
public string DataTextField { get; set; }
/// <summary>
/// value
/// </summary>
public string DataValueField { get; set; }
/// <summary>
/// datasource
/// </summary>
public object DataSource { get; set; }
/// <summary>
/// bind data
/// </summary>
private void BindData()
{
if (DataSource == null) return;
_DropDownList.DataSource = this.DataSource;
_DropDownList.DataTextField = this.DataTextField;
_DropDownList.DataValueField = this.DataValueField;
_DropDownList.DataBind();
}
protected override void Render(HtmlTextWriter output)
{
int iWidth = Convert.ToInt32(base.Width.Value);
if (iWidth == 0)
{
iWidth = 102;
base.Width = Unit.Parse("102px");
}
int sWidth = iWidth + 16;
int spanWidth = sWidth - 18;
output.Write("<div style=\"POSITION:relative\">");
output.Write("<span style=\"MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px\">");
_DropDownList.Width = Unit.Parse(sWidth.ToString() + "px");
_DropDownList.Style.Add("MARGIN-LEFT", "-" + spanWidth.ToString() + "px");
_DropDownList.ID = base.ID + "_Select";
_DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.options[this.selectedIndex].text");
_DropDownList.Attributes.Add("onfocus", "" + this.getFocusScript() + "");
//LoadData();
BindData();
_DropDownList.RenderControl(output);
output.Write("</span>");
base.Style.Clear();
base.Width = Unit.Parse(iWidth.ToString() + "px");
base.Style.Add("left", "0px");
base.Style.Add("POSITION", "absolute");
base.Render(output);
output.Write("</div>");
}
private string getFocusScript()
{
string strScript = "\n";
strScript += "var isExist = -2;\n";
strScript += "var obj = event.srcElement;\n";
strScript += "var str = this.parentNode.nextSibling.value;\n";
strScript += "var ary = obj.options;\n";
strScript += "for(var i=0;i<ary.length;i++){\n";
strScript += " if(str == ary[i].text){\n";
strScript += " isExist = i;\n";
strScript += " break;\n";
strScript += " }\n";
strScript += "}\n";
strScript += "if(isExist != -2){\n";
strScript += " obj.selectedIndex = isExist;\n";
strScript += "}\n";
strScript += "else{\n";
strScript += " obj.selectedIndex = -1;\n";
strScript += "}\n";
return strScript;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace Common_Core.CustomControl
{
[ToolboxData("<{0}:DropDownTextBox runat=\"server\" />")]
public class DropDownTextBox : System.Web.UI.WebControls.TextBox
{
private DropDownList _DropDownList;
public DropDownTextBox()
{
this._DropDownList = new DropDownList();
this.DataTextField = "sName";
this.DataValueField = "PK_ID";
}
/// <summary>
/// text
/// </summary>
public string DataTextField { get; set; }
/// <summary>
/// value
/// </summary>
public string DataValueField { get; set; }
/// <summary>
/// datasource
/// </summary>
public object DataSource { get; set; }
/// <summary>
/// bind data
/// </summary>
private void BindData()
{
if (DataSource == null) return;
_DropDownList.DataSource = this.DataSource;
_DropDownList.DataTextField = this.DataTextField;
_DropDownList.DataValueField = this.DataValueField;
_DropDownList.DataBind();
}
protected override void Render(HtmlTextWriter output)
{
int iWidth = Convert.ToInt32(base.Width.Value);
if (iWidth == 0)
{
iWidth = 102;
base.Width = Unit.Parse("102px");
}
int sWidth = iWidth + 16;
int spanWidth = sWidth - 18;
output.Write("<div style=\"POSITION:relative\">");
output.Write("<span style=\"MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px\">");
_DropDownList.Width = Unit.Parse(sWidth.ToString() + "px");
_DropDownList.Style.Add("MARGIN-LEFT", "-" + spanWidth.ToString() + "px");
_DropDownList.ID = base.ID + "_Select";
_DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.options[this.selectedIndex].text");
_DropDownList.Attributes.Add("onfocus", "" + this.getFocusScript() + "");
//LoadData();
BindData();
_DropDownList.RenderControl(output);
output.Write("</span>");
base.Style.Clear();
base.Width = Unit.Parse(iWidth.ToString() + "px");
base.Style.Add("left", "0px");
base.Style.Add("POSITION", "absolute");
base.Render(output);
output.Write("</div>");
}
private string getFocusScript()
{
string strScript = "\n";
strScript += "var isExist = -2;\n";
strScript += "var obj = event.srcElement;\n";
strScript += "var str = this.parentNode.nextSibling.value;\n";
strScript += "var ary = obj.options;\n";
strScript += "for(var i=0;i<ary.length;i++){\n";
strScript += " if(str == ary[i].text){\n";
strScript += " isExist = i;\n";
strScript += " break;\n";
strScript += " }\n";
strScript += "}\n";
strScript += "if(isExist != -2){\n";
strScript += " obj.selectedIndex = isExist;\n";
strScript += "}\n";
strScript += "else{\n";
strScript += " obj.selectedIndex = -1;\n";
strScript += "}\n";
return strScript;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询