Domanda

I have a site where a user goes to a tab to add a Manufacturer. They then move to another tab to add a Model. On this Model tab they enter a Model Number via textbox then select a Manufacturer from a dropdown list below that. The can also enter a Description (textbox).

The Manufacturer ddl is populated on the Page_Load (if I don't do it here, then when a new Manufacturer is added from the Manufacturer tab, it will not appear in the Manufacturer ddl on the Model tab). This is the code to populate the ddl:

private void PopulateListControls()
        {
            Dictionary<int, string> manufactuerers = ManufacturerAdapter.GetAllDictionary();

            ddlManufacturer.DataSource = manufactuerers;
            ddlManufacturer.DataTextField = "Value";
            ddlManufacturer.DataValueField = "Key";
            ddlManufacturer.DataBind();
        }

This is the Page_Load where the populate occurs:

protected void Page_Load(object sender, EventArgs e)

        {
            PopulateListControls();
        }

The problem I am having is that when the user hits the Save button the Manufacturer ddl is saving the first item in the list, not the one that is selected (because clicking that button is causing another PostBack?). The Model Number (textbox entry) gets saved as it should as does the Description.

If I try to write populate as a !IsPostBack check (see example below), it resolves this problem, but then I get the problem where the user is not able to see the Manufacturer they added in the Manufacturer tab.

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    { 
        PopulateListControls();
    }
}

Is there a different approach I could take that would resolve this?


EDIT: 04/01/2013 19:36


BUTTON SAVE CODE:

Here is the Button fire:

protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

The Save() runs:

    public bool Save()
    {
        bool bIsSaved = false;

        if (!ValidateForm())
        {
            return false;
        }

        PopulateObjectsFromForm();
        _manufacturerModel = ManufacturerModelAdapter.Save(_manufacturerModel, _currentUser); 

        if (_manufacturerModel.ManufacturerModelId != 0)
        {
            bIsSaved = true;
         }
        else
        { //problem occurred
            RaisePageNotificationEvent(new PageNotification(PageNotificationType.Error, "Model Number could not be saved"));
            return false;
        }

        if (bIsSaved)
        {
            RaiseActionEvent("Save");
            RaisePageNotificationEvent(new PageNotification(PageNotificationType.Generic, String.Format("Model Number Saved: {0}", _manufacturerModel.ModelNumber)));            }

        ClearForm(); 

        return bIsSaved;
    }

EDIT #2: 04/012/2014 19:55


PopulateObjectsFromForm code:

private void PopulateObjectsFromForm()
        {
            ManufacturerModel m = new ManufacturerModel();

            m.ManufacturerModelId = _manufacturerModel.ManufacturerModelId; // get from persisted object

            m.Manufacturer = ManufacturerAdapter.GetById(Convert.ToInt32(this.ddlManufacturer.SelectedValue));
            m.ModelNumber = this.txtModelNumber.Text;
            m.Description = this.txtDescription.Text;

            _manufacturerModel = m; // set persisted object to new object, values

        }
È stato utile?

Soluzione

Finally resolved this. I set the ddlManufacturer control's AutoPostBack property to true in the HTML:

<asp:DropDownList ID="ddlManufacturer" runat="server" CssClass="form-data" AutoPostBack="True">

and changed the Page_Load to grab the selected value from the ddl before the Instantiate() (which populates the list controls)

protected void Page_Load(object sender, EventArgs e)
{
    if (this.IsPostBack)
        this.ddlManufacturer.SelectedValue = Request.Form[ddlManufacturer.UniqueID];
    {
        Instantiate();
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top