tags:

views:

40

answers:

4

For my internal webpage at work, I display a DataGrid based on entries in a SQL table (not directly, but with some processing on entries).

Each row in the DataGrid has a button that the user clicks. I need this button to open a new window or tab (I believe I can't decide as this is based on browser config) and also change a value in the SQL table to say the button was clicked.

If I use an asp:Hyperlink then the page opens nicely, but I don't know how to update SQL. Vice-versa if I use an asp:LinkButton I can get the SQL updated but can't get a new page to open.

Is what I am trying to do impossible?

Thanks

EDIT:

I've tried both these in my .cs file, but neither worked:

ClientScript.RegisterStartupScript(GetType(), "openwindow", "window.open('" + url + "','_preview'");

Response.Write("<script type='text/javascript'>detailedresults=window.open('" + url + "');</script>");
A: 

try this

Response.Write("<script type='text/javascript'>detailedresults=window.open('PAGE NAME.aspx');</script>")

EDIT:

you can set command name for link button and execute code in rowcocommand event

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("commandname for linkbutton"))
            {
                //update code 
                Response.Write("<SCRIPT language=\"javascript\">open('Yourpage.aspx','_blank','top=0,left=0,status=yes,resizable=yes,scrollbars=yes');</script>");

        }
    }
Vyas
A: 

You can use LinkButton and use OnClientClick property to set the java-script that would open the page in new browser window. This script must return true so that post-back can happen and you can handle click event on server side to update your database.

IMO, better way would be to use hyper-link and open the page in new browser window. However, you must pass a query-string parameter while opening the page (say Page2) that would tell the new page that it should update the database. So url for opening the page will be something like "page2.aspx?recordId=xyz". And within page2.aspx.cs, you will have code such as

protected void page_load(object sender, EventArgs e)
{
   if (!IsPostback)
   {
      var recordId = Response.QueryString["recordId"];
      if (!string.IsNullOrEmpty(recordId))
      {
         // update database
         ...
      }
   }
   ...
}

From security perspective, you may want to include time-out within parameter value and encrypt the entire thing.

VinayC
Having tried several methods to use ClientScript.RegisterStartupScript/Response.Write, I failed to get them working. This might be down to incorrect usage, but in the end I went with VinayC's suggestion. Implemented a new .aspx page that gets passed a QueryString. This page then updates SQL and redirects to the URL. Works perfectly for my needs. No security issues as this is purely internal work website. Thanks!
neildeadman
@neildeadman, RegisterStartupScript (to register script that opens the browser page) would have worked but main issue with that approach would have been pop-up blocker - new page opened with start-up script would be treated as a advertise/pop-up and would be blocked. You might have faced the same issue while trying other's solutions.
VinayC
A: 

You could use your LinkButton and call RegisterStartupScript on postback to execute something like this on page load

.aspx:

<asp:LinkButton OnClick="OnButtonClick" Text="Update" runat="server" />

.aspx.cs:

protected void OnButtonClick(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(GetType(), "openwindow", "<script type=text/javascript> window.open('About.aspx'); </script>");
}

You need to supply the whole script element to RegisterStartupScript.

Cubicle
A: 

Heperlink will not cal server side event on click so u wont get post back to perform the SQL task, and by using the LinkButton u can Open the page in the new window or tab to do so u need to add below little code in the Grid_RowBoundEvent Ex: LinkButton.OnClientClick="window.open('http://www.google.com');"; the url u can mentions as per ur requirment

and Again in the Code behind u will get server side event der u can do your sql task.

Hope it will be help full to you

Nitesh Katare