简体   繁体   English

如何在GridView中单击编辑按钮时自动选中复选框模板,C#

[英]how to get automatically checked on checkbox templete on click of edit button in gridview,c#

am having a grid view and i have a template checkbox... Now i have a edit, delete button. 我有一个网格视图,我有一个模板复选框...现在我有一个编辑,删除按钮。

Once i click on edit button in gridview.... This template checkbox has to be automatically have checked == true... that is automatically checked has to be selected on click of edit buton in gride view..can anyone tell that code...Thank you 一旦我单击gridview中的编辑按钮...。此模板复选框必须已自动选中== true ...必须在网格视图中单击“编辑”按钮后才选中自动选中的模板。 ...谢谢

Here is a sample code for a grid view that populates set of Person objects. 这是一个填充了一组Person对象的网格视图的示例代码。

This is the markup code of the GridView 这是GridView的标记代码

<asp:GridView runat="server" ID="grdView" AutoGenerateColumns="False" 
        onrowcancelingedit="grdView_RowCancelingEdit" 
        onrowdatabound="grdView_RowDataBound" onrowediting="grdView_RowEditing">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:TemplateField HeaderText="IsActive Template">
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblIsActive"></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBox ID="chkIsActive" runat="server" />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>

and this is the code behind that handles these events 这是处理这些事件的背后代码

    public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }

    private void BindGrid()
    {
        List<Person> persons = new List<Person>
        {
            new Person{ID = 1, IsActive = true, Name = "Test 1"},
            new Person{ID = 2, IsActive = true, Name = "Test 2"},
            new Person{ID = 3, IsActive = true, Name = "Test 3"}
        };

        grdView.DataSource = persons;
        grdView.DataBind();
    }

    protected void grdView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdView.EditIndex = e.NewEditIndex;

        grdView.DataBind();
    }

    protected void grdView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdView.EditIndex = -1;
        grdView.DataBind();
    }

    protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Person p = e.Row.DataItem as Person;
        if (p == null)
            return;
        var lbl = e.Row.Cells[2].FindControl("lblIsActive") as Label;
        if (lbl != null)
        {
            lbl.Text = p.IsActive ? "Yes" : "No";
        }
        else
        {
            var chkIsActive = e.Row.Cells[2].FindControl("chkIsActive") as CheckBox;
            if (chkIsActive != null)
            {
                if (p != null)
                    chkIsActive.Checked = p.IsActive;
            }
        }
    }


}

class Person
{
    public int ID { get; set; }

    public string Name { get; set; }

    public bool IsActive { get; set; }
}

So As you see the RowDataBound event is the one responsible for writing the right value in the template field. 因此,如您所见,RowDataBound事件是负责在模板字段中写入正确值的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “单击按钮”复选框选中了C#中的验证 - On Button Click checkbox checked validation in C# CheckBox在c#GridView中的第二次单击中被选中 - CheckBox gets checked in the second click in c# GridView 如何在C#中单击按钮时在gridview中获取下拉值? - how to get dropdown value inside gridview on button click in c#? 从Gridview中获取一个已选中复选框的值 - Get a value of checked checkbox into a button from a Gridview gridview中的选中复选框,在c#代码中,checked属性为false - Checked checkbox in gridview, in c# codebehind checked property is false 如何设置填充复选框在网格视图中从动作按钮单击选中(true) - how to set populate checkbox is checked(true) in gridview from action button click 如何在C#中实现自动选中和未选中的单选按钮和计时器? - How to implement automatically checked and unchecked radio button and timer in C#? 如果使用Jquery选中了复选框,则ASP.NET C#在gridview中获取第二列 - ASP.NET C# Get second column in gridview if checkbox is checked with Jquery 在gridview中单击“编辑”按钮时,如何调用“删除行”事件? asp.net C# - How can I call “delete row ” event when i click on “edit” button in gridview? asp.net C# 通过C#在gridview中“按下”编辑按钮 - “press” edit button in gridview through c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM