在我的asp页面中,我使用了两个gridview
。并在页面加载事件中将数据绑定到这些gridview
。但它显示"System.NullReferenceException: Object reference not set to an instance of an object.":
并在此行获得例外
GridReturned.DataSource = ds2;
这是我的代码
<asp:Accordion ID="Accordion1" runat="server" SelectedIndex="0" AutoSize="None"
FadeTransitions="true" TransitionDuration="300" FramesPerSecond="25">
<Panes>
<asp:AccordionPane ID="AccordionPane1" runat="server">
<Header>
<br />
<div class="headPane">
Issued History
</div>
</Header>
<Content>
<div class="bodyPane">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridIssue" runat="server" CssClass="grid" AllowPaging="true" PageSize="8" OnPageIndexChanging="GridIssue_PageIndexChanging">
<EmptyDataTemplate>
<p style="margin-left: 10px;">No Book Has Been Issued Yet.</p>
</EmptyDataTemplate>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</Content>
</asp:AccordionPane>
<asp:AccordionPane runat="server" ID="acd2">
<Header>
<div class="headPane">
Restore Books
</div>
</Header>
<Content>
<div class="bodyPane">
<asp:GridView ID="GridReturned" runat="server" OnSelectedIndexChanged="GridReturned_SelectedIndexChanged">
<EmptyDataTemplate>
<p>Books Are Not Required To Return At Present.</p>
</EmptyDataTemplate>
<Columns>
<asp:CommandField ButtonType="Button" SelectText="Return" ShowSelectButton="true" />
</Columns>
</asp:GridView>
</div>
</Content>
<Content>
</Content>
</asp:AccordionPane>
</Panes>
</asp:Accordion>
后面的代码如下
string str = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
SqlConnection con;
SqlCommand cmd;
string getQuery;
protected void Page_Load(object sender, EventArgs e)
{
getQuery = "select Issue_ID as 'Issue Id', IssueDate as 'Issue Date', ReturnDate as 'Return Date' , UserName as 'User Name' , BookName as 'Book Name', Category ,ActualReturnDate from IssueRecords";
getData(getQuery,0);
getQuery = "select Issue_ID,BID,UserName,BookName,IssueDate,ReturnDate from IssueRecords where ActualReturnDate is null";
getData(getQuery, 1);
}
public void getData(string query, int i)
{
con = new SqlConnection(str);
cmd = new SqlCommand(query, con);
if (i == 0)
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridIssue.DataSource = ds;
GridIssue.DataBind();
con.Close();
}
else
{
con.Open();
SqlDataAdapter da2 = new SqlDataAdapter(cmd);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
GridReturned.DataSource = ds2;
GridReturned.DataBind();
con.Close();
}
}
是否有任何事件行更改页面手风琴索引,以便我可以在更改事件时触发这些事件。
答案 0 :(得分:0)
<div id="content">
<asp:Accordion ID="Accordion1" runat="server" SelectedIndex="0" AutoSize="None"
FadeTransitions="true" TransitionDuration="300" FramesPerSecond="25">
<Panes>
<asp:AccordionPane ID="AccordionPane1" runat="server">
<Header>
<br />
<div class="headPane">
Issued History
</div>
</Header>
<Content>
<div class="bodyPane">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridIssue" runat="server" CssClass="grid" AllowPaging="true" PageSize="8" OnPageIndexChanging="GridIssue_PageIndexChanging">
<EmptyDataTemplate>
<p style="margin-left: 10px;">No Book Has Been Issued Yet.</p>
</EmptyDataTemplate>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</Content>
</asp:AccordionPane>
<asp:AccordionPane runat="server" ID="acd2">
<Header>
<div class="headPane">
Restore Books
</div>
</Header>
<Content>
<div class="bodyPane">
<asp:GridView ID="GridReturned" DataSourceID="sdSource" runat="server" OnSelectedIndexChanged="GridReturned_SelectedIndexChanged">
<EmptyDataTemplate>
<p>Books Are Not Required To Return At Present.</p>
</EmptyDataTemplate>
<Columns>
<asp:CommandField ButtonType="Button" SelectText="Return" ShowSelectButton="true" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdSource" runat="server" SelectCommand="select Issue_ID,BID,UserName,BookName,IssueDate,ReturnDate from IssueRecords where ActualReturnDate is null" ConnectionString="<%$ConnectionStrings:connect %>" ></asp:SqlDataSource>
</div>
</Content>
</asp:AccordionPane>
</Panes>
</asp:Accordion>