User-707554951 posted
Hi Norque,
From your description, do you mean you want to use checkbox to implement Edit/Delete operation, when checked the check box you want to redirect to the Edit page to edit the selected row information? If that is the case, you could do as
the code below:
asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
Edit <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/>
Delete<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" data-name='<%#Eval("Name") %>' PostBackUrl='<%#Eval("Link") %>' OnClientClick="checkrecords(this)"><%#Eval("Link") %></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CodeBehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ID", typeof(int)), new DataColumn("Name", typeof(string)),
new DataColumn("Link", typeof(string)) });
dt.Rows.Add(1, "Asp.net Forum", "http://forums.asp.net/");
dt.Rows.Add(2, "Goole", "https://www.google.com.sg/");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
//Get the row current CheckBox located
GridViewRow row = (GridViewRow)cb.NamingContainer;
//Determine the RowIndex of the Row whose Button was clicked.
int index = row.RowIndex;
//Get the value of column from the DataKeys using the RowIndex.
int id = Convert.ToInt32(GridView1.DataKeys[index].Values[0]);
if (cb.Checked == true)
{
//redict to 'Edit text books' page with passing id
}
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
//Get the row current CheckBox located
GridViewRow row =(GridViewRow)cb.NamingContainer;
//Determine the RowIndex of the Row whose Button was clicked.
int index = row.RowIndex;
//Get the value of column from the DataKeys using the RowIndex.
int id = Convert.ToInt32(GridView1.DataKeys[index].Values[0]);
if (cb.Checked == true)
{
//do operation to delete the row from GridView according to id
}
}
Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
Best regards
Cathy