【问题标题】:How can I open another page when pressing the Edit button on a gridview?按下gridview上的编辑按钮时如何打开另一个页面?
【发布时间】:2011-06-03 07:32:52
【问题描述】:

我在 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;
        }

但没有运气。

最好的方法是什么?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    不要直接写入响应流,试试这个:

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

    【讨论】:

    • 如果您使用的是 Microsoft AJAX,那么您应该使用 ScriptManager 对象而不是调用 Page.ClientScript 的方法(因为如果回发是异步发生的,它们将不起作用)。
    【解决方案2】:

    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()))
                                {
    

    字符串 urlWithParameters = "Stickers.aspx?StickerId=" + hlStickerNum.Text; hlStickerNum.Attributes.Add("OnClick", "popWinNote('" + urlWithParameters + "')");

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

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

    【讨论】:

      【解决方案3】:

      试试这个

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

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

      【讨论】:

        【解决方案4】:

        此代码将在新窗口中打开窗口。为此,您需要使用 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);
        

        【讨论】:

        • 它正在工作,但我如何修改它以在另一个选项卡而不是另一个窗口中打开页面?
        • 我认为它的默认浏览器行为..是否在新标签/窗口中打开
        猜你喜欢
        • 2016-05-19
        • 1970-01-01
        • 2016-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-12
        相关资源
        最近更新 更多