[英]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.