2011-05-22 113 views
0

3編輯時,在同一行中顯示下拉列表。ASP.NET編輯GridView行

當1 dropdownlist有一個選擇,其他2應該去索引0(空的)。我的代碼

例子:

   <asp:TemplateField HeaderText="Project" SortExpression="Project"> 
        <ItemTemplate> 
         <%#Eval("Project") %> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:DropDownList ID="ProjectDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ProjectDropDownList_Changed" AppendDataBoundItems="true" 
          DataSourceID="ProjectDataSource" DataTextField="navn" DataValueField="navn"> 
          <asp:ListItem>-- choose one --</asp:ListItem> 
         </asp:DropDownList> 
        </EditItemTemplate> 
       </asp:TemplateField> 

我的SelectionChanged,甚至處理:

protected void ProjektDropDownList_SelectionChanged(object sender, EventArgs e) 
     { 
      DropDownList project = (DropDownList) GridView1.Rows[GridView1.SelectedIndex].FindControl("ProjectDropDownList"); 
      DropDownList kunde = (DropDownList)GridView1.Rows[GridView1.SelectedIndex].FindControl("KundeDropDownList"); 
      DropDownList øvrige = (DropDownList)GridView1.Rows[GridView1.SelectedIndex].FindControl("ØvrigeDropDownList"); 

      if(project.SelectedIndex >= 0 && kunde != null && øvrige != null) { 
       kunde.SelectedIndex = 0; 
       øvrige.SelectedIndex = 0; 
      } 

     } 

我在嘗試在同一行中獲取其它控制器,一個空指針... 我該如何解決?

+0

空指針爲哪個對象? – gbs 2011-05-22 13:46:02

+0

DropDownList的空指針我試圖到達UpdatingEvent/EditingEvent的外部。 – KristianB 2011-05-22 15:28:38

回答

2

我不確定這是您正在查找的SelectedIndex。沒有你應該使用的EditItemIndex嗎?

+0

簡單的工作!看起來像這樣:'DropDownList project =(DropDownList)GridView1.Rows [GridView1.EditIndex] .FindControl(「ProjectDropDownList」);' – KristianB 2011-05-22 15:33:24