How To Add a images in Gridview as a Teplate field..

Jul 29 2008 6:16 AM

Hi Friends..

I have a one Gridview with four fields like select,Name,Sample ID, Image

select fields having a CheckBox, Name is the Bound fied which is access the data from XMLDatasource, SampleID is Template field is having TextBox,and Image is also template field Having Image...There is one Button called Generate SampleID, when i click the button control i want display the SampleID (ex: take todays date formate as a SampleID), image (like Barcode) at a time...one thing the Sample id is Shown in particular Barcode image..

here i am sending my HTML Code,

<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="BarcodeGen.aspx.cs" Inherits="BarcodeGen" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

<meta http-equiv="refresh" content="300url=http://ktwo-157/PCScreen%20on%20ktwo-157%20on%20ktwo-93/BarcodeGen.aspx"/>

 

<link href="gridview.css" rel="stylesheet" type="text/css" />

<script language="javascript" type="text/javascript">

//Page Refreshing

// //Function called during body onload

//function body_onload(){

//window.setTimeout(RefreshPage, 300);

//}

////Function to refresh the page

//function RefreshPage(){

//document.forms(0).submit();

//}

 

function SelectAllCheckboxes(spanChk){

// Added as ASPX uses SPAN for checkbox

var oItem = spanChk.children;

var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];

xState=theBox.checked;

elm=theBox.form.elements;

for(i=0;i<elm.length;i++)

if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)

{

//elm[i].click();

if(elm[i].checked!=xState)

elm[i].click();

//elm[i].checked=xState;

}

}

</script>

<%--<script type="text/javascript">

var TotalChkBx;

var Counter;

window.onload = function()

{

//Get total no. of CheckBoxes in side the GridView.

TotalChkBx = parseInt('<%= this.gvCheckboxes.Rows.Count %>');

//Get total no. of checked CheckBoxes in side the GridView.

Counter = 0;

}

function HeaderClick(CheckBox)

{

//Get target base & child control.

var TargetBaseControl =

document.getElementById('<%= this.gvCheckboxes.ClientID %>');

var TargetChildControl = "chkBxSelect";

//Get all the control of the type INPUT in the base control.

var Inputs = TargetBaseControl.getElementsByTagName("input");

//Checked/Unchecked all the checkBoxes in side the GridView.

for(var n = 0; n < Inputs.length; ++n)

if(Inputs[n].type == 'checkbox' &&

Inputs[n].id.indexOf(TargetChildControl,0) >= 0)

Inputs[n].checked = CheckBox.checked;

//Reset Counter

Counter = CheckBox.checked ? TotalChkBx : 0;

}

function ChildClick(CheckBox, HCheckBox)

{

//get target control.

var HeaderCheckBox = document.getElementById(HCheckBox);

//Modifiy Counter;

if(CheckBox.checked && Counter < TotalChkBx)

Counter++;

else if(Counter > 0)

Counter--;

//Change state of the header CheckBox.

if(Counter < TotalChkBx)

HeaderCheckBox.checked = false;

else if(Counter == TotalChkBx)

HeaderCheckBox.checked = true;

}

</script>--%>

</head>

<body>

<form id="form1" runat="server">

<div>

<table cellspacing="5" style="width: 190px; border-right: black 1px solid; border-top: black 1px solid; border-left: black 1px solid; border-bottom: black 1px solid;">

<tr>

<td style="width: 97px; height: 21px;">

</td>

</tr>

<tr>

<td style="width: 97px">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="grid-view"

DataSourceID="XmlDataSource1" Width="190px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="5" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">

<Columns>

<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:CheckBox ID="chkbox1" runat="server" />

&nbsp;<br />

</ItemTemplate>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Top" Width="20px" BorderColor="Black" BorderWidth="1px" />

<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="20px" BorderColor="Black" BorderWidth="1px" />

<HeaderTemplate>

<asp:CheckBox ID="chkBxHeader" onclick="javascript:SelectAllCheckboxes(this);" runat="server" />

</HeaderTemplate>

<FooterStyle BorderColor="Black" BorderWidth="1px" />

</asp:TemplateField>

<asp:BoundField DataField="MRN" HeaderText="MRN" SortExpression="MRN" >

<ItemStyle BorderColor="Black" BorderWidth="1px" Height="40px" />

<HeaderStyle BorderColor="Black" BorderWidth="1px" />

<FooterStyle BorderColor="Black" BorderWidth="1px" />

