我正在使用GridView
並且在其中我有四列:labelID
,fName
,lName
和Grade
。 Grade
是一個簡單的通過或失敗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();
}
預先感謝您!
什麼檔次呢SQL返回?他們都是「通過」還是「失敗」? – Andomar
嗨Andomar, SQL等級無論是通過或失敗是。是否可以在加載時通過從SQL中提取值來選擇單選按鈕? – vadim
這個等級也可以是空的嗎?如果是的話,那麼在C#中的值將是'DBNull.Value'。如果您將Eval(「Grade」)替換爲Eval(「Grade」)作爲字符串,會有幫助嗎? – Andomar