2014-04-15 27 views
0

我正在开发一个项目,因为我有一个搜索选项并提交按钮。在提交时,如果有一些记录对应于选择,那么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(); 
    } 
} 
+0

您是否在页面加载上绑定了gridview ..? – Jameem

+0

你可以发布你的Page_Load事件吗? –

回答

1

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

尝试从您的代码中删除可见属性&在您的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>  

代码隐藏

 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

+0

我无法设置页面加载的可见性,因为它取决于何时隐藏或何时显示的条件。请提出一些其他解决方案,我无法解决此问题 – rupinder18

+0

@ rupinder18尝试在您的aspx代码中进行设置。您不需要将可见性设置为true/false。一个空网格不会显示任何数据。 –

+0

但我已经在面板和div中包含了我的网格以包含滚动,所以这就是我将面板的可见性设置为true/false的原因。我只是设置了gridview的可见性来解决我的问题,但它不起作用甚至 – rupinder18

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代码

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; 
    } 
    } 
} 
2

我不知道你已经在你的代码 做什么,但我知道你的问题。

问题1.当父Gridview可视为假行&列仍然可见False 即使您设置了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; 
     } 

没有必要的foreach为什么要浪费你的CPU时间的。从

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; 

网格删除的foreach和括号内保持

问题代码3,主要问题为您 更改代码将是可见的 问题是电网可见=真应该在数据绑定之前

相关问题