0

In web application using asp.net, i am using repeater control, in ItmeCommand event i am trying to find a control using fid control method, i write the code for finding the control it is working fine, when the control is not in repeater control, i am getting exception. How can i handle the exception if the control is not in repeater control. My code is like this :

if (((DropDownList)rpPendingApprovals.Items[i].FindControl "drpBack")).SelectedItem.Value != "0")

when dropdown controls is not there, in repeater then how can i handle this exception help me, thank you.

Pieter Germishuys
  • 4,828
  • 1
  • 26
  • 34
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219

3 Answers3

3

why don't you do this?

var dropdown = (DropDownList)rpPendingApprovals.Items[i].FindControl("drpBack"));

    if (dropdown != null && dropdown.SelectedItem.Value != "0")
Shoaib Shaikh
  • 4,565
  • 1
  • 27
  • 35
2
DropDownList drpBack = (DropDownList)rpPendingApprovals.Items[i].FindControl("drpBack");

if(drpBack!=null)
{
  if(drpBack.SelectedItem.Value != "0")
    {
       // Do Whatever you want
    }
}
Pankaj Tiwari
  • 1,378
  • 11
  • 13
0

When you've tried Shoaib's code you are getting exception because if the drodown is not null the second expression is checked which if SelectedItem is null creates the exception so nest the expressions as

var dropdown = (DropDownList)rpPendingApprovals.Items[i].FindControl("drpBack"));

if (dropdown != null && dropdown.SelectedItem != null)
   if(dropdown.SelectedValue !="0")

problem hopefully is gone

Mubarek
  • 2,691
  • 1
  • 15
  • 24