Gridview链接按钮在页面加载事件上的后url更改

本文关键字:url 更改 事件 加载 按钮 链接 Gridview | 更新日期: 2023-09-27 18:17:37

我有一个gridview与一个链接按钮,url链接按钮分配使用一个代码通过替换一个关键字从数据库的名称。

都工作得很好,除了当我在浏览器中单击后退按钮并尝试不同的链接时,我得到这个错误:

" HTTP动词POST用于访问路径'/System.Web.UI.WebControls. "标签"不允许"

下面是我的代码:
   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            GridView1.DataSourceID = "SqlDataSource1";
            GridView1.DataBind();

            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    var HyperLink = row.FindControl("LinkButton1") as LinkButton;
                    var RepID = row.FindControl("Label1") as Label;
                    if (RepID != null)
                    {
                        StringBuilder lnk = new StringBuilder("http://bhvwtwbis2/Ops/_layouts/ReportServer/RSViewerPage.aspx?rv:RelativeReportUrl=/Ops/GAPPBASE/Reports/kai.rdl&Source=http%3A%2F%2Fbhvwtwbis2%2FOps%2FGAPPBASE%2FForms%2FAllItems%2Easpx%3FRootFolder%3D%252FOps%252FGAPPBASE%252FReports%26FolderCTID%3D0x012000D833091DB062524DA7A0550847E4E075%26View%3D%7B8A039A42%2D111E%2D40C4%2D8489%2D0D7F32CEAF36%7D&DefaultItemOpen=1");
                        lnk.Replace("kai", RepID.Text + "x1");
                        HyperLink.PostBackUrl = lnk.ToString();
                    }
                }
            }
        }
    }

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="report_id" DataSourceID="SqlDataSource1" BackColor="White" 
        BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        ForeColor="Black" GridLines="Horizontal">
        <Columns>
        <asp:TemplateField HeaderText="ID">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("report_name")%>'></asp:Label>
        </ItemTemplate>
                    <HeaderStyle HorizontalAlign="center" 
                        VerticalAlign="Middle" />
                    <ItemStyle CssClass="link3" HorizontalAlign="Left" />
        </asp:TemplateField>
                <asp:TemplateField HeaderText="Title">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("report_subject")%>'></asp:LinkButton>
        </ItemTemplate>
                    <HeaderStyle HorizontalAlign="center" 
                        VerticalAlign="Middle" />
                    <ItemStyle CssClass="link3" HorizontalAlign="Left" />
        </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F7F7F7" />
        <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
        <SortedDescendingCellStyle BackColor="#E5E5E5" />
        <SortedDescendingHeaderStyle BackColor="#242121" />
    </asp:GridView>

Gridview链接按钮在页面加载事件上的后url更改

我认为问题可能是您在页面生命周期的Page_Load阶段进行绑定。尝试将foreach代码移动到

    protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
                  var HyperLink = e.Row.FindControl("LinkButton1") as LinkButton;
                  //var RepID = row.FindControl("Label1") as Label;
                  //You should be able to access your field without referencing the label too
                  DataRow row = ((DataRowView)e.Row.DataItem).Row;
                  var myField = row.Field<string>("report_name");
                  if (RepID != null)
                  {
                      StringBuilder lnk = new StringBuilder("http://bhvwtwbis2/Ops/_layouts/ReportServer/RSViewerPage.aspx?rv:RelativeReportUrl=/Ops/GAPPBASE/Reports/kai.rdl&Source=http%3A%2F%2Fbhvwtwbis2%2FOps%2FGAPPBASE%2FForms%2FAllItems%2Easpx%3FRootFolder%3D%252FOps%252FGAPPBASE%252FReports%26FolderCTID%3D0x012000D833091DB062524DA7A0550847E4E075%26View%3D%7B8A039A42%2D111E%2D40C4%2D8489%2D0D7F32CEAF36%7D&DefaultItemOpen=1");
                    lnk.Replace("kai", myField  + "x1");
                    HyperLink.PostBackUrl = lnk.ToString();
                  }
          }
    }