<script runat="server">
void Page_Load()
{
if (!(Page.IsPostBack)) {
object myText = new Hashtable();
myText.Add("1", "Joe");
myText.Add("2", "John");
myText.Add("3", "James");
myText.Add("4", "Tom");
DropDownList1.DataSource = myText;
DropDownList1.DataTextField = "Value";
DropDownList1.DataValueField = "Key";
DropDownList1.DataBind();
}
}
sub displayName(s as Object,e As EventArgs)
label1.text="Your Name: " & DropDownList1.SelectedItem.Text
end sub
</script>
<html>
<body>
<form runat="server">
<asp:DropDownList id="DropDownList1" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayName" />
<p><asp:label id="label1" runat="server" /></p>
</form>
</body>
</html>
"johnson" 來函:
> 請教一下:
> 如題;像我有一個DataReader,要將值連結到combox的valuemember 及displymembe,要如何做呢?
> 感謝回答。
private void Form1_Load(object sender, System.EventArgs e)
{
SqlCommand cmd= new SqlCommand("select top 10 productid,productname from
products",sqlConnection1);
sqlConnection1.Open();
try
{
SqlDataReader drProduct= cmd.ExecuteReader();
ArrayList Product = new ArrayList();
while(drProduct.Read())
{
Product.Add(new AddValue(drProduct.GetInt32(0),drProduct.GetString(1)));
}
drProduct.Close();
drProduct = null;
sqlConnection1.Close();
sqlConnection1.Dispose();
sqlConnection1= null;
this.comboBox1.DataSource= Product;
this.comboBox1.ValueMember = "Value";
this.comboBox1.DisplayMember = "Display";
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public class AddValue
{
private string mDisplay;
private int mValue;
public int Value
{
get
{
return mValue;
}
}
public string Display
{
get
{
return mDisplay;
}
}
public AddValue(int Value,string Display)
{
mValue = Value;
mDisplay = Display;
"johnson" <zhang...@hotmail.com.NoSpam> 撰寫於郵件新聞:5092D03C-708C-46FB...@microsoft.com...