2011-08-05 5 views

Respuesta

14

puede nosotros un objeto Dictionary para almacenar las claves/valores y enlazarlo a la RadioButtonList así:

 Dictionary<string, string> values = new Dictionary<string, string>(); 
     values.Add("key 1", "value 1"); 
     values.Add("key 2", "value 2"); 
     values.Add("key 3", "value 3"); 

     RadioButtonList radioButtonList = new RadioButtonList(); 
     radioButtonList.DataTextField = "Value"; 
     radioButtonList.DataValueField = "Key"; 
     radioButtonList.DataSource = values; 
     radioButtonList.DataBind(); 

O también puede agregar los elementos a la colección de artículos RadioButtonList así:

 radioButtonList.Items.Add(new ListItem("Text 1", "Value 1")); 
     radioButtonList.Items.Add(new ListItem("Text 2", "Value 2")); 
     radioButtonList.Items.Add(new ListItem("Text 3", "Value 3")); 
1

Puede usar una DataTable para la fuente de datos (u otra fuente enlazable), y enlazar la DataTable a la lista RadioButton. Use las propiedades DataTextField y DataValueField para especificar qué columna se usa para el texto y cuál se usa para el valor.

1

Puedes crear tu propia fuente de datos que se mostrará automáticamente en la caja de herramientas VisualStudio junto con otros controles estándar, si realmente necesita este tipo de fuente de datos probar siguiente:

public class CustomDataSource : System.Web.UI.WebControls.ObjectDataSource 
{ 
    public CustomDataSource()    
    { 
     // Hook up the ObjectCreating event so we can use our custom object 
     ObjectCreating += delegate(object sender, ObjectDataSourceEventArgs e) 
          { 
          // Here we create our custom object that the ObjectDataSource will use 
          e.ObjectInstance = new DataAccessor() 
          }; 
     } 


class DataAccessor 
{ 
    [DataObjectMethod(DataObjectMethodType.Insert, true)] 
    public void Add(string text, string value) 
    { 
     // Insert logic 
    } 

    [DataObjectMethod(DataObjectMethodType.Update, true)] 
    public void Update(int id, string text, string value) 
    { 
     // Update logic 
    } 

    [DataObjectMethod(DataObjectMethodType.Select, true)] 
    public IEnumerable<MyRadioButtonEntryWrapper> List(int filterById) 
    { 
     // Select logic 
    } 

}

ASPX :

<%@ Register TagPrefix="cc1" Namespace="DataLayer.DataSources" %> 
<cc1:CustomDataSource ID="customDataSource" runat="server" 
       TypeName="DataAccessor" 
       OldValuesParameterFormatString="original_{0}" 
       InsertMethod="Add"         
       UpdateMethod="Update"> 
       <UpdateParameters> 
        <asp:Parameter Name="id" Type="Int32" /> 
        <asp:Parameter Name="text" Type="String" /> 
        <asp:Parameter Name="value" Type="String" /> 
       </UpdateParameters> 
       <InsertParameters> 
        <asp:Parameter Name="text" Type="String" /> 
        <asp:Parameter Name="value" Type="String" /> 
       </InsertParameters> 
      </cc1:ArticleTypeDataSource> 
Cuestiones relacionadas