4Tune
4Tune

Reputation: 39

C# redirect to other page when gridview checkbox is ticked asp.net

i need you help guys,i have two pages and one with agridview with checkbox column, i want to redirect to the second page when the user tick the checkbox. here is my code :

<asp:BoundField DataField="BC_Description" HeaderText="Description">
    <HeaderStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" HorizontalAlign="Center" />
    <ItemStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" Width="290px"
            HorizontalAlign="Left" />
</asp:BoundField>

<asp:BoundField DataField="Update_Comments" HeaderText="Comments">
    <HeaderStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" HorizontalAlign="Center"
            Width="100px" />
    <ItemStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" Width="350px"
            HorizontalAlign="Left" />
</asp:BoundField>

<asp:TemplateField HeaderText="Changed">
    <HeaderStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" HorizontalAlign="Center" />
    <ItemStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" Width="150px"
            HorizontalAlign="Center" />
    <ItemTemplate>
        <asp:CheckBox ID="chkApprove" runat="server" />
    </ItemTemplate>
</asp:TemplateField>


if (!IsPostBack)
{
    ChangedBy = getInfo.GetUserDetails(compileUserDI);
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("chkApprove");

        if (cb.Checked)
        {
            Response.Redirect("CompileVariance.aspx");
        }
}

Upvotes: 0

Views: 856

Answers (1)

Andrei
Andrei

Reputation: 56688

The easiest way to do this is to make sure your checkbox triggers auto post back, and handle this event in the code behind:

<asp:CheckBox ID="chkApprove" runat="server" AutoPostBack="true" OnCheckedChanged="chkApprove_CheckedChanged" />

protected void chkApprove_CheckedChanged(object source, EventArgs e)
{
    var checkbox = (CheckBox)source;
    if (checkbox.Checked)
    {
        Response.Redirect("CompileVariance.aspx");
    }
}

Upvotes: 2

Related Questions