当grid.net复选框被勾选为asp.net时,C#重定向到其他页面

时间:2015-01-29 13:13:06

标签: c# asp.net gridview checkbox

我需要你帮助的人,我有两个页面和一个带有复选框列的gridview,我想在用户勾选复选框时重定向到第二页。这是我的代码:

<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");
        }
}

1 个答案:

答案 0 :(得分:2)

最简单的方法是确保您的复选框触发自动回发,并在后面的代码中处理此事件:

<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");
    }
}