2013-10-28 2 views
0

서버 측의 RadioButtonList에 "onclick"속성을 추가하지만 버튼을 클릭하면왜 RadioButtonList 속성이 포스트 백을 잃었습니까?

포스트 백을 제출하면 속성이 손실됩니다.

여기

를 해결하기 위해 좀 도와주세요 것은

=== HTML의 ===

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
     <title></title> 
    </head> 
    <body> 
     <form id="form1" runat="server"> 
      <div> 
       <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
        RepeatLayout="Flow" RepeatDirection="Horizontal" /> 
       <br /> 
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
       <br /> 
       <asp:Button ID="Button1" runat="server" Text="Button" /> 
     </form> 
    </body> 
</html> 

======= ASP.NET ==== 내 샘플 코드 ====

protected override void LoadViewState(object savedState) 
{ 
    if (savedState != null) 
    { 
     object[] myState = (object[])savedState; 

     // restore base first 
     if (myState[0] != null) 
     { 
      base.LoadViewState(myState[0]); 
     } 

     Int32 i = 1; 
     foreach (ListItem li in this.Items) 
     { 
      // loop through and restore each style attribute 
      foreach (string[] attribute in (string[][])myState[i++]) 
      { 
       li.Attributes[attribute[0]] = attribute[1]; 
      } 
     } 
    } 
} 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     SetList(); 
     TextBox1.TextMode = TextBoxMode.Password; 
     TextBox1.Attributes.Add("value", "123"); 
    } 
} 

void SetList() 
{ 
    List<ListItem> items = new List<ListItem>(); 
    items.Add(new ListItem { Text = "Yes", Value = "1" }); 
    items.Add(new ListItem { Text = "No", Value = "0" }); 

    RadioButtonList1.DataSource = items; 
    RadioButtonList1.DataBind(); 

    for (int i = 0; i < RadioButtonList1.Items.Count; i++) 
    { 
     RadioButtonList1.Items[i].Attributes.Add("onclick", "Hello()"); 
    } 
} 

protected override object SaveViewState() 
{ 
    // create object array for Item count + 1 
    object[] allStates = new object[this.Items.Count + 1]; 

    // the +1 is to hold the base info 
    object baseState = base.SaveViewState(); 
    allStates[0] = baseState; 

    Int32 i = 1; 
    // now loop through and save each Style attribute for the List 
    foreach (ListItem li in this.Items) 
    { 
     Int32 j = 0; 
     string[][] attributes = new string[li.Attributes.Count][]; 
     foreach (string attribute in li.Attributes.Keys) 
     { 
      attributes[j++] = new string[] { attribute, li.Attributes[attribute] }; 
     } 
     allStates[i++] = attributes; 
    } 
    return allStates; 
} 

답변

1

속성은 viewstate를 통해 유지되지 않습니다. 모든 포스트 백에서 다시로드하거나 직접 viewstate를 사용하여 유지하거나 다른 메커니즘 (데이터베이스, 세션 등)을 통해 작업을 지속해야합니다.

관련 문제