我正在开发一个ASP.NET应用程序,我有一个DropDownList,它没有使用SqlDataReader显示来自数据库的数据。下面是我的asp.net网络表单代码......
<p>
<span class="label">
<asp:Label ID="Label1" runat="server" Text="Document Number" Font-Size="Small" Font-Bold="True"></asp:Label>
</span>
<asp:TextBox ID="numb" runat="server" Width="176px" ReadOnly="true"></asp:TextBox>
<br />
<br />
<span class="label">
<asp:Label ID="Label2" runat="server" Text="Title" Font-Size="Small" Font-Bold="True"></asp:Label>
</span>
<asp:TextBox ID="TextBox1" runat="server" Width="176px"></asp:TextBox><br/>
<br />
<span class="label">
<asp:Label ID="Label3" runat="server" Text="Author" Font-Size="Small" Font-Bold="True"></asp:Label>
</span>
<asp:TextBox ID="TextBox2" runat="server" Width="176px"></asp:TextBox><br/>
<br />
<span class="label">
<asp:Label ID="Label4" runat="server" Text="Account Name" Font-Size="Small" Font-Bold="true"></asp:Label>
</span>
<asp:DropDownList ID="name" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged" AppendDataBoundItems="true">
<asp:ListItem Text="-SELECT-" Value="" />
</asp:DropDownList><br />
<br />
<span class="label">
<asp:Label ID="Label5" runat="server" Text="Account Code" Font-Size="Small" Font-Bold="true"></asp:Label>
</span>
<asp:DropDownList ID="code" runat="server" AutoPostBack="true"></asp:DropDownList><br />
<br />
<span class="label">
<asp:Label ID="Label6" runat="server" Text="Date" Font-Size="Small" Font-Bold="true"></asp:Label>
</span>
<asp:TextBox ID="date" runat="server" Width="176px" ReadOnly="true"></asp:TextBox><br />
<br />
<asp:Label ID="results" runat="server"></asp:Label>
</p>
以下是我的C#代码
if (!IsPostBack)
{
string CS = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using (SqlConnection conn = new SqlConnection(CS))
{
SqlCommand comm = new SqlCommand("SELECT Account_Name FROM Accounts", conn);
conn.Open();
name.DataSource = comm.ExecuteReader();
name.DataTextField = "Account_Name";
name.DataValueField = "Account_Name";
name.DataBind();
OnSelectedIndexChanged(sender, e);
}
date.Text = DateTime.Now.ToString("MM/dd/yyyy");
LoadDetails();
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
string account = "SELECT Account_Code FROM Accounts WHERE Account_Name = '" + name.Text + "'";
SqlCommand camd = new SqlCommand(account, con);
con.Open();
code.DataSource = camd.ExecuteReader();
code.DataTextField = "Account_Code";
code.DataValueField = "Account_Code";
code.DataBind();
con.Close();
}
private void LoadDetails()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
{
string docNo = Session["doc"].ToString();
string query = "SELECT * FROM [pubs].[dbo].[ReportDocumentNo] WHERE DocumentNo = @docNo;";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@docNo", docNo);
con.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
numb.Text = rdr["DocumentNo"].ToString();
TextBox1.Text = rdr["Title"].ToString();
TextBox2.Text = rdr["Author"].ToString();
name.Text = rdr["AccName"].ToString();
code.Text = rdr["AccCode"].ToString();
}
}
}
}
无论我运行哪个页面,我的所有文本框都由来自数据库的数据填充,但代码TextBox 除外。我错过了使代码文本框显示数据。谢谢
答案 0 :(得分:0)
<asp:DropDownList ID="ddlMealLocationQT" runat="server" OnSelectedIndexChanged="ddlMealLocationQT_SelectedIndexChanged"></asp:DropDownList>
protected void ddlMealLocationQT_SelectedIndexChanged(object sender, EventArgs e){
List<int> months = new List<int>(){1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
foreach (string month in months)
{
DropDownListMonth.Items.Add(month);
}
}
OR
您可以使用DataTextField和DataValueField属性。
ListControl.DataTextField Property
DropDownList1.DataSource = drpdt;
DropDownList1.DataTextField="StringValue";
DropDownList1.DataValueField="CurrencyValue";
DropDownList1.DataBind();
或者一次添加一个ListItem。
https://www.google.com/search?ie=UTF-8&ufsmps=1&oe=UTF-8&sourceid=navclient&gfns=1&q=ASP%20Tutorials