2011-02-27 83 views
3

我需要綁定網格視圖與複選框列,droupdown列和文本框。它就像默認模式是編輯。請幫我從後面的代碼中綁定組合框項目。組合框值需要從Web服務獲得。所有的網格數據綁定在代碼後面。用戶可以更改網格上的數據。背後Gridview綁定DropDownList後面的代碼

protected void Page_Load(object sender, EventArgs e) 
{ 

    DataTable table = new DataTable(); 
    table.Columns.Add("select", typeof(bool)); 
    table.Columns.Add("Roles", typeof(string)); 
    table.Columns.Add("Name", typeof(string)); 
    table.Rows.Add(true, "Admin", "name1"); 
    table.Rows.Add(true, "Admin", "name2"); 
    table.Rows.Add(true, "user", "name3"); 
    table.Rows.Add(false, "user", "name4"); 
    table.Rows.Add(false, "Admin", "name5"); 
    GridView1.DataSource = table; 
    GridView1.DataBind(); 

} 

回答

3

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 
     <Columns> 
     <asp:TemplateField > 
       <ItemTemplate > 
        <asp:CheckBox ID="CheckBox1" runat="server" Checked= "<%# Bind('select') %>" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Roles"> 
       <ItemTemplate> 
        <asp:DropDownList ID="DropDownList1" runat="server" 
         SelectedValue='<%# Eval("Roles") %>'> 
        </asp:DropDownList> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Name"> 
       <ItemTemplate> 
        <asp:TextBox ID="TextBox1" runat="server" Text="<%# Bind('Name') %>"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

代碼你可以嘗試這樣的事情。

<asp:DropDownList ID="DropDownList1" runat="server" 
OnDataBound="DropDownList_OnDataBound"></asp:DropDownList> 

protected void DropDownList_OnDataBound(object sender, EventArgs e) 

{ 
    DropDownList ddl = sender as DropDownList; 
    if(ddl != null) 
    { 
     // call web service and 
     // populate ddl.Items 
    } 
} 
相關問題