到目前为止,一切都没有错误地加载,一旦一切正常,SQL调用将被更安全的存储过程替换.当我从下拉菜单中选择一个炉子时,它应该传递给方法并用运行编号填充文本框.但是当我选择任何东西时,它只会回到第一个指数10A.我还编写了索引0应该说“Select Furnace”,但也没有显示,只有第一个索引.是否有关于如何获取所选索引并从SQL查询填充文本框的建议?
<asp:Content ID="BodyContent" ContentPlaceHolderID="BodyPlaceHolder" runat="server">
<center>
<table style="text-align:left">
<tr>
<td align="right">
<asp:Label ID="FurnaceID" Text="JUMP TO FURNACE : " runat="server" />
</td>
<td></td>
<td>
<asp:DropDownList ID="FurnID" runat="server" AutoPostBack ="true"
onselectedindexchanged="FurnID_SelectedIndexChanged" Width="150px">
<asp:ListItem Text="Select Furnace" Value = "0" />
</asp:DropDownList>
守则背后:
LoadList();在page_load事件中调用
public void LoadList()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM [Furnace]", new SqlConnection(ConfigurationManager.ConnectionStrings["FurnaceDeckConnectionString"].ConnectionString));
cmd.Connection.Open();
SqlDataReader Furns;
Furns = cmd.ExecuteReader();
FurnID.DataSource = Furns;
FurnID.DataValueField = "Furnaceno";
FurnID.DataTextField = "Furnaceno";
FurnID.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
更改索引时,将调用此方法:
protected void FurnID_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
String selectedFurn = "";
ListItem selectFurn = FurnID.Items.FindByText(selectedFurn);
LoadFurnDetails(selectedFurn);
}
}
public void LoadFurnDetails(String F)
{
String selectF = F;
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("SELECT * FROM [Furnace Run Data Table] Where Furnace = 'selectF' and Completed = 0 ", new SqlConnection(ConfigurationManager.ConnectionStrings["FurnaceDeckConnectionString"].ConnectionString));
cmd.Connection.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
lblFurnId.Text = dt.Rows[0]["runno"].ToString();
}
cmd.Connection.Close();
cmd.Connection.Dispose();
}
最佳答案 这部分
if (IsPostBack)
{
String selectedFurn = "";
ListItem selectFurn = FurnID.Items.FindByText(selectedFurn);
LoadFurnDetails(selectedFurn);
}
…将字符串“selectedFurn”定义为空字符串.
然后,您尝试使用此空字符串按文本查找项目.我假设,这个项目不存在……无论如何,你不使用找到的ListItem,但只是传递空字符串…
您最有兴趣对当前所选项目和此特殊项目的加载详细信息感兴趣,不是吗?尝试使用.Selecteditemrather查找ListItem,而不是在LoadFurnDetails中将空字符串作为参数传递…
下一个问题是:
SqlCommand cmd = new SqlCommand("SELECT * FROM [Furnace Run Data Table] Where Furnace = 'selectF' and Completed = 0 " ...
您认为传入变量“selectF”但实际上正在搜索硬编码的“selectF”…您必须将“selectF”替换为变量的实际内容.
再次禁止:你应该总是使用参数,不要连接字符串命令!