Surya sasidhar
Surya sasidhar

Reputation: 30303

Repeater Control in Asp.Net?

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.

Upvotes: 0

Views: 529

Answers (3)

Mubarek
Mubarek

Reputation: 2689

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

Upvotes: 0

Pankaj Tiwari
Pankaj Tiwari

Reputation: 1408

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

if(drpBack!=null)
{
  if(drpBack.SelectedItem.Value != "0")
    {
       // Do Whatever you want
    }
}

Upvotes: 2

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

why don't you do this?

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

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

Upvotes: 3

Related Questions