[英]Dynamically display images in a grid view based on the column values in asp.net
我正在將表數據綁定到網格視圖,我想在表中添加列狀態,並且該列中的值將被發送消息或保存消息。 我想基於發送或保存消息的值顯示圖像。 我該如何動態地做到這一點。
DataTable dt = new DataTable();
try
{
string MysqlStatement = "SELECT MsgID, MsgText, RespondBy, ExpiresBy, OwnerName FROM tbl_message WHERE tbl_user_tbl_organisation_OrganisationID = @Value1";
MySqlCommand sqlCmd = new MySqlCommand(MysqlStatement, connectionString);
sqlCmd.Parameters.AddWithValue("@Value1", newOrgID);
MySqlDataAdapter sqlDa = new MySqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
Grid_Messagetable.DataSource = dt;
Grid_Messagetable.DataBind();
Grid_Messagetable.Columns[2].Visible = false;
}
else
{
}
<asp:GridView ID="Grid_Messagetable" runat="server" AllowPaging="True" SelectedIndex="0"
DataKeyNames="MsgID" ShowHeaderWhenEmpty="True" OnRowDeleting="MsgTable_RowDeleting"
OnRowEditing="MsgTable_RowEditing" AutoGenerateColumns="False" BorderStyle="Double"
Width="537px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="Edit controls"
ButtonType="Image" DeleteImageUrl="~/Styles/Images/Delete.gif" EditImageUrl="~/Styles/Images/Edit.gif" />
<asp:TemplateField HeaderText="DashBoard">
<ItemTemplate>
<asp:ImageButton ID="imgbtn_ViewDashBoard" ImageUrl="Styles/Images/dash.png" Enabled="True"
Width="50" runat="server" PostBackUrl='<%# Eval("MsgID", "ResponseMetric.aspx?MsgID={0}") %>'
Text='Send'></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="MsgID" HeaderText="MsgID" ReadOnly="True" SortExpression="MsgID" />
<asp:BoundField DataField="MsgText" HeaderText="MsgText" ReadOnly="True" SortExpression="MsgText" />
<asp:BoundField DataField="RespondBy" HeaderText="RespondBy" ReadOnly="True" SortExpression="RespondBy" />
<asp:BoundField DataField="ExpiresBy" HeaderText="ExpiresBy" ReadOnly="True" SortExpression="ExpiresBy" />
<asp:BoundField DataField="OwnerName" HeaderText="OwnerName" ReadOnly="True" SortExpression="OwnerName" />
<asp:ImageField DataImageUrlField="Sent" HeaderText="Status"
NullImageUrl="~/Styles/Images/White.gif">
</asp:ImageField>
</Columns>
</asp:GridView>
您可以這樣創建模板列字段:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:ImageButton ID="img" runat="server" ImageUrl='<%# (Eval("Status") == "Sent") ? "messagesent.png" : "messagesaved.png" %>' />
</ItemTemplate>
</asp:TemplateField>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.