Gridview linkbutton postbackurl更改页面加载事件

时间:2013-06-10 12:53:09

标签: c# gridview

我有一个带链接按钮的网格视图,通过用数据库中的名称替换关键字,使用代码分配网址链接按钮。

一切正常,但是当我点击浏览器中的后退按钮并尝试不同的链接时,我收到此错误:

“不允许使用用于访问路径'/System.Web.UI.WebControls.Label'的HTTP谓词POST”

下面是我的代码:

   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>

1 个答案:

答案 0 :(得分:0)

我认为问题可能是您在页面生命周期的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();
                  }
          }

    }