更新Panel Gridview未更新

时间:2016-09-06 09:34:37

标签: c# asp.net gridview

当我点击提交按钮时数据在db中更新,但它没有列出 在gridview中

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>  <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True"><ContentTemplate>
    <fieldset style="border: thin dotted #000000; height:919px;width:70%;background-color:#999999;"><legend style="font-size:15px;color:rgb(255,255,255);">Registration</legend><div>       <asp:TextBox ID="txtcode" runat="server" CssClass="textbox" placeholder="Code"></asp:TextBox>   </div>   <br /><div><asp:TextBox ID="txtabr" runat="server" CssClass="textbox" placeholder="AER"></asp:TextBox> </div><br /> <div><asp:TextBox ID="txtauthor" runat="server" CssClass="textbox" placeholder="Author"></asp:TextBox></div><br /><div><asp:TextBox ID="txtEmail" runat="server" CssClass="textbox" placeholder="Email"></asp:TextBox</div><br /><div>
   <asp:TextBox ID="txtpublishdate" runat="server" CssClass="textbox" placeholder="PublishDate"></asp:TextBox>
   <asp:ImageButton ID="ImageButton1" runat="server" BorderColor="#9900FF" 
       onclick="ImageButton1_Click" style="width: 14px"  
        />
   <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
       BorderColor="Black" DayNameFormat="Shortest" Font-Names="Times New Roman" 
       Font-Size="10pt" ForeColor="Black" Height="220px" NextPrevFormat="FullMonth" 
       TitleFormat="Month" Width="400px" onselectionchanged="Calendar1_SelectionChanged" 
       >
       <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" 
           ForeColor="#333333" Height="10pt" />
       <DayStyle Width="14%" />
       <NextPrevStyle Font-Size="8pt" ForeColor="White" />
       <OtherMonthDayStyle ForeColor="#999999" />
       <SelectedDayStyle BackColor="#CC3333" ForeColor="White" />
       <SelectorStyle BackColor="#CCCCCC" Font-Bold="True" Font-Names="Verdana" 
           Font-Size="8pt" ForeColor="#333333" Width="1%" />
       <TitleStyle BackColor="Black" Font-Bold="True" Font-Size="13pt" 
           ForeColor="White" Height="14pt" />
       <TodayDayStyle BackColor="#CCCC99" />
   </asp:Calendar></div><br />  <div>
   <asp:TextBox ID="txtname" runat="server" CssClass="textbox" placeholder="BookName"></asp:TextBox> </div><br /><div>
   <asp:TextBox ID="txtduedate" runat="server" CssClass="textbox" placeholder="DueDate"></asp:TextBox>
     <asp:ImageButton ID="ImageButton2" runat="server" BorderColor="#9900FF" onclick="ImageButton2_Click"  
        />
   <asp:Calendar ID="Calendar2" runat="server" BackColor="White" 
       BorderColor="Black" DayNameFormat="Shortest" Font-Names="Times New Roman" 
       Font-Size="10pt" ForeColor="Black" Height="220px" NextPrevFormat="FullMonth" 
       TitleFormat="Month" Width="400px" onselectionchanged="Calendar2_SelectionChanged" 
       >
       <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" 
           ForeColor="#333333" Height="10pt" />
       <DayStyle Width="14%" />
       <NextPrevStyle Font-Size="8pt" ForeColor="White" />
       <OtherMonthDayStyle ForeColor="#999999" />
       <SelectedDayStyle BackColor="#CC3333" ForeColor="White" />
       <SelectorStyle BackColor="#CCCCCC" Font-Bold="True" Font-Names="Verdana" 
           Font-Size="8pt" ForeColor="#333333" Width="1%" />
       <TitleStyle BackColor="Black" Font-Bold="True" Font-Size="13pt" 
           ForeColor="White" Height="14pt" />
       <TodayDayStyle BackColor="#CCCC99" />
   </asp:Calendar>
 </div><div> <asp:Label ID="Label5" runat="server" Text="IsActive" CssClass="label"></asp:Label> </div> <div><asp:CheckBox ID="chkactive" runat="server"  /></div><div>
   <asp:Label ID="Label6" runat="server" Text="Status" CssClass="label"></asp:Label>   </div>   <div><asp:CheckBox ID="chkstactive" Text="Active" runat="server" Checked="true" /> <asp:CheckBox ID="chknotactive" Text="NotActive" runat="server" /></div><br /><div><asp:Button ID="Btnsubmit" runat="server" Text="Submit" CssClass="bluebutton" onclick="Btnsubmit_Click"  />  </div></fieldset></ContentTemplate></asp:UpdatePanel>
</div>
<div>

   <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="True"><ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="#CC33FF" BorderColor="Black" ForeColor="White">
            <AlternatingRowStyle BackColor="#666633" />
            <Columns>
                <asp:BoundField DataField="Code" HeaderText="Code" />
                <asp:BoundField DataField="Author" HeaderText="Author" />
                <asp:BoundField DataField="AER" HeaderText="ABR" />
            </Columns>
            <HeaderStyle BackColor="Black" />
            <RowStyle BackColor="#666699" />
            <SelectedRowStyle BackColor="#00FF99" />
        </asp:GridView> </ContentTemplate><Triggers><asp:AsyncPostBackTrigger ControlID="Btnsubmit" EventName="Click" /></Triggers>   </asp:UpdatePanel>        </div>    </form></body>

数据在表格中成功更新,在插入gridview未列出最新记录后,如果我再次刷新页面,数据将显示。请帮助...........

      protected void Btnsubmit_Click(object sender, EventArgs e)
    {OleDbConnection conn = new OleDbConnection(); conn.ConnectionString =ConfigurationManager.ConnectionStrings["conn"].ToString();conn.Open();            
       string query = "insert into Register
       (Code,Author,AER,PublishDate) values('" + txtcode.Text + "','" +
       txtauthor.Text + "','" + txtabr.Text + "','" + txtpublishdate.Text + "')";
        OleDbCommand cmd = new OleDbCommand(query, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        BindGrid();
        Clear();
    }
    protected void BindGrid()
    {

2 个答案:

答案 0 :(得分:0)

将代码添加到BindGrid()方法,以将datagridview重新绑定到新数据。

这样的事情:

// Set up the data source.
bindingSource.DataSource = GetData("Select * From Register");
GridView1.DataSource = bindingSource;

答案 1 :(得分:0)

在您的BindGrid数据中添加以下代码

           con.Open();
           SqlCommand cmd = new SqlCommand("Select * from Register", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
             con.Close();
             if (ds.Tables[0].Rows.Count > 0)
             {
                Gridview1.DataSource = ds;

               Gridview1.DataBind();
               UpdatePanel1.Update();
              }