user1438082
user1438082

Reputation: 2748

Bind list to gridview including buttons

I would like to bind a list of objects to gridview. The object needs to contain a column of buttons. Some of these buttons need to be disabled depending on the data in the list.

Is it possible to generate a list of objects including the enabled or disabled buttons and simply bind to the gridview?

Upvotes: 1

Views: 1464

Answers (2)

Denys Wessels
Denys Wessels

Reputation: 17049

Having a flag is a good approach but remember that you need someway to actually store the state of your flag and rebind it every time the data changes. I've chose ViewState for this demo, here's the complete example:

ASPX:

<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" Text="Update" Enabled='<%# Eval("AllowUpdate") %>' OnClick="Update" CommandArgument='<%# Eval("Name") %>' />
                <asp:Label runat="server" Text='<%# Eval("Name") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code behind:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            BindData();
    }

    private void BindData()
    {
        if (ViewState["Employees"] == null)
        {
            List<Employee> employees = new List<Employee>
        {
            new Employee{Name="Employee 1", AllowUpdate = true},
            new Employee{Name="Employee 2", AllowUpdate = true}
        };

            gvEmployees.DataSource = employees;
            ViewState["Employees"] = employees;
        }

        else
        {
            List<Employee> employees  = ViewState["Employees"] as List<Employee>;
            gvEmployees.DataSource = employees;
        }

        gvEmployees.DataBind();
    }

    protected void Update(object sender, EventArgs e)
    {
        Button update = sender as Button;
        if (update != null & !String.IsNullOrEmpty(update.CommandArgument))
        {
            string employeeName = update.CommandArgument;

            List<Employee> employees = ViewState["Employees"] as List<Employee>;
            Employee emp = employees.Where(em => em.Name == employeeName).FirstOrDefault();
            emp.Name = "some new value...";
            emp.AllowUpdate = false;

            //Rebind the grid with the new values
            ViewState["Employees"] = employees;
            BindData();
        }
    }
}

[Serializable]
public class Employee
{
    public string Name { get; set; }
    public bool AllowUpdate { get; set; }
}

Upvotes: 1

Mahmoud Farahat
Mahmoud Farahat

Reputation: 5475

you can add button in gridview itemtemplate like this :

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="btn" runat="server" 
     Text="click" Enabled="<%# Eval('IsButtonEnabled') %>"/>
  </ItemTemplate> 
</asp:TemplateField>

then you can add Flag as bool variable in your datasource and set your desired values , true to enable , false to disable

Edit , for example you have datasource like this :

class YourDataSource {

public string prop1 {get;set;}
public string prop2 {get;set;}
public bool IsButtonEnabled {get;set;}
}

if IsButtonEnabled is true so button will be enabled else it will be disabled

Upvotes: 1

Related Questions