GridView 和 OnRowDataBound事件显示图片, 没什么技术含量, 只是作为自己的一点总结:
前台只需要简单的放一个GridView控件,我做的是一个显示fax历史的一个例子,只要用OnRowDataBound 现实图片,如果需要可以更改现实代码。
后台的OnRowDataBound 如下:
我没有在前台绑定第三列的值,而在OnRowDataBound 事件里给他付值,这样可以比较容易实现在前台显示图片。另外通过(object)e.Row.DataItem可以比较容易实现绑定对象的cast.
前台只需要简单的放一个GridView控件,我做的是一个显示fax历史的一个例子,只要用OnRowDataBound 现实图片,如果需要可以更改现实代码。
1 <asp:GridView ID="faxHistoryList" runat="server" AutoGenerateColumns="False" AllowSorting="True"
2 EnableViewState="true"OnRowCommand="faxHistoryList_RowCommand" OnRowDataBound="faxHistoryList_RowDataBound">
4 <Columns>
5 <asp:TemplateField>
6 <HeaderTemplate>
7 <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkAll_CheckedChanged"></asp:CheckBox>
8 </HeaderTemplate>
9 <ItemTemplate>
10 <asp:CheckBox ID="chkFax" runat="server" ></asp:CheckBox>
11 </ItemTemplate>
12 </asp:TemplateField>
13 <asp:BoundField HeaderText="Id" DataField="Id" Visible="false"/>
14 <asp:BoundField HeaderText="Status" />
15 <asp:BoundField HeaderText="Number Faxed To" DataField="RecipientFaxNumber" />
16 <asp:BoundField HeaderText="Sender" DataField="SenderName" />
17 <asp:BoundField HeaderText="SenderTo" DataField="RecipientName" />
18 <asp:BoundField HeaderText="Patient" DataField="PatientName" />
19 <asp:BoundField HeaderText="SenderDate" DataField="CreateDateTime" />
20 <asp:TemplateField HeaderText="Preview">
21 <ItemTemplate>
22 <asp:LinkButton ID="Preview" runat="server" CommandName="Preview" Text = "Print/Preview"></asp:LinkButton>
23 </ItemTemplate>
24 </asp:TemplateField>
25 </Columns>
26 <EmptyDataTemplate>
27 <asp:Localize runat="server" ID="locEmptyResultMessage" Text="There is no record"></asp:Localize>
28 </EmptyDataTemplate>
29 </asp:GridView>
2 EnableViewState="true"OnRowCommand="faxHistoryList_RowCommand" OnRowDataBound="faxHistoryList_RowDataBound">
4 <Columns>
5 <asp:TemplateField>
6 <HeaderTemplate>
7 <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkAll_CheckedChanged"></asp:CheckBox>
8 </HeaderTemplate>
9 <ItemTemplate>
10 <asp:CheckBox ID="chkFax" runat="server" ></asp:CheckBox>
11 </ItemTemplate>
12 </asp:TemplateField>
13 <asp:BoundField HeaderText="Id" DataField="Id" Visible="false"/>
14 <asp:BoundField HeaderText="Status" />
15 <asp:BoundField HeaderText="Number Faxed To" DataField="RecipientFaxNumber" />
16 <asp:BoundField HeaderText="Sender" DataField="SenderName" />
17 <asp:BoundField HeaderText="SenderTo" DataField="RecipientName" />
18 <asp:BoundField HeaderText="Patient" DataField="PatientName" />
19 <asp:BoundField HeaderText="SenderDate" DataField="CreateDateTime" />
20 <asp:TemplateField HeaderText="Preview">
21 <ItemTemplate>
22 <asp:LinkButton ID="Preview" runat="server" CommandName="Preview" Text = "Print/Preview"></asp:LinkButton>
23 </ItemTemplate>
24 </asp:TemplateField>
25 </Columns>
26 <EmptyDataTemplate>
27 <asp:Localize runat="server" ID="locEmptyResultMessage" Text="There is no record"></asp:Localize>
28 </EmptyDataTemplate>
29 </asp:GridView>
后台的OnRowDataBound 如下:
1 protected void faxHistoryList_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 if (e.Row.RowType == DataControlRowType.DataRow)
4 {
5 LinkButton lkbPreview = (LinkButton)e.Row.FindControl("Preview");//.Cells[(int)DisplayActionPlan.DisplayActionPlanColumn.deletebutton].Controls[0];
6 Fax item = (Fax)e.Row.DataItem;
7 lkbPreview.CommandArgument = item.Id.ToString();
8
9 InterventionContracts.Fax fax = (InterventionContracts.Fax)e.Row.DataItem;
10 System.Text.StringBuilder sb = new System.Text.StringBuilder();
11 switch (int.Parse(fax.Status))
12 {
13 case 0:
14 sb.Append("");
15 sb.Append(" Pending");
16 break;
17 case 1:
18 sb.Append("<img class=\"imgAlignTextBottom imgFormulary\" src=\"/images/arrow_outline.gif\" >");
19 sb.Append(" In-Progress");
20 break;
21 case 2:
22 sb.Append("<img class=\"imgAlignTextBottom imgFormulary\" src=\"/images/check-done.gif\" >");
23 sb.Append(" Sent");
24 break;
25 case 3:
26 sb.Append("<img class=\"imgAlignTextBottom imgFormulary\" src=\"/images/alert.gif\" >");
27 sb.Append(" Support Viewing");
28 break;
29 }
30 e.Row.Cells[2].Text = sb.ToString();
31 }
32 }
2 {
3 if (e.Row.RowType == DataControlRowType.DataRow)
4 {
5 LinkButton lkbPreview = (LinkButton)e.Row.FindControl("Preview");//.Cells[(int)DisplayActionPlan.DisplayActionPlanColumn.deletebutton].Controls[0];
6 Fax item = (Fax)e.Row.DataItem;
7 lkbPreview.CommandArgument = item.Id.ToString();
8
9 InterventionContracts.Fax fax = (InterventionContracts.Fax)e.Row.DataItem;
10 System.Text.StringBuilder sb = new System.Text.StringBuilder();
11 switch (int.Parse(fax.Status))
12 {
13 case 0:
14 sb.Append("");
15 sb.Append(" Pending");
16 break;
17 case 1:
18 sb.Append("<img class=\"imgAlignTextBottom imgFormulary\" src=\"/images/arrow_outline.gif\" >");
19 sb.Append(" In-Progress");
20 break;
21 case 2:
22 sb.Append("<img class=\"imgAlignTextBottom imgFormulary\" src=\"/images/check-done.gif\" >");
23 sb.Append(" Sent");
24 break;
25 case 3:
26 sb.Append("<img class=\"imgAlignTextBottom imgFormulary\" src=\"/images/alert.gif\" >");
27 sb.Append(" Support Viewing");
28 break;
29 }
30 e.Row.Cells[2].Text = sb.ToString();
31 }
32 }
我没有在前台绑定第三列的值,而在OnRowDataBound 事件里给他付值,这样可以比较容易实现在前台显示图片。另外通过(object)e.Row.DataItem可以比较容易实现绑定对象的cast.