在gridview上按“编辑”按钮时,如何打开另一页?

时间:2011-01-04 10:22:26

标签: c# asp.net gridview

我在asp.net中使用C#进行Gridview控件。

在我的应用程序中,当我按下Gridview中的编辑按钮时,我需要在另一个窗口中打开信息。此时我使用'Response.Redirect(“..”)'但它会在同一个窗口中打开。

我试过了:

 protected void OutputGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            outputGridView.EditIndex = e.NewEditIndex;
            GridViewRow row = outputGridView.Rows[outputGridView.EditIndex];

            string url = "http://localhost/MyPage.aspx";
            Response.Write("<script>");
            Response.Write("window.open('" + url + "')");
            Response.Write("<" + "/script>")

            e.Cancel = true;
        }

但没有运气。

有谁知道最好的方法吗?

4 个答案:

答案 0 :(得分:2)

试试这个

protected void OutputGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    try
    {
        outputGridView.EditIndex = e.NewEditIndex;
        GridViewRow row = outputGridView.Rows[outputGridView.EditIndex];

        string url = "http://localhost/MyPage.aspx";
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("<script type='text/javascript'>");
        sb.AppendLine("window.open('" + url + "')");
        sb.AppendLine("<" + "/script>");
        ClientScript.RegisterStartupScript(this.GetType(), "myjs", sb.ToString(), false);
        //or if the gridview is inside an updatepanel do the code given below
        //ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "myjs", sb.ToString(), false);
    }
    catch (System.Threading.ThreadAbortException)
    {
        throw;
    }
    catch (Exception err)
    {
        //handle error here
        //Elmah.ErrorSignal.FromCurrentContext().Raise(err);
    }        
}

正如我在上面的代码中所评论的,如果您使用的是ScriptManager,请取消注释并使用此

ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "myjs", sb.ToString(), false);

我已经对它进行了测试及其工作。

答案 1 :(得分:1)

此代码将在新窗口中打开窗口。为此,您需要使用ScriptManager

string BrowserSettings = "status=no,toolbar=no,menubar=no,location=no,resizable=no,"+
                                        "titlebar=no, addressbar=no, width=600 ,height=750";
            string URL = "http://localhost/MyPage.aspx";
            string scriptText = "window.open('" + URL + "','_blank','" + BrowserSettings + "');";
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ClientScript1", scriptText, true);

答案 2 :(得分:0)

不要直接写入响应流,而是尝试:

Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenNewWindow", "window.open(...)", true);

- 帕维尔

答案 3 :(得分:0)

ASPX:创建一个像这样的网格视图

<asp:GridView runat="server" AllowPaging="True" AutoGenerateColumns="False" ID="gvSticker"
                                                        Width="100%" EmptyDataText="No sticker found for this Fisher. Click on add to Add a new sticker."
                                                        HorizontalAlign="Left" ShowFooter="True" ShowHeaderWhenEmpty="True" BackColor="White"
                                                        BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" PageSize="3"
                                                        OnRowDataBound="gvSticker_RowDataBound" OnPageIndexChanging="gvSticker_PageIndexChanging">
                                                        <Columns>
                                                            <asp:TemplateField>
                                                                <HeaderTemplate>
                                                                    <table style="width: 100%">
                                                                        <tr align="left">
                                                                            <td style="width: 15%">
                                                                                Sticker Year
                                                                            </td>
                                                                            <td style="width: 15%">
                                                                                Sticker Number
                                                                            </td>
                                                                            <td style="width: 15%">
                                                                                Issue Date
                                                                            </td>
                                                                            <td style="width: 35%">
                                                                                Issue Type
                                                                            </td>
                                                                            <td style="width: 20%">
                                                                                Status
                                                                            </td>

                                                                        </tr>
                                                                    </table>
                                                                </HeaderTemplate>
                                                                <ItemTemplate>
                                                                    <table style="width: 100%">
                                                                        <tr align="left">
                                                                            <td style="width: 15%">
                                                                                <%# Eval("YearOfIssue")%>
                                                                            </td>
                                                                            <td style="width: 15%">
                                                                                <asp:HyperLink ID="hlStickerNumber" runat="server" Text='<%#Eval("StickerNumber")%>' Style="cursor: hand; text-decoration:underline"></asp:HyperLink>
                                                                            </td>
                                                                            <td style="width: 15%">
                                                                                <%# Eval("DateOfIssue", "{0:MM/dd/yyyy}")%>
                                                                            </td>
                                                                            <td style="width: 35%">
                                                                                <%# Eval("RegistrationType")%>
                                                                            </td>
                                                                            <td style="width: 20%">
                                                                                <asp:Label ID="lblStickerStatus" runat="server" Text='<%# Eval("StickerStatus")%>'></asp:Label>
                                                                            </td>

                                                                        </tr>
                                                                    </table>
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                        </Columns>
                                                        <FooterStyle BackColor="White" ForeColor="#000066" />
                                                        <HeaderStyle HorizontalAlign="Left" BackColor="#006699" Font-Bold="True" ForeColor="White" />
                                                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                                                        <RowStyle HorizontalAlign="Left" ForeColor="#000066" />
                                                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                                                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                                                        <SortedAscendingHeaderStyle BackColor="#007DBB" />
                                                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                                                        <SortedDescendingHeaderStyle BackColor="#00547E" />
                                                        </asp:GridView>    

在数据绑定事件中添加代码。

 protected void gvSticker_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (Session["FisherId"] != null)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lblStatus = (Label)e.Row.FindControl("lblStickerStatus");


                    if (e.Row.RowIndex == 0)
                    {
                        if (lblStatus.Text.Contains("Active"))
                        {
                            btnAddSticker.Enabled = false;
                            HyperLink hlStickerNum = (HyperLink)e.Row.FindControl("hlStickerNumber");
                            if (!string.IsNullOrEmpty(hlStickerNum.Text.Trim()))
                            {
  

string urlWithParameters =   “Stickers.aspx?StickerId =”+   hlStickerNum.Text;                                       hlStickerNum.Attributes.Add( “点击”,   “popWinNote('”“+ urlWithParameters +   “')”);

                            }

                        }
                        else
                        {
                            btnAddSticker.Enabled = true;
                            btnVoidSticker.Enabled = true;
                        }

                    }
                }
            }
            else
            {
                btnAddSticker.Enabled = true;
                btnVoidSticker.Enabled = true;
            }
        }

在aspx页面中,如果需要一个小窗口,在脚本标签PopWinNote中添加一个函数来打开一个show modal对话框。这段代码实际上不是您需要的代码,但可能会让您了解逻辑的实现