private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add("Item 1");
DropDownList1.Items.Add("Item 2");
DropDownList1.Items.Add("Item 3");
DropDownList1.Items.Add("Item 4");
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(TextBox1.Text));
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
<P>
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList></P>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
/// <summary>
/// Zusammenfassung f
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function setDropDownList()
{
for (i=0; i<document.forms[0].myDropDownList.children.length; i++)
{
if (document.forms[0].myDropDownList.children[i].innerText == document.forms[0].tbToSearch.value)
{
document.forms[0].myDropDownList.selectedIndex = i;
break;
}
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P><INPUT id="tbToSearch" type="text"><INPUT type="button" value="Button" onclick="setDropDownList();"></P>
<P><SELECT id="myDropDownList">
<OPTION selected>Item 1</OPTION>
<OPTION selected>Item 2</OPTION>
<OPTION selected>Item 3</OPTION>
<OPTION selected>Item 4</OPTION>
<OPTION selected>Item 5</OPTION>
</SELECT>
</P>
</form>
</body>
</HTML>