</asp:BoundField>

<asp:TemplateField HeaderText="SID">

<ItemTemplate>

<asp:TextBox ID="TextBox1" ReadOnly="true" runat="server" Width="150px" />

</ItemTemplate>

<ItemStyle BorderColor="Black" BorderWidth="1px" />

<HeaderStyle BorderColor="Black" BorderWidth="1px" />

<FooterStyle BorderColor="Black" BorderWidth="1px" />

</asp:TemplateField>

<asp:TemplateField HeaderText="Image">

<ItemTemplate>

<asp:Image ID="Image1" runat="server" Height="100px" Width="200px" />

</ItemTemplate>

<ItemStyle BorderColor="Black" BorderWidth="1px" />

<HeaderStyle BorderColor="Black" BorderWidth="1px" />

<FooterStyle BorderColor="Black" BorderWidth="1px" />

</asp:TemplateField>

</Columns>

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<RowStyle BackColor="#EFF3FB" CssClass="normal" />

<EditRowStyle BackColor="#2461BF" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" CssClass="alternate" />

</asp:GridView>

<%--<asp:GridView ID="gvCheckboxes" runat="server"

AutoGenerateColumns="False" OnRowCreated="gvCheckboxes_RowCreated" Width="179px" DataSourceID="XmlDataSource1" CellPadding="4" CssClass="grid-view" ForeColor="#333333" GridLines="None">

<Columns>

<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:CheckBox ID="chkBxSelect" runat="server" />

</ItemTemplate>

<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />

<HeaderTemplate>

<asp:CheckBox ID="chkBxHeader"

onclick="javascript:HeaderClick(this);" runat="server" />

</HeaderTemplate>

</asp:TemplateField>

<asp:BoundField DataField="MRN" HeaderText="MRN" SortExpression="MRN" />

<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />

</Columns>

<RowStyle BackColor="#EFF3FB" />

<AlternatingRowStyle BackColor="White" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#2461BF" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

</asp:GridView>--%>

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Barcode.xml">

</asp:XmlDataSource>

</td>

</tr>

<tr>

<td style="width: 97px; height: 14px;" align="left" valign="top">

&nbsp;&nbsp;

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="BarCode Gen" OnClientClick="window.opener.location.reload();" />

</td>

</tr>

<tr>

<td align="left" style="width: 97px; height: 14px" valign="top">

</td>

</tr>

</table>

</div>

</form>

</body>

</html>

Code Behind i used C#....

int i = 1;

int n = GridView1.PageSize;

for (int k = 0; k < n; k++)

{

//foreach (GridViewRow row in GridView1.Rows)

//{

//CheckBox chkItem = (CheckBox)row.FindControl("chkbox1");

//TextBox txt = (TextBox)row.FindControl("TextBox1");

//Image img = (Image)row.FindControl("Image1");

CheckBox chkItem = (CheckBox)GridView1.Rows[k].Cells[0].FindControl("chkbox1");

TextBox txt = (TextBox)GridView1.Rows[k].Cells[2].FindControl("TextBox1");

Image img = (Image)GridView1.Rows[k].Cells[3].FindControl("Image1");

if (chkItem.Checked)

{

string c_date = DateTime.Now.ToString();

string mil_second = DateTime.Now.Millisecond.ToString();

c_date = c_date.Replace("/", "");

c_date = c_date.Replace(":", "");

c_date = c_date.Replace("PM", "");

c_date = c_date.Replace("AM", "");

c_date = c_date.Replace(" ", "");

c_date = c_date + i;

txt.Text = c_date;

string[] s ={ "BareCode" };

System.Drawing.Image img1;

img1 = Care21.Components.BarCode.Engine.Code128Rendering.MakeBarcodeImage(txt.Text, 1, true, s);

//Image img = (Image)row.FindControl("Image1");

System.Drawing.Image ImgObj;

BarcodeGenerator.Barcode BarcodeGenerate = new BarcodeGenerator.Barcode();

ImgObj = BarcodeGenerate.Code128(2, "BarCode", txt.Text, true);

ImgObj.Save(@"D:\PCScreen on ktwo-157 on ktwo-93\images\BarcodImage.bmp");

img.ImageUrl = @"~\images\BarcodImage.bmp";

}

i++;

// }

}

....

its working fine, but it will take last data value for all the images....

Just Tell Me How Generate ....

Advance Thanks ..Hope you people getting my problem...

Basu Malipatil


Answers (5)