2014-10-17 23 views
0

从下拉列表中选择学生ID时,我必须在我的网页上显示学生姓名和图像。该图像以db的二进制格式存储。我如何检索图像并在图像框中显示。下面给出的代码仅显示学生的名字和姓氏。如何在不使用http通用处理程序页面的情况下显示图像?请帮帮我。不使用通用http处理程序显示二进制图像

代码:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DataSet1TableAdapters.TextBoxTableTableAdapter tx; 
     tx = new DataSet1TableAdapters.TextBoxTableTableAdapter(); 
     DataTable dt = new DataTable(); 
     dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue)); 

     foreach (DataRow row in dt.Rows) 
     { 
      TextBox1.Text = (row["FirstName"].ToString()); 
      TextBox2.Text = (row["SecondName"].ToString()); 
     } 
    } 

SQL查询:

SELECT FirstName, SecondName, StudentImage FROM TextBoxTable WHERE (Id = @Id) 

.aspx的来源:

<div> 
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"> 
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
<asp:Image ID="Image1" runat="server" /> 
</div> 

数据库:

enter image description here

+0

你不能工作。至少你不能轻松。有一种方法可以将图像嵌入到网页中:http://www.websiteoptimization.com/speed/tweak/inline-images/但是编写您不想写的处理程序会非常容易。如果你使用的是MVC和WebAPI,它会更容易。 – 2014-10-17 05:02:55

+0

@IanMercer:好的。那么我如何为上面的代码添加通用的http处理程序。我想通过从下拉列表中选择ID来显示图像。你能否为此代码发布示例代码。 – Vipin 2014-10-17 05:14:35

回答

1

代码

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DataSet1TableAdapters.TextBoxTableTableAdapter tx; 
    tx = new DataSet1TableAdapters.TextBoxTableTableAdapter(); 
    DataTable dt = new DataTable(); 
    dt = tx.GetstudData(int.Parse(DropDownList1.SelectedValue)); 
    foreach (DataRow row in dt.Rows) 
    { 
     TextBox1.Text = (row["FirstName"].ToString()); 
     TextBox2.Text = (row["SecondName"].ToString()); 
     byte[] barrImg = (byte[])(row["StudentImage"].ToString()); 
     string base64String = Convert.ToBase64String(barrImg , 0, barrImg.Length); 
     Image1.ImageUrl = "data:image/png;base64," + base64String; 
    } 
}' 

我认为这段代码会为你

+0

使用您的代码时出现错误。错误是不能转换类型'字符串'字节[]' – Vipin 2014-10-17 05:37:58

+0

使用此链接http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array将字符串转换为字节数组 – 2014-10-17 06:05:04

相关问题