使用 Gridview 数据有时才有效

Using Gridview data only works sometimes

提问人:Oic0 提问时间:11/4/2023 最后编辑:Oic0 更新时间:11/4/2023 访问量:24

问:

我写了一个小的内部网页,查询SQL的很多页面逻辑(我是一个SQL人,而不是一个Web开发人员!如果我当前的浏览器会话在打开之前先打开了同一网站的另一个页面,那么一切都很好。我可以在选项卡中打开另一个页面,关闭选项卡,然后复制并粘贴到我的链接中,效果很好。如果我尝试在我从未打开过其他页面的新浏览器中打开它,我会得到“[ArgumentOutOfRangeException:索引超出范围。必须为非负数且小于集合的大小。 参数名称:index]”。当网格视图尝试从单元格中提取数据时,它的行为就好像它没有被绑定一样。如果我注释掉页面加载事件中的所有内容,则页面将加载并填充网格视图。我尝试将所有内容移动到各自网格视图的数据绑定事件中,但行为保持不变。我不知所措!

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlAccessLevelDatasource.DataBind();
        SqlAttendeeDatasource.DataBind();
        SqlBtnLogicDatasource.DataBind();
        GvAttendeeList.DataBind();
        GvBtnLogic.DataBind();
        GvLevel.DataBind();
        ButtonRemove.Visible = GvBtnLogic.Rows[0].Cells[0].Text == "0" ? true : false;
        ButtonAdd.Visible = GvBtnLogic.Rows[0].Cells[0].Text == "1" ? true : false;
        ButtonWait.Visible = GvBtnLogic.Rows[0].Cells[0].Text == "2" ? true : false;
        Label1.Visible = GvBtnLogic.Rows[0].Cells[0].Text == "3" ? true : false;
        TrAdminPanel.Visible = int.Parse(GvLevel.Rows[0].Cells[0].Text) > 1 ? true : false;
        BtnOverride.Visible = int.Parse(GvLevel.Rows[0].Cells[0].Text) > 2 ? true : false;
        GvAttendeeList.Columns[0].Visible = int.Parse(GvLevel.Rows[0].Cells[0].Text) > 2 ? true : false;
        GvAttendeeList.Columns[2].Visible = int.Parse(GvLevel.Rows[0].Cells[0].Text) > 2 ? true : false;

    }

编辑Page 确实在 URL 中使用了一个参数,该参数在其中一个 SQL 数据源中用作 QueryStringParameter,但它位于我尝试打开但失败的 URL 中。

我简单的小网格视图是

        <asp:GridView ID="gvBtnLogic" runat="server" AutoGenerateColumns="False" DataSourceID="SqlBtnLogicDatasource" ShowHeaderWhenEmpty="true" SelectedIndex="0" ViewStateMode="Enabled">
            <Columns>
                <asp:BoundField DataField="status" HeaderText="status" ReadOnly="True" SortExpression="status" />
                <asp:BoundField DataField="Column1" HeaderText="Column1" ReadOnly="True" SortExpression="Column1" />
            </Columns>
        </asp:GridView>
C# asp.net GridView SqlDataSource

评论

0赞 Albert D. Kallal 11/4/2023
网页是否需要 URL 中的某些参数?您发布了加载事件,但您至少应该发布 GridView 标记的第一行,并且将用于数据源的标记发布到 gv 可能是个好主意。
0赞 Oic0 11/4/2023
当我收集阿尔伯特要求的东西时,终于弄清楚了。我在所有数据源中使用的用户名会话变量不想始终如一地工作。我改用 lblUser.Text = User.Identity.Name.ToString()。Replace(“MyDomain\\”,“”) ,然后将该标签引用为变量。现在效果很好。这是一个奇怪的问题。我可以从同一个网站打开一个页面,然后关闭选项卡,然后打开这个页面就好了。关闭浏览器并重新打开它,它不会加载。就好像变量被第一页缓存并被第二页引用一样。

答: 暂无答案