【问题标题】:Getting "Specified argument was out of the range of valid values. Parameter name:"获取“指定的参数超出了有效值的范围。参数名称:”
【发布时间】:2017-07-24 23:07:26
【问题描述】:

我知道在几篇文章中有大量这个标题,但浏览了其中一些,我没有找到任何解决我的问题的帮助。

我正在尝试在 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();

【问题讨论】:

  • Repeater2.Controls.Count 0 是偶然的吗?您可能正在尝试做Repeater2.Controls[-1]。您可能应该检查一个空的结果集并适当地处理它。
  • 你检查过我的回答@Kenny吗?
  • @kbok。抱歉迟了回应。我有紧急情况,我现在要检查一下。不过看起来棒极了。非常感谢。我一直在为这个中继器下拉列表而苦苦挣扎。很快就会回来。
  • @kblok,请查看上面更新的代码。没有错误总是好的,但下拉列表没有被填充。我错过了什么吗?
  • @kblok,好的,它正在工作。做得好!我在标记上缺少 repeater_ItemDataBound 。我想我的问题是,如果我有两个下拉列表,我是否将两者都添加到 repeater_ItemDataBound 事件中?

标签: c# asp.net repeater


【解决方案1】:

由于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();

}

【讨论】:

  • 非常感谢。简短而甜蜜的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
相关资源
最近更新 更多