views:

621

answers:

3

First of all:

  • _ddlOptions is drop down list
  • _selectedOptions is repeater control

and it's just provisional code of my final control.

What I want to do is to get data for _ddlOption on !IsPostBack. There is Add button that enables user to move selected drop down item to repeater control.

It the following way of updating Repeater.Items correct? I found many solution of adding/removing elements manually using DataSource, but here my DataSource is null, as I set it only on !IsPostBack.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _ddlOptions.DataSource = new[] { 1, 2, 3 };
            _ddlOptions.DataBind();
        }
    }

    protected void OnAdd(object sender, EventArgs e)
    {
        var list = new ArrayList(_selectedOptions.Items);
        list.Add(_ddlOptions.SelectedItem);
        _ddlOptions.Items.RemoveAt(_ddlOptions.SelectedIndex);
        _selectedOptions.DataSource = list;
        _selectedOptions.DataBind();
    }
A: 

You can use a DataAdapter to fill a table in a DataSet.

DataSet ds = new DataSet();

using (SqlConnection conn = YourConnectionFactory.GetConnection())
{
SqlCommand objComm = DBHelper.CreateStoredProc("YourStoredProcedure", conn);
SqlDataAdapter adapt = new SqlDataAdapter(objComm);
adapt.Fill(ds, TableName);
conn.Close();
}

DataTable dt = ds.Tables[0];
for (int a=dt.Rows.Count-1; a>= 0; a--)
{
// check and insert as necessary
}

YourControl.DataSource = ds;
YourControl.DataBind();

You can also do something like this then,
like rebind Taken from: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx: < /br> Dim values As New ArrayList() < /br> values.Add(New PositionData("Microsoft", "Msft"))
values.Add(New PositionData("Intel", "Intc"))
values.Add(New PositionData("Dell", "Dell"))
Repeater1.DataSource = values Repeater1.DataBind()
Repeater2.DataSource = values Repeater2.DataBind()

James Campbell
Hmm, I think you are missing a point. I'm going to fetch data only once on !IsPostBack and later I'm won't store data source. I'm using ViewState mechanism so that controls restore their content.
dragonfly
A: 

Looks good to me. You just may want to move the decalration for your list outside the onAdd method. As you have it I think it will be reinitialized every time the add button is clicked, so you'll never have more than the currently selected item in your repeater.

Ian Jacobs
+1  A: 

If you only need to fetch data once and you're going to use viewstate, get the data first time you need it, store it in VS and get it from VS for all future PostBacks.

Example:

public List<int> Data
    {
        get
        {
            if (ViewState["Data"] == null)
            {
                // Get your data, save it and return it.
                var data = new List<int> { 1, 2, 3 };
                ViewState["Data"] = data;
                return data;
            }
                return (List<int>)ViewState["Data"];
        }
        set
        {
            ViewState["Data"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            BindData(Data); 
        }
    }

    private void BindData(List<int> data)
    {
        _ddlOptions.DataSource = data;
        _ddlOptions.DataBind();
    }

    protected void OnAdd(object sender, EventArgs e)
    {
        var existing = Data;

        existing.Add(_ddlOptions.SelectedItem);                        
        _ddlOptions.Items.RemoveAt(_ddlOptions.SelectedIndex);

        Data = existing;
        BindData(existing);
    }

I didn't test this - and its only my first thought but you can build on it from here.

Patrick.

Patrick