views:

24

answers:

1

I have a very strange issue where SelectionList always returns NULL when i try check its Selected Item/Value. I Googled a bit and I found out that when i click the submit button, the page is being refreshed and the SelectionList is being data bound again so it will revert back to its original behavior.

Then i tried enclosing the binding code in the Page_Load event in a !IsPostBack but still when i try to access the Selected property it is null and an exception is thrown.

Any help would be greatly appreciated.

My code goes something like this... (the braces are not matched properly)

static SelectionList[] Symptoms;
static string UserID = "";

cmbSymptoms1,cmbSymptoms2,cmbSymptoms3 and cmbSymptoms4 are SelectionLists. I took them in to an array of SelectionList and then set the properties.

I had to make them static or else when i click the button to update, they will not retain their values. Any idea why they don't retain the values?

protected void Page_Load(object sender, EventArgs e)
{

if (this.IsPostBack == false)
        {
            //System.Diagnostics.Debug.WriteLine("Not IsPostBack");

            if (Request.QueryString["id"] != null && Request.QueryString.ToString() != "")
            {
                //System.Diagnostics.Debug.WriteLine("id query string is not null :- " + Request.QueryString["id"]);

                myclass = new Class1();

                UserID = Request.QueryString["id"];

                Symptoms = new SelectionList[4];

                Symptoms[0] = cmbSymptoms1;
                Symptoms[1] = cmbSymptoms2;
                Symptoms[2] = cmbSymptoms3;
                Symptoms[3] = cmbSymptoms4;

                System.Data.DataTable dt = myclass.getAllSymptoms();

                foreach (SelectionList listItem in Symptoms)
                {
                    listItem.DataSource = dt;
                    listItem.DataTextField = "symptomsname";
                    listItem.DataValueField = "symptomsid";
                    listItem.DataBind();
                    listItem.Items.Insert(0, new MobileListItem("None"));
                }

And in the update button click event protected void cmbUpdate_Click(object sender, EventArgs e) {

 foreach (SelectionList listItem in Symptoms)
        {
            if (listItem.SelectedIndex != 0)
            {
                cmd.CommandText = "INSERT INTO Patient_Symptom (patientid,symptomid) VALUES (" + UserID + ",'" + listItem.Selection.Value + "')";
                cmd.ExecuteNonQuery();
            }

        }   

}

A: 

You can try two things. Try placing the databinding code in the PreRender event. The second and better option would be to use an ObjectDataSource controls and bind the control declaratively.

drohm
If i place the code in the pre render, it would always happen whether or not it is a post back right?
dasunARTworks
No, you can do the same IsPostBack check in the PreRender as well. The PreRender event happens further in the page lifecycle after Load, but you can still check the IsPostBack property.
drohm