[英]unable to bind string values which are separated with comma to a check box list
我正在編輯語言,我知道gridview中的復選框,我具有字符串值english,spanish。 在單擊“編輯”按鈕時,應選中復選框列表項。
我想獲取gridview行列的詳細信息,並希望綁定在一個復選框列表中。 我已經綁定了所有語言復選框。 我需要選中已處於選中狀態的復選框。
網格視圖
<asp:TemplateField HeaderText="Languages">
<EditItemTemplate>
<asp:TextBox ID="txtLanguages" runat="server" Text='<%# Bind("Languages") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblLanguages" runat="server" Text='<%# Bind("Languages") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
private void bindLanguages()
{
string Languages = "English,Spanish";
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from tbl_Languages";
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ListItem item = new ListItem();
item.Text = sdr["LanguageName"].ToString();
item.Value = sdr["LanguageId"].ToString();
// item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
chkLanguages.Items.Add(item);
}
}
conn.Close();
}
}
}
public void Bindemployeedetails(string str)
{
DataTable dt = new DataTable();
string Languages = dt.Rows[0]["Languages"].ToString();//English,Hindi
string[] words = Languages.Split(',');
foreach (string word in words)
{
foreach (GridViewRow row in ChkLanguages.Items )
{
if (row.Cells[1].Text == word)
{
CheckBox chkRow = word.ToString();
chkRow.Checked = true;
}
}
}
}
也許嘗試拆分?
string[] words = Languages.Split(',');
foreach (string word in words)
{
foreach (GridViewRow row in gdvHealthProblem.Rows)
{
if (row.Cells[1].Text == word)
{
CheckBox chkRow = row.Cells[0].FindControl("chkTableHealthProblem") as CheckBox;
chkRow.Checked = true;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.