dropdownlist selected item on item inserting

3 posts, 0 answers
  1. Rizwan Ansari
    Rizwan Ansari avatar
    26 posts
    Member since:
    Nov 2009

    Posted 06 Jan 2014 Link to this post

    Dear Expert,

    i am getting wrong values of dropdownlist selected item when i am using item inserting command.
    i bind the ddl on item created event and able to get reference in item inserting event.
    please help.

    Thanks

    protected void RadListView1_ItemCreated(object sender, RadListViewItemEventArgs e)
            {
                if (e.Item is RadListViewInsertItem)
                {
                    
                    DropDownList frmyr = (DropDownList)e.Item.FindControl("drpFromYear");
                    DropDownList toyer = (DropDownList)e.Item.FindControl("drpToYear");
                    int yearLast = DateTime.Now.Year;
                    int yearThen = yearLast - 60;
                    for (int i = yearLast; i > yearThen; i--)
                    {
                        ListItem list = new ListItem();
                        list.Text = i.ToString();
                        list.Value = i.ToString();
                        frmyr.Items.Add(list);
                        toyer.Items.Add(list);
                    }
     
                }
     
            }
     protected void RadListView1_ItemInserting(object sender, RadListViewCommandEventArgs e)
            {
                try
                {
                    RadListViewInsertItem editedItem = (RadListViewInsertItem)e.ListViewItem;
                    DropDownList ddltest = (DropDownList)RadListView1.InsertItem.FindControl("drpFromYear");
                    string tt = ddltest.SelectedItem.Text;(Wrong Values)
                   
    }
    }
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Jan 2014 Link to this post

    Hi,

    Please try the following C# code snippet which works fine at my end.

    C#:
    protected bool IsInsertItem;
    protected void RadListView1_ItemCreated(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
    {
        if (e.Item is RadListViewInsertItem)
        {
            //flag will set to true when radlistview in insertmode
            IsInsertItem = true;
        }
    }
    protected void RadListView1_PreRender(object sender, EventArgs e)
    {
        if (IsInsertItem)
        {
            //items will added to the dropdownlist when radlistview is in insertmode
            DropDownList frmyr = (DropDownList)RadListView1.InsertItem.FindControl("drpFromYear");
            DropDownList toyer = (DropDownList)RadListView1.InsertItem.FindControl("drpToYear");
            int yearLast = DateTime.Now.Year;
            int yearThen = yearLast - 60;
            for (int i = yearLast; i > yearThen; i--)
            {
                ListItem list = new ListItem();
                list.Text = i.ToString();
                list.Value = i.ToString();
                frmyr.Items.Add(list);
                toyer.Items.Add(list);
            }
        }
    }
    protected void RadListView1_ItemCommand(object sender, RadListViewCommandEventArgs e)
    {
        if ( e.CommandName == "PerformInsert")
        {
            //on inserting the item,setting the flag as false so that it will not recreate dropdownlist
            IsInsertItem = false;
            DropDownList list = (DropDownList)RadListView1.InsertItem.FindControl("drpFromYear");
            string from = list.SelectedItem.Text;
            DropDownList list1 = (DropDownList)RadListView1.InsertItem.FindControl("drpToYear");
            string to = list1.SelectedItem.Text;
        }
    }

    Hope this will helps you.
    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rizwan Ansari
    Rizwan Ansari avatar
    26 posts
    Member since:
    Nov 2009

    Posted 06 Jan 2014 Link to this post

    Thanks princy your code works perfect.
Back to Top