在 itemdatabound 的列表视图中设置 Dropdownlist 值

发布于 2024-08-27 18:15:40 字数 276 浏览 6 评论 0原文

我有动态发布的年份下拉列表。我已经使用对象数据源填充了下拉列表。在插入列表视图控件时它工作正常。但是当我单击编辑按钮时,应该设置来自数据库的下拉列表值。 例如,如果我有一行包含 Year=2006 和 Month=“Jan” 然后单击编辑按钮,这些下拉列表应该被填充。

我已经在 ItemDataBound 中编写了代码来设置 dropdownlilst 的值。但是当我使用 findcontrol 时,它的值为 null,因此出现对象引用错误。所以请为我提供解决方案。

谢谢萨米尔

i have dropdownlist of year which is coming dynamically.i have filled the dropdownlist using object datasource.on inserting in the listview control it is working fine. but when i click on edit button that dropdownlist value should be set which is coming from the database.
e.g. if i have a row which contains Year=2006 and month="Jan"
then on click on edit button these dropdown list should be fill up.

i have written the code in ItemDataBound to set the value of the dropdownlilst.but when i use findcontrol its taking null so object reference error is coming. so please provide me the solution.

thanks

samir

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め七分饶幸 2024-09-03 18:15:40
 protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
 {
     if (e.Item.ItemType == ListViewItemType.DataItem)
     {
          DropDownList ddl = (DropDownList)e.Item.FindControl("nameOfDDLOnAspxPage");
          ddl.SelectValue = (however you are getting the year data for this row);
     }
 }
 protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
 {
     if (e.Item.ItemType == ListViewItemType.DataItem)
     {
          DropDownList ddl = (DropDownList)e.Item.FindControl("nameOfDDLOnAspxPage");
          ddl.SelectValue = (however you are getting the year data for this row);
     }
 }
青巷忧颜 2024-09-03 18:15:40

我编写了以下代码

protected void ListView_Articles_ItemDataBound(object sender, ListViewItemEventArgs e )
{

        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (cmd == "edit")
            {
                // Display the e-mail address in italics.
                int month, year;
                month = Convert.ToDateTime(DataBinder.Eval(((ListViewDataItem)e.Item).DataItem,"Created")).Month;
                year = Convert.ToDateTime(DataBinder.Eval(((ListViewDataItem)e.Item).DataItem, "Created")).Year;
                ListViewDataItem item = (ListViewDataItem)e.Item;

                DropDownList ddlmonth = (DropDownList)e.Item.FindControl("ddlmonth");
                DropDownList ddlyear = (DropDownList)e.Item.FindControl("ddlyear");
                ListItem lstitem = ddlyear.Items.FindByValue(year.ToString()); 

// 我发现 ddlyear 为 null,因此无法绑定数据。

if (ddlmonth != null)
                {
                    foreach (ListItem monthitem in ddlmonth.Items)
                    {
                        if (int.Parse(monthitem.Value) == month)
                        {
                            ddlmonth.ClearSelection();
                            monthitem.Selected = true;
                            return;
                        }
                    }
                }
                if (ddlyear != null)
                {
                    foreach (ListItem yearitem in ddlyear.Items)
                   {
                       if (int.Parse(yearitem.Value) == year)
                        {
                           ddlyear.ClearSelection();
                            yearitem.Selected = true;
                            return;
                        }
                    }
                }
            }

        }


    }

i have written the below code

protected void ListView_Articles_ItemDataBound(object sender, ListViewItemEventArgs e )
{

        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (cmd == "edit")
            {
                // Display the e-mail address in italics.
                int month, year;
                month = Convert.ToDateTime(DataBinder.Eval(((ListViewDataItem)e.Item).DataItem,"Created")).Month;
                year = Convert.ToDateTime(DataBinder.Eval(((ListViewDataItem)e.Item).DataItem, "Created")).Year;
                ListViewDataItem item = (ListViewDataItem)e.Item;

                DropDownList ddlmonth = (DropDownList)e.Item.FindControl("ddlmonth");
                DropDownList ddlyear = (DropDownList)e.Item.FindControl("ddlyear");
                ListItem lstitem = ddlyear.Items.FindByValue(year.ToString()); 

// i found that ddlyear is null so it unable to bind the data.

if (ddlmonth != null)
                {
                    foreach (ListItem monthitem in ddlmonth.Items)
                    {
                        if (int.Parse(monthitem.Value) == month)
                        {
                            ddlmonth.ClearSelection();
                            monthitem.Selected = true;
                            return;
                        }
                    }
                }
                if (ddlyear != null)
                {
                    foreach (ListItem yearitem in ddlyear.Items)
                   {
                       if (int.Parse(yearitem.Value) == year)
                        {
                           ddlyear.ClearSelection();
                            yearitem.Selected = true;
                            return;
                        }
                    }
                }
            }

        }


    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文