[英]Show a button based on the value of a field in SQL database
我有兩個按鈕(btn_Out和btn_In)。 我想顯示btn_Out如果場上主動的叫機器的SQL表中的值是真(1),如果主動的值設置為False(0)顯示btn_In。
網格視圖中數據的每一行可能都有一個不同的“活動”標志,因此按鈕需要反映這一點。 該按鈕會將活動標志從0更改為1,反之亦然(我有此功能!)。
我正在使用gridview,我的代碼如下:
ASP.NET
<ItemTemplate>
<asp:Button ID="btn_In" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
</ItemTemplate>
<ItemTemplate>
<asp:Button ID="btn_Out" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
</ItemTemplate>
C#:
DataTable dt = new DataTable();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Open();
con = new SqlConnection(cs);
// SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select [active] from [ALLMACHINES].[dbo].[Machines] where [serial_number] = @serialNumber";
cmd.Parameters.AddWithValue("@Serial_Number", serialNumber);
int Active = Convert.ToInt32(cmd.ExecuteScalar());
if (Active == 1)
{
btn_In.Visible = false;
btn_Out.Visible = true;
}
else if (Active == 0)
{
btn_Out.Visible = false;
btn_In.Visible = true;
}
}
我的aspx.net頁面不喜歡我使用過if和else if語句中的按鈕,因此無法編譯! 任何提示將不勝感激:)
動態更改gridview單元格值的最佳方法是onrowdatabound
事件。 可能下面的代碼可以幫助您:
Aspx頁面
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Lable ID="lblstus" runat="server" Text="#Eval("active")" Visible=false>
</ItemTemplate>
... otherfields you wanted to add
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_In" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
<asp:Button ID="btn_Out" runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代碼:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Lable lblstus = e.Row.FindControl("lblstus") as Lable;
Button btn_Out = e.Row.FindControl("btn_Out") as Button;
Button btn_In = e.Row.FindControl("btn_In") as Button;
if(lblstus.Text == "1")
{
btn_In.Visible = false;
btn_Out.Visible = true;
} else {
btn_In.Visible = false;
btn_Out.Visible = true;
}
}
}
為什么不直接在按鈕中設置可見性? 那么您不需要OnRowDataBound
<asp:Button ID="btn_In" Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Active")) == true %>' Text="Set in Scope" CommandName="Update" CssClass="Button" runat="server" />
<asp:Button ID="btn_Out" Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "Active")) == false %>' runat="server" Text="Set in Scope" CommandName="Update" CssClass="Button" />
如果您的標志不是布爾值,也可用於字符串等。
Visible='<%# DataBinder.Eval(Container.DataItem, "Active").ToString() == "1" %>'
我有兩個按鈕(btn_Out和btn_In)。
每行有2個按鈕,並且每行可能具有不同的值,也就是說,您不能像在代碼中那樣僅選擇一次該值。 相反,請閱讀RowDataBound事件( MSDN , 如何在GridView的TemplateField中找到控件?等),擺脫當前的代碼,並執行類似的操作
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String flag = rowView["active"].ToString();
Button btn_Out = e.Row.FindControl("btn_Out") as Button;
Button btn_In = e.Row.FindControl("btn_In") as Button;
if (flag == "1") {
btn_In.Visible = false;
btn_Out.Visible = true;
} else {
btn_In.Visible = false;
btn_Out.Visible = true;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.