This is a migrated thread and some comments may be shown as answers.

dropdownlist selected item on item inserting

2 Answers 171 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Rizwan Ansari
Top achievements
Rank 1
Rizwan Ansari asked on 06 Jan 2014, 06:15 AM
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 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Jan 2014, 08:32 AM
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.
0
Rizwan Ansari
Top achievements
Rank 1
answered on 06 Jan 2014, 01:50 PM
Thanks princy your code works perfect.
Tags
ListView
Asked by
Rizwan Ansari
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Rizwan Ansari
Top achievements
Rank 1
Share this question
or