根据SQL数据库中字段的值显示按钮

本文关键字:显示 按钮 字段 SQL 数据库 根据 | 更新日期: 2023-09-27 18:04:49

我有两个按钮(btn_Out和btn_In)。如果SQL表Machines中的字段Active的值为True(1),则显示btn_Out;如果Active的值设置为False(0),则显示btn_In。

gridview中数据的每一行可能有不同的Active标志,因此按钮需要反映这一点。该按钮将改变活动标志从0到1,反之亦然(我有这个工作!)。

我正在使用gridview和我的代码如下:

ASP。净

<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语句中使用按钮,不会编译!如有任何提示,不胜感激:)

根据SQL数据库中字段的值显示按钮

为什么不直接在按钮中设置可见性呢?那么你就不需要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" %>'

动态更改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;
        }
    }
    }

我有两个按钮(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;
        }
    }
}