2012-07-20 38 views
2

我已經構建了一些綁定到linq數據源的下拉列表。當我在gridview上選擇一行時,執行一個查詢,然後根據查詢中的數據設置下拉列表選擇。當我這樣做時,RegionListBox工作正常,但是當我嘗試使用.FindByName設置其他三個列表框的.Selected屬性時,它顯示它們尚未按操作順序填充。當我在下面的代碼中註釋出三個「.FindByName」行時,代碼將毫無問題地運行,但當然下拉值不會被選中。當我用我的3 findbyname行代碼執行代碼時,它的第一個錯誤與對象引用未設置爲對象的實例。 - 在列表的項目集合上設置手錶,顯示爲空FindByName或FindByValue set .selected dropdownlist返回錯誤對象引用未設置爲對象實例

問題是:爲什麼當regionlistbox項目選擇已填充時,dropdownlist項目集合爲空?何時/應在哪裏設置所選內容,以便填充下拉列表項目集合?

 protected void ItemGridView_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Page") 
      return; 

     if (e.CommandName == "EditItem") 
     { 
      string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' }); 

      ItemGridView.SelectedIndex = -1; 
      ItemGridView.SelectedIndex = int.Parse(commandArgs[1]); 
      ServiceListBox.ClearSelection(); 
      RegionListBox.ClearSelection(); 

      editPanel.Visible = true; 
      //query your database and store the result set in a DataTable 
      DasLinqClassesDataContext db = new DasLinqClassesDataContext(); 
      var ItemRowSelected = db.GetItemForUserById(Session["CSPAttuid"].ToString(), int.Parse(commandArgs[0])).FirstOrDefault(); 
      if (ItemRowSelected != null) 
      { 
       if (ItemRowSelected.region != null) 
       { 
        string[] RegionIds; 
        RegionIds = ItemRowSelected.region.Split(','); 
        foreach (string element in RegionIds) 
        { 
         RegionListBox.Items.FindByValue(element.ToString()).Selected = true; 
        } 
       } 


       fieldTitle.Text = ItemRowSelected.title; 
       fieldNewOverview.Text = ItemRowSelected.overview; 
       lblPreviewTitle.Text = ItemRowSelected.title; 
       lblPreviewAudience.Text = ItemRowSelected.audience; 
       lblPreviewRegion.Text = ItemRowSelected.region_name; 
       lblPreviewService.Text = ItemRowSelected.service; 
       lblPreviewOverview.Text = ItemRowSelected.overview; 
       lblPreviewDocument.Text = ItemRowSelected.doc; 
       lblPreviewAuthor.Text = ItemRowSelected.author_name; 
       //urgent checkbox/image 
       if (ItemRowSelected.urgent == true) 
       { 
        urgentEditImage.Visible = true; 
        checkUrgent.Checked = true; 
        urgentViewImage.Visible = true; 
       } 
       else 
       { 
        urgentEditImage.Visible = false; 
        checkUrgent.Checked = false; 
        urgentViewImage.Visible = false; 
       } 
       //audio hyperlink/image 
       if (ItemRowSelected.audio != null) 
       { 
        audioEditImage.Visible = true; 
        audioViewImage.Visible = true; 
        fieldAudio.Text = ItemRowSelected.audio.ToString(); 
       } 
       else 
       { 
        audioEditImage.Visible = false; 
        audioViewImage.Visible = false; 
        fieldAudio.Text = ""; 
       } 
       //document 
       if (ItemRowSelected.doc != null) 
       { 
        fieldDocument.Text = ItemRowSelected.doc; 
        fieldDocLink.Text = ItemRowSelected.link; 
       } 
       //service 
       if(ItemRowSelected.service != null) 
       { 
        ServiceListBox.Items.FindByText(ItemRowSelected.service.ToString()).Selected = true; 
       } 
       //audience 
       if (ItemRowSelected.audience != null) 
       { 
        ServiceListBox.Items.FindByText(ItemRowSelected.audience.ToString()).Selected = true; 
       } 
       //author 
       if (ItemRowSelected.author != null) 
       { 
        ServiceListBox.Items.FindByText(ItemRowSelected.author.ToString()).Selected = true; 
       } 
      } 
      hiddenId.Value = ItemRowSelected.id.ToString(); 

     } 
    } 

下面是一些爲下拉列表的頁面代碼:

