0

In my GridView in C# ASP.NET 4 I insert a button to edit the row of GridView that open a new webpage on the browser in this mode

protected void btn1_Click(object sender, EventArgs e)
{
    ImageButton btn1 = (ImageButton)sender;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
    int oID = Convert.ToInt32(gv.DataKeys[row.RowIndex].Values[0]);

    string queryString = "newpage.aspx?oID=" + oID.ToString();
    string newWin = "window.open('" + queryString + "','_blank');";
    ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}

The newpage.aspx use Ajax and JSON for save an image on the server and close window in this mode

<script type="text/javascript">
    $(function () {
        $("#btnSave").click(function () {
            var image = document.getElementById("cc").toDataURL("image/png");
            image = image.replace('data:image/png;base64,', '');
            var qString = "?" + window.location.href.split("?")[1];
            $.ajax({
                type: 'POST',
                url: 'newpage.aspx/oImage' + qString,
                data: '{ "imageData" : "' + image + '" }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',

                success: function (msg) {
                    alert('Ok');
                    window.close();
                },

                failure: function (msg) {
                    alert(response.d);
                },

                error: function (msg) {
                    alert(response.d);
                },

                error: function (xhr, ajaxOptions, thrownError) {
                    alert("error : " + thrownError + JSON.stringify(image));
                }
            });
        });
    });
</script>

Now i need refresh the Gridview after closing window

I have tried withous success these single solution, but the parent page with the GridView is not refreshed and the edited row is always available

window.opener.location.reload(true);
window.close();

Or

parent.location.reload(true);
window.close();

Or

location.href = 'gv.aspx' + qString;
window.close();

Or

window.location.replace("gv.aspx")
window.close();

Or

window.location = result.getResponseHeader('gv.aspx');
window.close();
                 

Any suggestion?

Thanks for help.

code-behind

[WebMethod()]
public static void oImage(string imageData)
{
    string folderLocation = path + "\\" + DateTime.Now.ToString("ddMMyyyy") + "\\";
    bool exists = Directory.Exists(folderLocation);

    if (!exists)
    {
        Directory.CreateDirectory(folderLocation);
    }

    string fileNameWitPath = folderLocation +
        Guid.NewGuid() + "_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".png";

    mtsp(fileNameWitPath);

    using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
    {
        using (BinaryWriter bw = new BinaryWriter(fs))
        {
            byte[] data = Convert.FromBase64String(imageData);
            bw.Write(data);
            bw.Close();
        }
    }
}                                 
Chevy Mark Sunderland
  • 401
  • 2
  • 10
  • 21
  • Where did you put your code for refreshing the opener - in the ajax success handler? Did you check, if window.opener is the right window? – Homungus Oct 07 '20 at 13:50
  • @Homungus Yes the code for refreshing the opener page is in the ajax success handler. If trying `window.opener.location.reload(true);` the opener page is refreshed but the browser reopen again the window just closed – Chevy Mark Sunderland Oct 07 '20 at 13:56
  • Perhaps the __doPostBack-function can help you - see here: https://stackoverflow.com/questions/3591634/how-to-use-dopostback You can use it like window.opener.__doPostBack(...) – Homungus Oct 07 '20 at 14:02
  • @Homungus thanks but I don't know how to adapt it to my case sorry... – Chevy Mark Sunderland Oct 07 '20 at 15:00
  • Well, you need a post back to re-load/re-fresh the grid. Place a button with style=none, and then in jQuqery simply go: $('#MyBtn').Click() and it will click that button and run the code behind. In fact, since you are running code behind to re-fresh, then perhaps you send the image as a base64 in the on-row databind that fires for each row of the datagrid. – Albert D. Kallal Oct 07 '20 at 15:46
  • @AlbertD.Kallal thank you for reply, but don't have any example? – Chevy Mark Sunderland Oct 08 '20 at 06:40
  • You would have/place the one line of js click the asp.net button right after the line of code that closes the window. So $('#MyHiddenBtn').Click(); is how you would click on the button and that button is a standard asp.net button that then can run the code behind and do whatever you want - in this case re-load the grid. This is cleaner then a __dopostback. So the buttion click can work well. As for the on-row bind and image - well that's a different question. – Albert D. Kallal Oct 09 '20 at 04:59

0 Answers0