Search code examples
c#asp.netgridviewrowdatabound

Gridview click on row enable controls


I have a gridview. The gridview has a textbox multiline that keeps an text. After that I have a column with imagebutton named approved, and one named not approved. I want to force the user before click approved or not approved to read the text inside the multiline box. How can I achieve this programmatically? I know I should create a Rowdatabound Event, but what I should do with the code? I am using c# ASP.NET web application.


Solution

  • I know you can track the scroll bar of the browser using javascript but I've personally never come across similar functionality for a text box. I would suggest trying a slightly different approach, why don't you add an extra column to you grid view which has a check box control for - I've read and accepted the agreement. And the Approve button will only be enabledd when the checkbox is checked, here's an example:

    ASPX:

    <div>
        <asp:ScriptManager ID="sm" runat="server" />
        <asp:UpdatePanel ID="updatePanel" runat="server">
            <ContentTemplate>
                <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" OnRowDataBound="grid_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:TextBox TextMode="MultiLine" runat="server" ID="txtAgreement" Text='<%# Eval("Agreement") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                I have read and accepted the agreement
                                <asp:CheckBox ID="chkAgreement" AutoPostBack="true" runat="server" OnCheckedChanged="CheckedChanged" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="btnAccept" runat="server" Text="Accept" OnClick="Accept" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    

    Code behind:

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var items = new List<License> { new License { Agreement = "Agreement 1" }, new License { Agreement = "Agreement 2" } };
                grid.DataSource = items;
                grid.DataBind();
            }
        }
    
        protected void Accept(object sender, EventArgs e)
        {
        }
    
        protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                (e.Row.FindControl("btnAccept") as Button).Enabled = false;
        }
    
        protected void CheckedChanged(object sender, EventArgs e)
        {
            var chkAgreement = sender as CheckBox;
            Button btnAccept = null;
    
            if (chkAgreement != null)
            {
                var row = chkAgreement.Parent.Parent as GridViewRow;
                btnAccept = row.FindControl("btnAccept") as Button;
    
                if (btnAccept != null)
                    if (chkAgreement.Checked)
                        btnAccept.Enabled = true;
                    else
                        btnAccept.Enabled = false;
            }
        }
    }
    
    public class License
    {
        public string Agreement { get; set; }
    }