2011-02-23 65 views
0

我發佈了一個類似的RBL問題,但我遇到了一個新問題,所以我想我會發一個新帖子。ASP.NET單選按鈕列表| SelectedValue來了NULL

這裏是我的代碼:

的Page_Load

protected void Page_Load(object sender, EventArgs e) 
{ 
    //Output Success/Error Message 
    if (Session["formProcessed"] != null) 
    { 
     Label lblMessage = (Label)Master.FindControl("lblMessage"); 
     new Global().DisplayUserMessage("success", Session["formProcessed"].ToString(), lblMessage); 
    } 
    Session.Remove("formProcessed"); 

    if (Page.IsPostBack == false) 
    { 
     rblContentTypesGetAll.DataBind(); 
    } 
} 

rblContentTypesGetAll_Load

protected void rblContentTypesGetAll_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     DataTable dt = new DataTable(); 
     using (SqlConnection con = new SqlConnection(Global.conString)) 
     using (SqlCommand cmd = new SqlCommand("contentTypeGetAll", con)) 
     using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 
     { 
      da.Fill(dt); 
     } 
     //Clear Items before reloading 
     rblContentTypesGetAll.Items.Clear(); 

     //Populate Radio button list 
     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      rblContentTypesGetAll.Items.Add(new ListItem(dt.Rows[i]["contentType"].ToString() + " - " + dt.Rows[i]["description"].ToString(), 
       dt.Rows[i]["ID"].ToString())); 
     } 

     //Set Default Selected Item by Value 
     rblContentTypesGetAll.SelectedIndex = rblContentTypesGetAll.Items.IndexOf(rblContentTypesGetAll.Items.FindByValue(((siteParams)Session["myParams"]).DefaultContentType.ToLower())); 
    } 

} 

HTML/ASP.NET前端

<asp:RadioButtonList id="rblContentTypesGetAll" OnLoad="rblContentTypesGetAll_Load" runat="server"> 
     </asp:RadioButtonList> 

當我提交表單一世t似乎selectedValue變成空白。我在做什麼這顯然是不正確的?

回答

4

雖然大家都是有幫助的,問題是深刻得多。我有viewState禁用。

+0

我想殺死'view-state' – Arshad 2014-07-05 11:46:03

1

所有的代碼在你Page_Load需要在裏面:

if(Page.IsPostBack == false) 

您重新填充列表頁面提交引起列表時重新填充,並因此失去了以前的項目,包括其中之一是選擇。

http://gurustop.net

+0

看來按下提交按鈕後,該值立即消失。我在上面發佈的內容中包裝了DataBind。 – balexander 2011-02-23 02:52:59

0

嘗試通過填充在Page_Load中你需要結合不忘記用(!的IsPostBack)

+0

沒有工作。我更新了上面的代碼。 – balexander 2011-02-23 02:53:15

+0

我問的是綁定在頁面加載而不是單選按鈕加載 – Dotnet 2011-02-23 03:37:10

+0

你有一個小代碼示例? – balexander 2011-02-23 12:51:14

0

我認爲可能會發生的唯一的事情是,當你設置的原這裏選擇索引:

rblContentTypesGetAll.SelectedIndex = rblContentTypesGetAll.Items.IndexOf(rblContentTypesGetAll.Items.FindByValue(((siteParams)Session["myParams"]).DefaultContentType.ToLower())); 

它可能找不到值,然後將其設置爲「-1」。然後,如果您沒有選擇頁面上的單選按鈕,您將不會獲得選定的值。

我想這和它似乎罰款:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      //Pretending to call your stored proc.. 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("contentType"); 
      dt.Columns.Add("description"); 
      dt.Columns.Add("ID"); 
      dt.AcceptChanges(); 
      for (int i = 0; i < 6; i++) 
      { 
       DataRow dr = dt.NewRow(); 
       dr["contentType"] = "cnt" + i.ToString(); 
       dr["description"] = "desc" + i.ToString(); 
       dr["ID"] = i.ToString(); 
       dt.Rows.Add(dr); 
      } 


      //Populate Radio button list 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       rblContentTypesGetAll.Items.Add(new ListItem(dt.Rows[i]["contentType"].ToString() + " - " + dt.Rows[i]["description"].ToString(), 
        dt.Rows[i]["ID"].ToString())); 
      } 

      //Set Default Selected Item by Value 
      rblContentTypesGetAll.SelectedIndex = 0; //this could be -1 also 
     } 

     lblMessage.Text = "rblContentTypesGetAll.SelectedValue :" + rblContentTypesGetAll.SelectedValue; 

    } 
+0

太奇怪了......我覺得這個問題很簡單。我試過了,它仍然提交了一個空白值。該列表填充並且值有價值。我不知道是什麼導致名單消失。 – balexander 2011-02-23 12:57:57

+0

所選的值在按下提交之後立即刪除,之後它甚至會穿過 – balexander 2011-02-24 00:34:23

+0

後面的代碼。必須缺少一些東西,嘗試將主頁面和任何未顯示的javascript取消,上面的代碼可以正常工作,以至於有一些外部代碼與無線電按鈕列表。 – RussHWalker 2011-02-24 01:09:00