简体   繁体   中英

How to refresh whole page before going into update mode in gridview(asp.net)?

I have a website where the admin can delete or add new gridview. For example, if i currently have gridview for giordano products and wanted to change the brand to bossini products. When the admin clicked edit button, the whole gridview and code for giordano products should be removed and if i'm already in update mode and changed the brand name to "bossini" and clicked update, It should add new gridview and code for bossini products.

Now here's the problem. When i do click the edit button, it will remove the whole gridview code but the gridview itself still appears in the website. I finally know that i need to refresh the page but i don't know the best way yet. I have tried Response.Redirect("urls") in GuitarBrandsGridView_RowEditing, but it is not working and it is giving me an exception. It works for GuitarBrandsGridView_RowDeleting tho. I just assumed that maybe it would work for my edit button but it failed.

My goal is, when i click the edit button in gridview, the whole webpage should refresh and then proceed to update mode. Hope this makes sense.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) {
        bindgridviewguitarbrands();
        BindGridViewDataList.GetItemsLoad();
    }
}

//Start of Gridview Code for Guitar Brands
private void bindgridviewguitarbrands()
{
    con1.Open();
    cmd1.CommandText = "SELECT * FROM [guitarBrands]";
    cmd1.Connection = con1;
    SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
    da1.Fill(ds1);
    con1.Close();
    GuitarBrandsGridView.DataBind();

}

protected void GuitarBrandsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string name = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));
        Button button = (Button)e.Row.FindControl("GuitarBrandsGridViewBtnDelete");
        button.Attributes.Add("onclick", "JavaScript:return ConfirmationBox('" + name + "' )");
    }
}

protected void GuitarBrandsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    int id = Convert.ToInt32(GuitarBrandsGridView.DataKeys[e.RowIndex].Value.ToString());
    Label name = (Label)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("lblName");

    RemoveCodeToGuitarFile.RemoveAddGuitarClass(name.Text);
    RemoveCodeToGuitarFile.RemoveConnectionClassGuitarItems(name.Text);
    RemoveCodeToGuitarFile.RemoveOverviewGuitarDataASPX(name.Text);
    RemoveCodeToGuitarFile.RemoveOverviewGuitarDataCode(name.Text);
    File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx");
    File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx.cs");
    ConnectionClassGuitarBrands.RemoveGuitarBrandsDatabase(name.Text);

    con1.Open();
    cmd1.CommandText = "DELETE FROM [guitarBrands] WHERE id=" + id;
    cmd1.Connection = con1;
    int a = cmd1.ExecuteNonQuery();

    con1.Close();

    if (a > 0) {
        bindgridviewguitarbrands();
    }

    Response.Redirect("~/Pages/OverviewGuitarData.aspx");

}

protected void GuitarBrandsGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    GuitarBrandsGridView.EditIndex = e.NewEditIndex;
    string id = GuitarBrandsGridView.DataKeys[e.NewEditIndex].Value.ToString();
    Label name = (Label)GuitarBrandsGridView.Rows[e.NewEditIndex].FindControl("lblName");

    RemoveCodeToGuitarFile.RemoveAddGuitarClass(name.Text);
    RemoveCodeToGuitarFile.RemoveConnectionClassGuitarItems(name.Text);
    RemoveCodeToGuitarFile.RemoveOverviewGuitarDataASPX(name.Text);
    RemoveCodeToGuitarFile.RemoveOverviewGuitarDataCode(name.Text);
    File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx");
    File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx.cs");
    ConnectionClassGuitarBrands.RemoveGuitarBrandsDatabase(name.Text);       

    bindgridviewguitarbrands();
    Response.Redirect("~/Pages/OverviewGuitarData.aspx");//this one is not working
}
// row update event
protected void GuitarBrandsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    // find student id of edit row
    string id = GuitarBrandsGridView.DataKeys[e.RowIndex].Value.ToString();
    // find updated values for update
    TextBox type = (TextBox)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("txtType");
    TextBox name = (TextBox)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("txtName");
    TextBox image = (TextBox)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("txtImage");


    cmd1 = new SqlCommand("UPDATE [guitarBrands] SET Type = '" + type.Text + "', Name = '" + name.Text + "', Image = '" + image.Text + "' WHERE ID = " + id, con1);
    con1.Open();
    cmd1.ExecuteNonQuery();
    con1.Close();

    int ID = Convert.ToInt32(id);
    ConnectionClassGuitarBrands.CreateGuitarBrandsDatabase(name.Text);
    AddCodeToGuitarFile.AddGuitarClassCode(name.Text, ID);
    AddCodeToGuitarFile.AddConnectionClassGuitarItems(name.Text);
    AddCodeToGuitarFile.AddOverviewGuitarDataASPX(name.Text, ID);
    AddCodeToGuitarFile.AddOverviewGuitarDataASPXCode(name.Text);
    AddASPXAndCSFileForGuitarBrands.AddFile(name.Text, ID);

    GuitarBrandsGridView.EditIndex = -1;
    bindgridviewguitarbrands();

}
// cancel row edit event
protected void GuitarBrandsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    string id = GuitarBrandsGridView.DataKeys[e.RowIndex].Value.ToString();
    TextBox name = (TextBox)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("txtName");

    int ID = Convert.ToInt32(id);
    ConnectionClassGuitarBrands.CreateGuitarBrandsDatabase(name.Text);
    AddCodeToGuitarFile.AddGuitarClassCode(name.Text, ID);
    AddCodeToGuitarFile.AddConnectionClassGuitarItems(name.Text);
    AddCodeToGuitarFile.AddOverviewGuitarDataASPX(name.Text, ID);
    AddCodeToGuitarFile.AddOverviewGuitarDataASPXCode(name.Text);
    AddASPXAndCSFileForGuitarBrands.AddFile(name.Text,ID);

    GuitarBrandsGridView.EditIndex = -1;
    bindgridviewguitarbrands();
}

you need not refresh whole page

GuitarBrandsGridView.DataSource = ds1
GuitarBrandsGridView.DataBind();

then will update new data

Response.Redirect("http://localhost:14999/Home/Index");

Where you want your page to refresh you need to give full path of the page it will reload that page. I hope it will work coz its working for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM