C# SelectionList始终返回NULL

C# SelectionList始终返回NULL,c#,asp.net,mobile,null,C#,Asp.net,Mobile,Null,我有一个非常奇怪的问题,当我尝试检查SelectionList的选定项/值时,它总是返回NULL。我在谷歌上搜索了一下,发现当我单击submit按钮时,页面正在刷新,SelectionList正在重新绑定数据,因此它将恢复到原来的行为 然后我尝试将绑定代码封装在一个!IsPostBack,但当我尝试访问所选属性时,它仍然为null,并引发异常 任何帮助都将不胜感激 我的代码是这样的。。。支架未正确匹配 static SelectionList[] Symptoms; static string

我有一个非常奇怪的问题,当我尝试检查SelectionList的选定项/值时,它总是返回NULL。我在谷歌上搜索了一下,发现当我单击submit按钮时,页面正在刷新,SelectionList正在重新绑定数据,因此它将恢复到原来的行为

然后我尝试将绑定代码封装在一个!IsPostBack,但当我尝试访问所选属性时,它仍然为null,并引发异常

任何帮助都将不胜感激

我的代码是这样的。。。支架未正确匹配

static SelectionList[] Symptoms;
static string UserID = "";
CMB症状1、CMB症状2、CMB症状3和CMB症状4是选择列表。我将它们放入SelectionList数组,然后设置属性

我必须将它们设置为静态,否则当我单击按钮进行更新时,它们将不会保留其值。知道他们为什么不保留价值观吗

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"));
                }
然后在更新按钮中单击事件 受保护的无效更新\u Clickobject发送者,事件参数e {


}你可以尝试两件事。尝试将数据绑定代码放置在PreRender事件中。第二个也是更好的选择是使用ObjectDataSource控件并声明性地绑定控件。

如果我将代码放在预渲染中,无论它是否是回发,都会发生,对吗?不,您也可以在预渲染中执行相同的IsPostBack检查。加载后,PreRender事件会在页面生命周期中进一步发生,但您仍然可以检查IsPostBack属性。
 foreach (SelectionList listItem in Symptoms)
        {
            if (listItem.SelectedIndex != 0)
            {
                cmd.CommandText = "INSERT INTO Patient_Symptom (patientid,symptomid) VALUES (" + UserID + ",'" + listItem.Selection.Value + "')";
                cmd.ExecuteNonQuery();
            }

        }