我想知道是否有人可以帮助我使用此代码。
我想要做的是从数据库中读取记录并在gridview上显示它们。不过我网格的最后一列是TemplateField
,我点击后会显示ImageButton
来执行某些操作。
我的问题是:
仅当网格的特定单元格为值ImageButton
时,如何显示true
。
当值为ImageButton
时,我试图将false
设置为false,但它无法正常工作
请帮助
private void BindCustomDateBookings()
{
try
{
String queryCustomDateBookings = "Select * from bookings";
//Creates a connection to the database
SqlConnection connectionViewBookings = new SqlConnection(connectionString);
//Query builder
SqlDataAdapter dataAdapterViewBookings = new SqlDataAdapter(queryCustomDateBookings, connectionViewBookings);
dataAdapterViewBookings.SelectCommand.Parameters.AddWithValue("@customDate", customDate);
//Create the data that will hold the result of DB
DataSet dataSetViewTodaysBookings = new DataSet();
//open, save results and close connection
connectionViewBookings.Open();
dataAdapterViewBookings.Fill(dataSetViewTodaysBookings);
connectionViewBookings.Close();
//fill the grid with results
GridViewCustomDate.DataSource = dataSetViewTodaysBookings.Tables[0];
GridViewCustomDate.DataBind();
}
catch (SqlException SqlExceptionTomorrow)
{
Response.Write(@"<SCRIPT LANGUAGE=""JavaScript"">alert('Could not connect to the database This date bookings')</SCRIPT>");
}
}//Ends Bind Custom Date bookings
网格视图具有以下编码
<asp:GridView ID="GridViewCustomDate" runat="server" AutoGenerateColumns="False" CssClass="viewBookingTable" GridLines="None"
HeaderStyle-CssClass="gridViewHeader">
<HeaderStyle CssClass="gridViewHeader" />
<RowStyle CssClass="GridViewRowStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle " />
<Columns>
<asp:BoundField DataField="bookingID" ItemStyle-CssClass="displayGridViewID"
HeaderStyle-CssClass="displayGridViewID" >
<HeaderStyle CssClass="displayGridViewID" />
<ItemStyle CssClass="displayGridViewID" />
</asp:BoundField>
<asp:BoundField DataField="visitorName" HeaderText="Visitor Name" />
<asp:BoundField DataField="contactNo" HeaderText="contactNo" />
<asp:BoundField DataField="hostName" HeaderText="Host Name" />
<asp:BoundField DataField="roomName" HeaderText="Room Name" />
<asp:BoundField DataField="timeIn" HeaderText="Time In" />
<asp:BoundField DataField="smartCardNo" HeaderText="Smart Card No" />
<asp:TemplateField HeaderText="Required Access">
<ItemTemplate>
<asp:ImageButton ID="btnGetRequiredAccessCustomDate" runat="server"
ImageUrl="~/images/buttons/clickMe.png"
onclick="btnGetRequiredAccessCustomDate_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:1)
您可以将asp:ImageButton的Visible属性绑定到数据集中的布尔值。 e.g。
<asp:ImageButton ID="btnGetRequiredAccessCustomDate" runat="server"
Visible=<%# Eval("ShowImage") %>
ImageUrl="~/images/buttons/clickMe.png"
onclick="btnGetRequiredAccessCustomDate_Click" />
答案 1 :(得分:1)
使用gridview中的OnDataBound
属性。您的gridview初始化将是,
<asp:GridView ID="GridViewCustomDate" runat="server" AutoGenerateColumns="False" CssClass="viewBookingTable" GridLines="None" OnDataBound="GridViewCustomDate_DataBound"
HeaderStyle-CssClass="gridViewHeader">
<HeaderStyle CssClass="gridViewHeader" />
<RowStyle CssClass="GridViewRowStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle " />
在后面的代码中处理数据绑定事件,如下所示。
public void GridViewCustomDate_DataBound(object send, EventArgs e)
{
for (int i = 0; i < GridViewCustomDate.Rows.Count; i++)
{
if( condition) // the condition for which you want to check true of false
GridViewCustomDate.Rows[i].Cells[7].Visible = false; //the cells[7] indicates the last cell. replace with appropriate cell number.
}
}
DataBound 方法将在gridview的数据绑定之后以及在页面上呈现之前调用。因此,如果if
条件失败,则单元格将为空。如果条件满足,则带有按钮的单元格将显示。
基本上,您为所有行渲染按钮,并在databind
之后动态隐藏它。
答案 2 :(得分:0)
使用数据绑定和转换器?
<asp:ImageButton ID="btnGetRequiredAccessCustomDate" runat="server"
Visibility={Binding FieldThatControlsVisibility, Converter={StaticResource ConverterForFieldThatReturnsVisibility}}
ImageUrl="~/images/buttons/clickMe.png"
onclick="btnGetRequiredAccessCustomDate_Click" />
(语法可能不正确,因为曾经在xaml中工作,但快速搜索显示ImageButton的Visibility属性是一个可绑定属性。