2014-01-07 3 views
0

У меня есть две страницы один MasterPage.master и по умолчанию я думаю, что эта ошибка для двух таблиц [0] Я использую на главной странице, мастер один опрос с таблицей данных и по умолчанию я использую таблицу данных для показа новости, когда таблица удаления данных в страницы по умолчанию и запустить правильно с из ошибок при использовании две таблицы данных я вижу ошибкукак sloved System.ArgumentOutOfRangeException

при запуске defualt.aspx эта ошибка см:

Specified argument was out of the range of valid values. 
Parameter name: index 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. 
Parameter name: index 

Source Error: 


Line 88:    tbl.BorderWidth = 0; 
Line 89:    tbl.Attributes.Add("Style", "text-align:right"); 
Line 90:    ImageButton ButtonPolls = (ImageButton)tbl.Controls[0]; 
Line 91:    ButtonPolls.ImageUrl = "../images/poll/CastVote.jpg"; 

Line 92:   

этот код использовать MasterPage.master

string strSQL = "select QuestionText from TPollQuestions where Iscurrent=1 and Isarchived=0"; 
     string cmdtext = ""; 
     SqlConnection conn = Conn; 
     Pollcontrol1.CanVote = true; 

     if (conn.State == System.Data.ConnectionState.Closed) 
      conn.Open(); 

     cmdtext = "select QuestionText from TPollQuestions where Iscurrent=1 and Isarchived=0"; 
     cmd = new SqlCommand(cmdtext, conn); 
     Pollcontrol1.PollQuestion = cmd.ExecuteScalar().ToString(); 
     conn.Close(); 
     cmdtext = 
      "select optionID,PollID,OptionText,Votes from TPollOptions where pollID in(select PollID from TPollquestions where Iscurrent=1 and Isarchived=0)"; 
     SqlDataAdapter da = new SqlDataAdapter(cmdtext, conn); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 

     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      Pollcontrol1.AddPollAnswer(Convert.ToInt32(dt.Rows[i]["pollID"]), Convert.ToInt32(dt.Rows[i]["optionID"]), dt.Rows[i]["optionText"].ToString(), Convert.ToInt32(dt.Rows[i]["votes"])); 
     } 

     TableCell tbl = (TableCell)Pollcontrol1.Controls[0].Controls[Pollcontrol1.Controls[0].Controls.Count - 1].Controls[0]; 
     tbl.BorderWidth = 0; 
     tbl.Attributes.Add("Style", "text-align:right"); 
     ImageButton ButtonPolls = (ImageButton)tbl.Controls[0]; 
     ButtonPolls.ImageUrl = "../images/poll/CastVote.jpg"; 

и этот код в default.aspx это DataTable для показа новостей, если удалить GetData (ул) выполняется правильно с вне ошибки

PagedDataSource pgsource = new PagedDataSource(); 
int findex, lindex; 
DataRow dr1; 
static string str = "select * from TNews where 1=1"; 
protected void Page_Load(object sender, EventArgs e) 
{ 

    if (!IsPostBack) 
    { 
     //CurrentPage = 0; 
GetData(str); 
    } 
} 

DataTable GetData(string str) 
{ 
    DataTable dtable1= new DataTable(); 

    SqlConnection Conn; 
    SqlCommand Cmd; 

    Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["bakerConnectionString"].ToString()); 
    Cmd = new SqlCommand(); 
    Conn.Open(); 
    Cmd.Connection = Conn; 

    Cmd.CommandText = str; 

    SqlDataAdapter dap1= new SqlDataAdapter(Cmd); 
    DataSet ds1 = new DataSet(); 
    dap1.Fill(ds1, "ds1"); 
    pgsource.DataSource = ds1.Tables[0].DefaultView; 
    DataBind(); 
    return ds1.Tables[0]; 
} 

ответ

0

Глядя на сообщение об ошибке, я бы предположить, что ваша проблема здесь:

Line 90:    ImageButton ButtonPolls = (ImageButton)tbl.Controls[0]; 

Кроме того, я вижу, что вы используете индексный поиск элементов управления довольно много, что нежелательно. Вместо этого используйте метод Control.FindControl для извлечения дочерних элементов управления (и, конечно же, проверьте на null).

Предположим, что ваш ImageButton имеет идентификатор _imgButton. Для того, чтобы извлечь его из использования таблицы:

var imgButton = tbl.FindControl("_imgButton") as ImageButton; 
if(imgButton != null) 
{ 
    // your logic here... 
} 
+0

благодаря РМКО использовать IAM Dont ID кнопки – user3166739

+0

@ user3166739, я извиняюсь но я не понимаю, что вы пытаетесь сказать ... – RePierre

Смежные вопросы