C# 如何使用更新面板在不重新加载的情况下更新页面#

C# 如何使用更新面板在不重新加载的情况下更新页面#,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridview,它包含一个edit和一个delete命令 我希望当我点击编辑,更新值,然后点击删除时,该值会从数据库中删除,页面不会重新加载 现在,我使用更新面板和脚本管理器,但是页面再次被重新加载,更新面板不工作。我的另一个问题是,当我将表单标记放在gridview之前时,它工作正常,gridview显示,但当我删除此标记时,会出现错误: 对象引用未设置实例对象 我的aspx代码是: <asp:Content ID="Content2" ContentPlaceHolderID

我有一个gridview,它包含一个edit和一个delete命令

我希望当我点击编辑,更新值,然后点击删除时,该值会从数据库中删除,页面不会重新加载

现在,我使用更新面板和脚本管理器,但是页面再次被重新加载,更新面板不工作。我的另一个问题是,当我将
表单标记放在gridview之前时,它工作正常,gridview显示,但当我删除此标记时,会出现错误:

对象引用未设置实例对象

我的aspx代码是:

<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate>
<form runat="server"></form>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="Database_id" Height="184px" 
    onrowcancelingedit="GridView1_RowCancelingEdit" 
    onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
    onrowupdating="GridView1_RowUpdating" style="margin-left: 181px" Width="361px" 
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField HeaderText="Database Name">
            <EditItemTemplate>
                <asp:TextBox ID="txtDatabaseName" runat="server" Height="22px" 
                    Text='<%# Eval("Database_Name") %>' Width="192px"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Database_Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
            <EditItemTemplate>
                <asp:TextBox ID="txtdescription" runat="server" Height="24px" 
                    Text='<%# Eval("Description") %>' Width="209px"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("Date") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField HeaderText="Operations" ShowDeleteButton="True" 
            ShowEditButton="True" />
    </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</ContentTemplate></asp:UpdatePanel>
</asp:Content>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            binddata();
        }
    }
    private void binddata()
    {
    string con = "Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True";

     DataTable dt = new DataTable();
     using (SqlConnection cnn = new SqlConnection(con))
     {
            string user = Session["name"].ToString();
            SqlCommand cmd2 = new SqlCommand("SELECT User_ID from tbl_user WHERE User_Name='" + user + "'", cnn);
            cnn.Open();
            string id = cmd2.ExecuteScalar().ToString();
            int ID = Int32.Parse(id);
            SqlDataAdapter da = new SqlDataAdapter("SELECT Database_id,Database_Name,Description,Date FROM Create_db WHERE User_ID='" + ID + "'", cnn);
            da.Fill(dt);
            if (dt.Rows.Count > 0)
     {
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        delete(id);
        GridView1.EditIndex = -1;
   }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        TextBox txtDatabaseName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDatabaseName");
        TextBox txtdescription = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdescription");
        updatedb(txtDatabaseName.Text, txtdescription.Text, id);
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
      GridView1.EditIndex = e.NewEditIndex;
     binddata();
     }
    private void updatedb(string dbname, string Dis, int id)
    {
    string con = "Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True";
     using (SqlConnection cnn = new SqlConnection(con))
     {
     string query = "UPDATE Create_db SET Database_Name='" + dbname + "',Description='" + Dis + "' WHERE Database_id='" + id + "' ";
     SqlCommand cmd = new SqlCommand(query, cnn);
     cnn.Open();
     cmd.ExecuteNonQuery();
     }
     }
    private void delete(int id)
    {
        string con = "Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True";
        using (SqlConnection cnn = new SqlConnection(con))
        {
        string query = "DELETE FROM Create_db WHERE Database_id='" + id + "' ";     
      SqlCommand cmd = new SqlCommand(query, cnn);
       cnn.Open();
       cmd.ExecuteNonQuery();
     }
     }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

您的代码格式不正确,UpdatePanel在
表单
之外,gridview也在
表单
之外。一定是这样的

<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView>
...

 </asp:GridView>
 </ContentTemplate>
 </asp:UpdatePanel>
</form>

...

您的代码格式不正确,UpdatePanel在
表单
之外,gridview也在
表单
之外。一定是这样的

<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView>
...

 </asp:GridView>
 </ContentTemplate>
 </asp:UpdatePanel>
</form>

...
第一件事:

<%-- Your code (fragment) --%>
  <EditItemTemplate>
     <asp:TextBox ID="txtDatabaseName" runat="server" Height="22px" 
         Text='<%# Eval("Database_Name") %>' Width="192px"></asp:TextBox>
  </EditItemTemplate>
下一个。我建议您首先使用
第一件事:

<%-- Your code (fragment) --%>
  <EditItemTemplate>
     <asp:TextBox ID="txtDatabaseName" runat="server" Height="22px" 
         Text='<%# Eval("Database_Name") %>' Width="192px"></asp:TextBox>
  </EditItemTemplate>

下一个。我建议您在更新Panel之前使用
别忘了
。太好了!!!!!!!1非常感谢,但它现在工作了一半,当页面加载时,当我单击“编辑”gridview时,然后页面不重新排序,这很好,但当我在编辑后单击“更新”,然后页面重新加载,然后更新时,我希望当我单击“更新”时,页面不会重新排序。请告诉我如何执行此操作,谢谢:)@Hameed您是否还包括scriptmanager?按照Alex的建议,我已经更新了答案。。。同时在浏览器上打开控制台,查看是否有错误。@如果您得到
对象引用未设置实例对象,请立即重试。
,移除临时更新面板以定位错误行并首先修复+1@Aristos是的,我包括脚本管理器,但当我在gridview中单击“编辑”,然后页面未重新加载其良好状态,我在gridview中更新值,当单击更新,然后页面在重新加载后重新加载,然后值更新,但我不希望页面更新当我点击UpdatePanel时重新加载不要忘记UpdatePanel之前的
。哦,太棒了!!!!!!!1非常感谢,但它现在工作了一半,当页面加载时,当我单击“编辑”gridview时,然后页面不重新排序,这很好,但当我在编辑后单击“更新”,然后页面重新加载,然后更新时,我希望当我单击“更新”时,页面不会重新排序。请告诉我如何执行此操作,谢谢:)@Hameed您是否还包括scriptmanager?按照Alex的建议,我已经更新了答案。。。同时在浏览器上打开控制台,查看是否有错误。@如果您得到
对象引用未设置实例对象,请立即重试。
,移除临时更新面板以定位错误行并首先修复+1@Aristos是的,我包括脚本管理器,但当我在gridview中单击“编辑”,然后页面未重新加载其良好状态,我在gridview中更新值,当单击更新,然后页面在重新加载后重新加载,然后值更新,但我不希望页面更新当我点击更新时重新加载