获取 "Specified argument was out of the range of valid values. Parameter name:"
Getting "Specified argument was out of the range of valid values. Parameter name:"
我知道好几篇文章中都有大量此类标题,但浏览了其中一些文章后,我没有找到对我的问题有任何帮助的信息。
我正在尝试在 Repeater 控件中构建一个下拉列表,该控件动态填充来自数据库的数据。
这是我使用的代码:
//标记
状态:
//代码文件
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconstring"].ToString());
string sSQL = "Select stateID, sateName from Mytable ORDER By stateName ASC";
SqlCommand cmd6 = new SqlCommand(sSQL, con);
con.Open();
SqlDataReader dtrClient = cmd6.ExecuteReader();
DropDownList ddl = (DropDownList)Repeater2.Controls[Repeater2.Controls.Count - 1].FindControl("ddlstate");
ddl.DataSource = dtrClient;
ddl.DataTextField = "stateName";
ddl.DataValueField = "stateID";
ddl.DataBind();
当我 运行 它时,我收到以下错误消息:
指定的参数超出有效值范围。
参数名称:索引
在以下行中:
DropDownList ddl = (DropDownList)Repeater2.Controls[Repeater2.Controls.Count - 1].FindControl("ddlstate");
有什么解决办法吗?
更新:
State: <asp:DropDownList ID="aircraftstate" runat="server" style="width:150px;" AppendDataBoundItems="True">
<asp:ListItem Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
//We query the DB only once in the Page Load
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
string sSQL = "Select StateID, StateName from MyTable ORDER By sName ASC";
SqlCommand cmd3 = new SqlCommand(sSQL, con);
con.Open();
table = new DataTable();
table.Load(cmd3.ExecuteReader());
//We load the DropDownList in the event
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var ddl = (DropDownList)e.Item.FindControl("aircraftstate");
ddl.DataSource = table;
ddl.DataTextField = "StateName";
ddl.DataValueField = "StateID";
ddl.DataBind();
由于 Repeater 是基于模板的控件,您应该 Find
您的 DropDownList 并在 ItemDataBound
事件中填充它。
所以你可以这样做:
DataTable table;
protected void Page_Load(object sender, EventArgs e)
{
//We query the DB only once in the Page Load
string sSQL = "Select stateID, sateName from Mytable ORDER By stateName ASC";
SqlCommand cmd6 = new SqlCommand(sSQL, con);
con.Open();
table = new DataTable();
table.Load(cmd6.ExecuteReader());
}
//We load the DropDownList in the event
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var ddl = (DropDownList)e.Item.FindControl("ddlID");
ddl.DataSource = table;
ddl.DataTextField = "stateName";
ddl.DataValueField = "stateID";
ddl.DataBind();
}
我知道好几篇文章中都有大量此类标题,但浏览了其中一些文章后,我没有找到对我的问题有任何帮助的信息。
我正在尝试在 Repeater 控件中构建一个下拉列表,该控件动态填充来自数据库的数据。
这是我使用的代码:
//标记
状态:
//代码文件
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconstring"].ToString());
string sSQL = "Select stateID, sateName from Mytable ORDER By stateName ASC";
SqlCommand cmd6 = new SqlCommand(sSQL, con);
con.Open();
SqlDataReader dtrClient = cmd6.ExecuteReader();
DropDownList ddl = (DropDownList)Repeater2.Controls[Repeater2.Controls.Count - 1].FindControl("ddlstate");
ddl.DataSource = dtrClient;
ddl.DataTextField = "stateName";
ddl.DataValueField = "stateID";
ddl.DataBind();
当我 运行 它时,我收到以下错误消息:
指定的参数超出有效值范围。 参数名称:索引
在以下行中:
DropDownList ddl = (DropDownList)Repeater2.Controls[Repeater2.Controls.Count - 1].FindControl("ddlstate");
有什么解决办法吗?
更新:
State: <asp:DropDownList ID="aircraftstate" runat="server" style="width:150px;" AppendDataBoundItems="True">
<asp:ListItem Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
//We query the DB only once in the Page Load
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ToString());
string sSQL = "Select StateID, StateName from MyTable ORDER By sName ASC";
SqlCommand cmd3 = new SqlCommand(sSQL, con);
con.Open();
table = new DataTable();
table.Load(cmd3.ExecuteReader());
//We load the DropDownList in the event
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var ddl = (DropDownList)e.Item.FindControl("aircraftstate");
ddl.DataSource = table;
ddl.DataTextField = "StateName";
ddl.DataValueField = "StateID";
ddl.DataBind();
由于 Repeater 是基于模板的控件,您应该 Find
您的 DropDownList 并在 ItemDataBound
事件中填充它。
所以你可以这样做:
DataTable table;
protected void Page_Load(object sender, EventArgs e)
{
//We query the DB only once in the Page Load
string sSQL = "Select stateID, sateName from Mytable ORDER By stateName ASC";
SqlCommand cmd6 = new SqlCommand(sSQL, con);
con.Open();
table = new DataTable();
table.Load(cmd6.ExecuteReader());
}
//We load the DropDownList in the event
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var ddl = (DropDownList)e.Item.FindControl("ddlID");
ddl.DataSource = table;
ddl.DataTextField = "stateName";
ddl.DataValueField = "stateID";
ddl.DataBind();
}