第一次加载页面时的GridView可见性

时间:2014-04-15 07:45:49

标签: c# asp.net gridview

我正在开发一个项目,因为我有一个搜索选项和提交按钮。如果有一些记录对应于选择,则提交gridview会显示。但我面临的问题是当页面被加载时第一次即使Gridview没有数据也没有出现,但是在调试时我发现数据已经加载,但是在第二次选择时,gridview变得可见。

以下屏幕截图显示了我的页面

以下是提交按钮上的代码点击

enter image description here

protected void btnsubmitclass_OnClick(object sender,EventArgs e)
{
    if(drpfromclass.SelectedIndex>0 && drptoclass.SelectedIndex>0)
    {
        ds = objpromote.selectclasswise(ConfigurationManager.AppSettings["schoolcode"].ToString(),drpfromclass.SelectedItem.Value,drptoclass.SelectedItem.Value);
        if(ds.Tables[0].Rows.Count>0)
        {
            foreach(GridViewRow gr in grdstudents.Rows)
            {
                grdstudents.Columns[0].Visible = true;
                grdstudents.Columns[1].Visible = true;
                grdstudents.Columns[2].Visible = false;
                grdstudents.Columns[3].Visible = false;
                grdstudents.Columns[4].Visible = true;
            }
            grdstudents.DataSource = ds;
            grdstudents.DataBind();
            grdstudents.Visible = true;
            lblheading.Text = "Showing list of students moved from " + drpfromclass.SelectedItem.Text + " to " + drptoclass.SelectedItem.Text;
            pnlgrd.Visible = true;
        }
        else
        {
            pnlgrd.Visible = false;
            lblalerts.Text = "No record found";
            lblalerts.Visible = true;
            divalerts.Visible = true;
        }
        drpfromclass.ClearSelection();
        drptoclass.ClearSelection();
    }
    else
    {
        lblalerts.Text = "Select fromclass and toclass";
        lblalerts.Visible = true;
        divalerts.Visible = true;
        pnlgrd.Visible = false;
    }
}

以下是aspx代码

<asp:Panel ID="pnlgrd" runat="server" Visible="false">
            <div style="width:700px;height:30px;" class="blueheadingdiv"><asp:Label ID="lblheading" runat="server" Text="Label" CssClass="lblheading"></asp:Label></div>
        <div style="width:610px; height:500px;margin-top:10px; overflow-y:scroll;">
        <asp:GridView ID="grdstudents" runat="server"  Width="590"  ForeColor="#333333" BorderColor="#6abb00" GridLines="None" AutoGenerateColumns="False" CellPadding="5">
    <Columns>
       <asp:BoundField HeaderText="Student Code" DataField="studentcode" Visible="false"/>
       <asp:BoundField HeaderText="Student Name" DataField="studentname" Visible="false"/>
       <asp:BoundField HeaderText="From Class" DataField="fromclass" Visible="false"/>
       <asp:BoundField HeaderText="To Class" DataField="toclass" Visible="false"/> 
       <asp:BoundField HeaderText="Date" DataField="date" Visible="false"/>            
    </Columns>
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#4382EB" Font-Bold="True" ForeColor="White" CssClass="gridviewheader" HorizontalAlign="Left"/>
        <PagerStyle BackColor="#4382EB" Font-Bold="True" ForeColor="White" CssClass="gridviewheader" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
        </div>
        </asp:Panel>

页面加载代码如下所示

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        filldrpclass();
    }
}

3 个答案:

答案 0 :(得分:2)

我不知道你在代码中做了什么 但我知道你的问题。

问题1.当父网格视图显示为错误而不是行&amp;列仍然可见falser 即使你设置了grdstudents.Columns [0] .Visible = true;这是假的 问题2

 foreach(GridViewRow gr in grdstudents.Rows)
        {
            grdstudents.Columns[0].Visible = true;
            grdstudents.Columns[1].Visible = true;
            grdstudents.Columns[2].Visible = false;
            grdstudents.Columns[3].Visible = false;
            grdstudents.Columns[4].Visible = true;
        }

没有必要预先知道为什么要浪费你的CPU时间。删除foreach和braces将代码保留在

问题3.主要问题 从

更改代码
 foreach(GridViewRow gr in grdstudents.Rows)
        {
            grdstudents.Columns[0].Visible = true;
            grdstudents.Columns[1].Visible = true;
            grdstudents.Columns[2].Visible = false;
            grdstudents.Columns[3].Visible = false;
            grdstudents.Columns[4].Visible = true;
        }
        grdstudents.DataSource = ds;
        grdstudents.DataBind();

        grdstudents.Visible = true;
        grdstudents.Columns[2].Visible = true;
        grdstudents.Columns[3].Visible = true;
        grdstudents.DataSource = ds;
        grdstudents.DataBind();
        grdstudents.Columns[0].Visible = ;
        grdstudents.Columns[1].Visible = true;
        grdstudents.Columns[2].Visible = false;
        grdstudents.Columns[3].Visible = false;
        grdstudents.Columns[4].Visible = true;

您的网格将可见 问题是Grid visible = true应该在databind之前

答案 1 :(得分:1)

