2011-02-15 90 views
0

我有一個模板Gridview,我只想顯示一列(問題---這些都是數據庫獲得),另一個可能的答案(選項)下拉列表。下拉列表的值根據問題的類型而變化。只有兩種類型:T/F或範圍(低,中,高)。所以如果問題是類型1,下拉列表應該只顯示T/F。同樣,如果它是第二類。如何在Webform Gridview中動態加載下拉列表?

下圖就是GridView和加載的下拉列表(據說)的方法:

<asp:GridView AutoGenerateColumns="false" runat="server" ID="SurveyView"> 
    <Columns> 

     <asp:BoundField HeaderText="Questionnaire" DataField="Questionaire" ReadOnly="true"/> 
     <asp:BoundField HeaderText="QuestionID" DataField="Id" ReadOnly="true" Visible="false" /> 
     <asp:BoundField HeaderText="IsBoolean" DataField="Filter" ReadOnly="true" Visible="false" /> 
     <asp:TemplateField HeaderText="Response"> 
      <ItemTemplate> 
       <asp:DropDownList ID="UserDropDown" runat="server" AppendDataBoundItems="true" DataSource="LoadDropdownList(Filter)" DataTextField="key" DataValueField="value"></asp:DropDownList> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 


     public Dictionary<String, String> GenerateDropdownList(bool BooleanFilterStatus) 
    { 
     Dictionary<String, String> tempStores = new Dictionary<string, string>() ; 
     if (BooleanFilterStatus) 
     { 
      tempStores.Add(Boolean.TrueString, Boolean.TrueString); 
      tempStores.Add(Boolean.FalseString, Boolean.FalseString); 
     } 
     else 
     { 
      tempStores.Add("NONE", "NIL"); 
      tempStores.Add("Lo", "Low"); 
      tempStores.Add("Medium", "Medium"); 
      tempStores.Add("High", "High"); 
     } 

     return tempStores; 
    } 

我希望通過LoadDropdownList(),它會被填充列表。但這似乎並不奏效。

任何想法或其他可能的解決方案,將不勝感激。

回答

2

如何像

<asp:DropDownList ID="UserDropDown" runat="server" AppendDataBoundItems="true" 
ondatabinding="DropDownList1_DataBinding" DataTextField="key" DataValueField="value"></asp:DropDownList> 

,並在代碼隱藏

protected void DropDownList1_DataBinding(object sender, EventArgs e) 
    { 
     var ddl = sender as DropDownList; 
     if(ddl!=null) 
     { 
      //populate list. 
      ddl.Items.Add(new ListItem("test")); 
     } 
    } 
+0

酷。感謝您的建議,我會嘗試。 – yungun 2011-02-16 03:27:01