是否有任何方法通過datasource/databind填充複選框列表,然後實際添加圖像到每個複選框?我知道如何分開做,但似乎無法弄清楚如何讓他們一起工作。綁定後將圖片添加到複選框列表
本質上,我將有一個複選框列表的學生教師可供選擇,我已被要求把複選框旁邊的每個學生的照片。
目前我獲取圖片的方式是我有一個網頁表單,它在網址中提取一個ID,然後將數據庫中的blob從數據庫中抓取,然後將二進制文件轉換爲縮略圖圖像,保存到本地然後重定向到它。然後當我想要在不同的webform上獲取圖像時,我只需要使用Response.Redirect("imgs.aspx?ID=[Student Id]")
,它會放入學生的小圖像,是否有任何方法來修改複選框列表,以便我可以調用imgs webform並顯示學生在複選框旁邊的圖像?背後imgs.aspx
代碼:
protected void Page_Load(object sender, EventArgs e)
{
var stuId = Request.QueryString["ID"];
if (stuId.Length <= 0)
{
stuId = "100097645"; // If no ID number sent, display default 'image not available' thumbnail.
}
var con = new SqlConnection
{
ConnectionString =
removed for security reasons
};
con.Open();
var sqlCommand = new SqlCommand("Select BINARY_OBJECT FROM BLOBS WHERE OWNER_REF=" + stuId, con);
var reader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
reader.Read();
var byteArray = (byte[])reader["BINARY_OBJECT"];
var mstream = new System.IO.MemoryStream(byteArray, 0, byteArray.Length);
var dbImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(byteArray));
var thumbnailImage = dbImage.GetThumbnailImage(100, 100, null, new IntPtr());
thumbnailImage.Save(mstream, dbImage.RawFormat);
var thumbnailByteArray = new byte[mstream.Length];
mstream.Position = 0;
mstream.Read(thumbnailByteArray, 0, Convert.ToInt32(mstream.Length));
Response.Clear();
Response.BinaryWrite(thumbnailByteArray);
}
代碼我迄今顯示圖像:
private void StudentSelected()
{
string query = "Select distinct (Convert(varchar, PERSON_CODE) + '|' + FORENAME + ' ' + SURNAME) as StudentName, (Convert(varchar, PERSON_CODE) + '|' + FORENAME + '|' + SURNAME) as StudentValue From people where (FORENAME = '" + stuforename + "') and (SURNAME = '" + stusurname + "') and (Convert(varchar,PERSON_CODE) = '" + stuid + "')";
StudentCheckBoxData.Merge(GetData(query));
cbSelection.DataSource = StudentCheckBoxData;
cbSelection.DataTextField = "StudentName";
cbSelection.DataValueField = "StudentValue";
cbSelection.DataBind();
try
{
foreach (ListItem checkBox in cbSelection.Items)
{
checkBox.Text += string.Format("<img src = \"{0}\" /> ", GetImageUrl(checkBox.Text.Split('|')[0]));
}
}
catch
{
//Display Error Here
}
}
private string GetImageUrl(string id)
{
return string.Format("http://bceforms/Contact/imgs.aspx?ID=", id);
}
目前最大的問題是,它顯示了邊界,但出現的圖像一個縮略圖沒有找到圖像,當我之前有過這個問題,因爲圖像還沒有被數據綁定,但是當我在複選框列表上使用.databind();
,因爲它顯然是原始數據源沒有圖像,它只是刪除圖片
任何幫助將apprecieated
工程太棒了!謝謝! –
歡迎你。非常樂意提供幫助。 –