将gridview的可见性设置为false没有任何意义,因为没有记录它根本不显示任何数据。我能理解的是,在初始pageLoad中,您指定DataBind事件后的可见性。由于某些奇怪的原因,这无法显示数据。现在,第一次尝试时网格可见,第二次尝试时会显示数据。

尝试从代码中删除visible属性&amp;在aspx页面中将可见性设置为true。

        grdstudents.DataSource = ds;
        grdstudents.DataBind();
        //grdstudents.Visible = true;    

此外,当您可以使用GridView的EmptyDataTemplatePropertyEmptyDataText属性时,无需在标签控件中显示消息。

这是一个没有可见性或面板的工作示例。当网格为空时,不会显示滚动条。

.aspx的

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridtest.aspx.cs" Inherits="gridtest" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search Records" />
            <br />
            <div style="width: 100%; height: 100px; width: 400px; overflow: auto">
                <asp:GridView ID="GridView1" runat="server" Width="400px">
                </asp:GridView>
            </div>
        </div>
        </form>
    </body>
    </html>    

Codebehind

     public class Customer
    {
        public int CustId { get; set; }
        public string CustomerName { get; set; }
        public string Address { get; set; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<Customer> customers= new List<Customer>()
        {
            new Customer {CustId=1 ,CustomerName= "John Doe", Address= "ACME Street"},
            new Customer {CustId=2 ,CustomerName= "David Bowie", Address= "abcd lane"},
            new Customer {CustId=3 ,CustomerName= "Sarah Lynn", Address= "House 84"},
            new Customer {CustId=1 ,CustomerName= "John Doe", Address= "ACME Street"},
            new Customer {CustId=2 ,CustomerName= "David Bowie", Address= "abcd lane"},
            new Customer {CustId=3 ,CustomerName= "Sarah Lynn", Address= "House 84"}
        };

        GridView1.DataSource = customers;
        GridView1.DataBind();
    }

Sample

答案 2 :(得分:0)

我之前修改了我的代码我从代码隐藏设置了gridview列的可见性,但现在我只包含了所需的列。我正在更新我的aspx代码和cs代码

aspx代码

<asp:Panel ID="pnlgrd" runat="server" Visible="false">
            <div style="width:700px;height:30px;" class="blueheadingdiv"><asp:Label ID="lblheading" runat="server" Text="Label" CssClass="lblheading"></asp:Label></div>
        <div style="width:610px; height:500px;margin-top:10px; overflow:auto;">
        <asp:GridView ID="grdstudents" runat="server"  Width="590"  ForeColor="#333333" BorderColor="#6abb00" GridLines="None" CellPadding="5">       
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#4382EB" Font-Bold="True" ForeColor="White" CssClass="gridviewheader" HorizontalAlign="Left"/>
        <PagerStyle BackColor="#4382EB" Font-Bold="True" ForeColor="White" CssClass="gridviewheader" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
        </div>
        </asp:Panel>

cs code

protected void btnsubmitclass_OnClick(object sender,EventArgs e)
{
    if(rbchoice.SelectedIndex==0)
    {
    if(drpfromclass.SelectedIndex>0 && drptoclass.SelectedIndex>0)
    {
        ds = objpromote.selectclasswise(ConfigurationManager.AppSettings["schoolcode"].ToString(),drpfromclass.SelectedItem.Value,drptoclass.SelectedItem.Value);
        if(ds.Tables[0].Rows.Count>0)
        {
            grdstudents.Visible = true;
            grdstudents.DataSource = ds;
            grdstudents.DataBind();
            lblheading.Text = "Showing list of students moved from " + drpfromclass.SelectedItem.Text + " to " + drptoclass.SelectedItem.Text;
            pnlgrd.Visible = true;
        }
        else
        {
            pnlgrd.Visible = false;
            lblalerts.Text = "No record found";
            lblalerts.Visible = true;
            divalerts.Visible = true;
        }
        drpfromclass.ClearSelection();
        drptoclass.ClearSelection();
    }
    else
    {
        lblalerts.Text = "Select fromclass and toclass";
        lblalerts.Visible = true;
        divalerts.Visible = true;
        pnlgrd.Visible = false;
    }
    }
    else
    {
        if (drpclass.SelectedIndex > 0 && drpstudent.SelectedIndex > 0)
    {
        ds = objpromote.selectstudentwise(ConfigurationManager.AppSettings["schoolcode"].ToString(),drpstudent.SelectedItem.Value);
        if (ds.Tables[0].Rows.Count > 0)
        {
            grdstudents.Visible = true;
            grdstudents.DataSource = ds;
            grdstudents.DataBind();
            lblheading.Text = "Showing detailed list of "+drpstudent.SelectedItem.Text+" of class- "+drpclass.SelectedItem.Text;
            pnlgrd.Visible = true;
        }
        else
        {
            lblalerts.Text = "No record found";
            lblalerts.Visible = true;
            divalerts.Visible = true;
            pnlgrd.Visible = false;
        }
        drpclass.ClearSelection();
        drpstudent.ClearSelection();
    }
    else
    {
        lblalerts.Text = "Select fromclass and toclass";
        lblalerts.Visible = true;
        divalerts.Visible = true;
        pnlgrd.Visible = false;
    }
    }
}