.....d> 
       <td width=707 colspan=2 valign=top style='width:530.25pt;border:none;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt'> 
       <p class=MsoNormal> 
       <b> 
        <asp:DropDownList ID="ServiceListBox" runat="server" 
         DataSourceID="LinqDsService" DataTextField="name" DataValueField="id"> 
        </asp:DropDownList><asp:ImageButton ID="AddNewServiceToDropdown" runat="server" 
         CausesValidation="False" ImageUrl="~/images/addButton.png" /> 
        <asp:ModalPopupExtender ID="ModalPopAddNewServiceToDropdown" runat="server" 
         BackgroundCssClass="modalBackground" CancelControlID="btnCancelService" 
         DynamicServicePath="" Enabled="True" PopupControlID="panAddService" 
         TargetControlID="AddNewServiceToDropdown"> 
        </asp:ModalPopupExtender> 
       </span> 
       </b> 
       </p> 
       </td> 
       </tr> 
       <tr> 
       <td valign=top 
         style='border-left:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FF6600;padding:0in 5.4pt 0in 5.4pt; border-top-style: none; border-top-color: inherit; border-top-width: medium; border-bottom-style: none; border-bottom-color: inherit; border-bottom-width: medium;' 
         class="style2"> 
       <p class=MsoNormal> 
       <span style='font-family:"ClearviewATT","sans-serif"'> 
       <o:p>&nbsp;</o:p> 
       </span> 
       </p> 
       </td> 
       <td width=193 colspan=2 valign=top style='width:144.55pt;border:none;padding:0in 5.4pt 0in 5.4pt'> 
       <p class=MsoNormal> 
       <span style='font-size:11.0pt;font-family:"ClearviewATT LT","sans-serif"'>Overview:<o:p> 
       </o:p> 
       </span> 
       </p> 
       </td> 
       <td width=707 colspan=2 valign=top style='width:530.25pt;border:none;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt'> 
       <p class=MsoNormal 
         style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto; height: 52px; width: 602px;'> 
        <asp:TextBox ID="fieldNewOverview" TextMode="MultiLine" runat="server" 
         Width="600px"></asp:TextBox> 
       </p> 
       </td> 
       </tr> 
       <tr> 
       <td valign=top 
         style='border-left:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FF6600;padding:0in 5.4pt 0in 5.4pt; border-top-style: none; border-top-color: inherit; border-top-width: medium; border-bottom-style: none; border-bottom-color: inherit; border-bottom-width: medium;' 
         class="style2"> 
       <p class=MsoNormal> 
       <span style='font-family:"ClearviewATT","sans-serif"'> 
       <o:p>&nbsp;</o:p> 
       </span> 
       </p> 
       </td> 
       <td width=193 colspan=2 valign=top style='width:144.55pt;border:none;padding:0in 5.4pt 0in 5.4pt'> 
       <p class=MsoNormal> 
       <span style='font-size:11.0pt;font-family:"ClearviewATT LT","sans-serif"'>WIIFM (What&#8217;s In It For Me):<o:p> 
       </o:p> 
       </span> 
       </p> 
       </td> 
       <td width=707 colspan=2 valign=top style='width:530.25pt;border:none;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt'> 
       <p class=MsoNormal> 
        <asp:TextBox ID="TextBox1" runat="server" Width="600px"></asp:TextBox> 
       </p> 
       </td> 
       </tr> 
       <!--- document number ---> 
       <tr> 
       <td valign=top 
         style='border-left:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FF6600;padding:0in 5.4pt 0in 5.4pt; border-top-style: none; border-top-color: inherit; border-top-width: medium; border-bottom-style: none; border-bottom-color: inherit; border-bottom-width: medium;' 
         class="style2"> 
       <p class=MsoNormal> 
       <span style='font-family:"ClearviewATT","sans-serif"'> 
       <o:p>&nbsp;</o:p> 
       </span> 
       </p> 
       </td> 
       <td width=193 colspan=2 valign=top style='width:144.55pt;border:none;padding:0in 5.4pt 0in 5.4pt'> 
       <p class=MsoNormal> 
       <span style='font-size:11.0pt;font-family:"ClearviewATT LT","sans-serif"'>Document Number:<o:p> 
       </o:p> 
       </span> 
       </p> 
       </td> 
       <td width=707 colspan=2 valign=top style='width:530.25pt;border:none;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt'> 


       <p class=MsoNormal> 
        <table> 
        <tr> 
         <td><span style='font-size:11.0pt;font-family:"ClearviewATT LT","sans-serif"'><asp:Label ID="Label3" runat="server" Text="Document Name"></asp:Label></span></td> 


          <td> 
           <asp:TextBox ID="fieldDocument" runat="server" Width="212px"></asp:TextBox> 
          </td> 

       </tr> 
       <tr> 
        <td><span style='font-size:11.0pt;font-family:"ClearviewATT LT","sans-serif"'><asp:Label ID="Label4" runat="server" Text="Hyperlink"></asp:Label></span></td> 
        <td><asp:TextBox ID="fieldDocLink" runat="server" Width="485px"></asp:TextBox></td></tr></table> 
       </p> 
       </td> 
       </tr> 
       <!--- author ---> 
       <tr> 
       <td valign=top 
         style='border-left:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;background:#FF6600;padding:0in 5.4pt 0in 5.4pt; border-top-style: none; border-top-color: inherit; border-top-width: medium; border-bottom-style: none; border-bottom-color: inherit; border-bottom-width: medium;' 
         class="style2"> 
       <p class=MsoNormal> 
       </p> 
       </td> 
       <td width=193 colspan=2 valign=top style='width:144.55pt;border:none;padding:0in 5.4pt 0in 5.4pt'> 
       <p class=MsoNormal> 
       <span style='font-size:11.0pt;font-family:"ClearviewATT LT","sans-serif"'>Author:<o:p> 
       </o:p> 
       </span> 
       </p> 
       </td> 
       <td width=707 colspan=2 valign=top style='width:530.25pt;border:none;border-right:solid windowtext 1.0pt;padding:0in 5.4pt 0in 5.4pt'> 
       <p class=MsoNormal> 
       <asp:DropDownList ID="AuthorListBox" runat="server" DataSourceID="LinqDsAuthor" 
         DataTextField="name" DataValueField="id"> 
        </asp:DropDownList> etc..... 

,這裏是一些頁面代碼的LINQ數據源:

<!---begin linqdatasources---> 
<asp:LinqDataSource ID="Region" runat="server" 
    ContextTypeName="das.DasLinqClassesDataContext" EntityTypeName="" 
    Select="new (id, name)" TableName="ref_regions" Where="active == @active"> 
    <WhereParameters> 
     <asp:Parameter DefaultValue="true" Name="active" Type="Boolean" /> 
    </WhereParameters> 
</asp:LinqDataSource> 
<asp:LinqDataSource ID="LinqDsService" runat="server" 
    ContextTypeName="das.DasLinqClassesDataContext" EntityTypeName="" 
    Select="new (id, name, active)" TableName="ref_services" 
    Where="active == @active"> 
    <WhereParameters> 
     <asp:Parameter DefaultValue="true" Name="active" Type="Boolean" /> 
    </WhereParameters> 
</asp:LinqDataSource> 
<asp:LinqDataSource ID="LinqDsAudience" runat="server" 
    ContextTypeName="das.DasLinqClassesDataContext" EntityTypeName="" 
    Select="new (id, name, type)" TableName="ref_audiences" 
    Where="active == @active"> 
    <WhereParameters> 
     <asp:Parameter DefaultValue="true" Name="active" Type="Boolean" /> 
    </WhereParameters> 
</asp:LinqDataSource> 
<br /> 
<asp:LinqDataSource ID="LinqDsAuthor" runat="server" 
    ContextTypeName="das.DasLinqClassesDataContext" EntityTypeName="" 
    Select="new (id, name)" TableName="ref_authors" Where="active == @active"> 
    <WhereParameters> 
     <asp:Parameter DefaultValue="true" Name="active" Type="Boolean" /> 
    </WhereParameters> 
</asp:LinqDataSource> 
<!---end linqdatasources---> 

回答

3

使用:

ServiceListBox.SelectedIndex = ServiceListBox.Items.IndexOf(ServiceListBox.Items.FindByText(ItemRowSelected.service.ToString())); 

而不是

ServiceListBox.Items.FindByText(ItemRowSelected.service.ToString()).Selected = true; 

不知道爲什麼,但它現在正常工作與新代碼上面

0

這爲我工作:

For Each item As ListEditItem In cbCurr.Items 
    If String.Compare(item.Text.Trim, .Item("Curr").ToString.Trim, True) = 0 Then 
     cbCur.SelectedIndex = item.Index 
     Exit For 
    End If 
Next 
相關問題