2012-11-01 49 views
0

我正在使用GridView並且在其中我有四列:labelID,fName,lNameGradeGrade是一個簡單的通過或失敗Radiobuttonlist。數據更新後,如果用戶通過或失敗,我希望它在下一次重新加載時提取數據以顯示選定的值。下面是代碼:這是無效的,因爲它不存在於項目列表中的選定值

<asp:TemplateField> 
      <ItemTemplate> 
      <asp:RadioButtonList ID="rblChoices" runat="server" OnSelectedIndexChanged="rblChoices_SelectedIndexChanged" Text='<%# Eval("Grade") %>'> 
       <asp:ListItem Value="Pass" Text="Pass"></asp:ListItem> 
       <asp:ListItem Value="Fail" Text="Fail"></asp:ListItem> 
      </asp:RadioButtonList> 
      </ItemTemplate> 
</asp:TemplateField> 

C#代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     BindData(); 
    } 
} 
private void BindData() 
{ 
    string connectiongString = "Data Source=WSCJTCSQ1;Initial Catalog=LiquorStore;Integrated Security=True"; 
    SqlConnection myConnection = new SqlConnection(connectiongString); 
    SqlDataAdapter ad = new SqlDataAdapter("SELECT id, firstname, lastname, nickname, Grade FROM Company", myConnection); 
    DataSet ds = new DataSet(); 
    ad.Fill(ds); 
    gvUsers.DataSource = ds; 
    gvUsers.DataBind(); 
} 

預先感謝您!

+0

什麼檔次呢SQL返回?他們都是「通過」還是「失敗」? – Andomar

+0

嗨Andomar, SQL等級無論是通過或失敗是。是否可以在加載時通​​過從SQL中提取值來選擇單選按鈕? – vadim

+0

這個等級也可以是空的嗎?如果是的話,那麼在C#中的值將是'DBNull.Value'。如果您將Eval(「Grade」)替換爲Eval(「Grade」)作爲字符串,會有幫助嗎? – Andomar

回答

2

您必須使用GridView的RowDataBound事件此

HTML

<asp:GridView runat="server" ID="gvUsers" OnRowDataBound="gvUsers_RowDataBound" AutoGenerateColumns="False"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <%# Eval("Name") %> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:RadioButtonList ID="rblChoices" runat="server"> 
        <asp:ListItem Value="Pass" Text="Pass"></asp:ListItem> 
        <asp:ListItem Value="Fail" Text="Fail"></asp:ListItem> 
       </asp:RadioButtonList> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

C#代碼

一個非常簡單的公司類 - Company.cs

public class Company 
{ 
    public string Name { get; set; } 
    public string Grade { get; set; } 
} 

.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
{ 

    List<Company> companies = new List<Company>() 
     { 
      new Company(){ Name = "Toyota", Grade = "Pass"}, 
      new Company(){ Name = "Form", Grade = "Fail"} 
     }; 

    gvUsers.DataSource = companies; 
    gvUsers.DataBind(); 
} 


protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      if (e.Row.DataItem != null) 
      { 
       string grade = DataBinder.Eval(e.Row.DataItem, "Grade") as string; 
       if (!string.IsNullOrEmpty(grade)) 
       { 
        RadioButtonList radio = e.Row.FindControl("rblChoices") as RadioButtonList; 
        radio.Items.FindByValue(grade).Selected = true; 
        //You can use this to select as well - see comments from Andomar 
        //radio.SelectedValue = grade; 
       } 
      } 
     } 
    } 

輸出

enter image description here

+0

用'Text ='<%#Eval(「Grade」)%>'>'綁定會簡單得多!這不行嗎? – Andomar

+0

我在這行代碼中遇到了一個錯誤:'string grade = DataBinder.Eval(e.Item.DataItem,「Grade」);' – vadim

+0

RadioButtonList根本沒有任何Text屬性。 – Tariqulazam

相關問題