我似乎无法弄清楚为什么这不起作用...
我有两个下拉列表。第一个我绑定到DataView
第二个我想将其绑定到DataTable本身。
当我尝试使用block将第二个下拉列表绑定到DataView之外的数据表时。我没有填充记录...
不确定为什么会这样..有人有什么想法吗? 如果我移动代码以将第二个下拉列表绑定到DataView的using块内或该块之前,则可以正常工作。
为什么我不能在使用块的DataView之外使用DataTable?
下面是我的代码:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderBody" runat="Server">
<asp:DropDownList runat="server" ID="ddlEstNo1"></asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlEstNo2"></asp:DropDownList>
</asp:Content>
protected void bindDDLs(string vCat, int year, int month)
{
using (DataTable dt = getData(vCat, year, month))
{
// THIS CODE ALSO WORKS..
//ddlEstNo2.DataSource = dt;
//ddlEstNo2.DataTextField = "estNo";
//ddlEstNo2.DataValueField = "estNo";
//ddlEstNo2.DataBind();
//ddlEstNo2.Items.Insert(0, new ListItem("--Any--", ""));
//ddlEstNo2.Items.Remove("Summary");
using (DataView dv = dt.DefaultView)
{
// populate ddlEstNo1
ddlEstNo1.DataSource = dv;
ddlEstNo1.DataTextField = "estNo";
ddlEstNo1.DataValueField = "estNo";
ddlEstNo1.DataBind();
ddlEstNo1.Items.Insert(0, new ListItem("--Any--", ""));
ddlEstNo1.Items.Remove("Summary");
// THIS CODE ALSO WORKS..
//ddlEstNo2.DataSource = dt;
//ddlEstNo2.DataTextField = "estNo";
//ddlEstNo2.DataValueField = "estNo";
//ddlEstNo2.DataBind();
//ddlEstNo2.Items.Insert(0, new ListItem("--Any--", ""));
//ddlEstNo2.Items.Remove("Summary");
}
// populate ddlEstNo2 ==>>> doesnt work!
ddlEstNo2.DataSource = dt;
ddlEstNo2.DataTextField = "estNo";
ddlEstNo2.DataValueField = "estNo";
ddlEstNo2.DataBind();
ddlEstNo2.Items.Insert(0, new ListItem("--Any--", ""));
ddlEstNo2.Items.Remove("Summary");
}
}
答案 0 :(得分:0)
实际上您的第二个using语句以某种方式取消了第一个using语句的作用……所以您可以做的是
using (DataTable dt = getData(vCat, year, month))
using (DataView dv = dt.DefaultView)
{
// THIS CODE ALSO WORKS..
//ddlEstNo2.DataSource = dt;
//ddlEstNo2.DataTextField = "estNo";
//ddlEstNo2.DataValueField = "estNo";
//ddlEstNo2.DataBind();
//ddlEstNo2.Items.Insert(0, new ListItem("--Any--", ""));
//ddlEstNo2.Items.Remove("Summary");
// populate ddlEstNo1
ddlEstNo1.DataSource = dv;
ddlEstNo1.DataTextField = "estNo";
ddlEstNo1.DataValueField = "estNo";
ddlEstNo1.DataBind();
ddlEstNo1.Items.Insert(0, new ListItem("--Any--", ""));
ddlEstNo1.Items.Remove("Summary");
// THIS CODE ALSO WORKS..
//ddlEstNo2.DataSource = dt;
//ddlEstNo2.DataTextField = "estNo";
//ddlEstNo2.DataValueField = "estNo";
//ddlEstNo2.DataBind();
//ddlEstNo2.Items.Insert(0, new ListItem("--Any--", ""));
//ddlEstNo2.Items.Remove("Summary");
// populate ddlEstNo2 ==>>> doesnt work!
ddlEstNo2.DataSource = dt;
ddlEstNo2.DataTextField = "estNo";
ddlEstNo2.DataValueField = "estNo";
ddlEstNo2.DataBind();
ddlEstNo2.Items.Insert(0, new ListItem("--Any--", ""));
ddlEstNo2.Items.Remove("Summary");
}