-1

I have two buttons (btn_Out and btn_In). I want to display btn_Out if the value of field Active in an SQL table called Machines is True (1) and display btn_In if the value of Active is set to False (0).

Every row in the data in the gridview may have a different Active flag and so the button needs to reflect this. The button will change the active flag from 0 to 1 and vice versa (i have this working!).

I am using a gridview and my code is as follows:

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;
    }
}

My aspx.net page doesn't like that I've used the buttons in the if and else if statement and won't compile! Any tips would be much appreciated :)

MethodMan
  • 18,625
  • 6
  • 34
  • 52
akb29
  • 61
  • 1
  • 3
  • 11
  • What is the error you are getting? Canyou put complete code, with method names and all. – Dheeraj Kumar Aug 30 '16 at 14:27
  • Where is your code? You have to do this on PageLoad. – Gustavo F Aug 30 '16 at 14:27
  • you need to be familiar with how PostBacks and Rendering work.. you need to get at the attributes of the button to do this also in C# code behind what event does this code reside in..? – MethodMan Aug 30 '16 at 14:27
  • 1
    You should get an exception from sql-server because you are using `@Serial_Number` as parameter name but it's actually `@SerialNumber`. Do you have a `Try...Catch{//ignore}` that swallows this exception? – Tim Schmelter Aug 30 '16 at 14:28
  • I think you are in wrong way, if you using this code withing `foreach`, you are attacking to the server. I said `foreach`, because you need that to access `buttons` – Mehdi Dehghani Aug 30 '16 at 14:39

3 Answers3

1

Best method to dynamically change the gridview cell value is onrowdatabound event. May following code help you out:

Aspx Page

    <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> 

Code-behind:

    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;
        }
    }
    }
Rojalin Sahoo
  • 1,025
  • 1
  • 7
  • 18
1

Why not set the visibility directly in the buttons? Then you don't need 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" />

Also works with strings etc if your flag is not a Boolean.

Visible='<%# DataBinder.Eval(Container.DataItem, "Active").ToString() == "1" %>'
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Thank you - i found that this was the easiest and quickest way of doing it without having to change a lot of code!! – akb29 Aug 31 '16 at 09:59
0

I have two buttons (btn_Out and btn_In).

You have 2 buttons in each row, and each row may have different value, that is you can't select that value only once as you did in your code. Instead, read about RowDataBound event (MSDN, How to find control in TemplateField of GridView?, etc), get rid of your current code and do something similar to this

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;
        }
    }
}
Community
  • 1
  • 1
user2316116
  • 6,726
  • 1
  • 21
  • 35