Hi,

I have a Edit Button(which is a Image Button) and when I click that ImageButton a new browser window should open and it should load the values previously entered by the user. The new window should contain the old values and now should be able to edit them.

For example,
If a user clicks on the Edit button whose userId= 329 it should open in a new window with the values entered by the user.

<%--<asp:ImageButton ID="ibtnEdit" runat="server" Text="Edit" ToolTip="Edit" ImageUrl="~/images/edit-icon.gif" />--%>
<%--<script type="text/javascript">
function EditInNewWindow()
{
window.open("abc.aspx?bid="+document.getElementById('<%#Eval("UserID") %>'));
}

</script>--%>

UserId id the column name where the userID is stored.


Can anyone plzz help.

Dani AI

Generated

The original attempt mixes server-side binding and a client-side DOM lookup in a way that won't reliably work. <%# Eval(...) %> runs on the server during data binding, while document.getElementById(...) expects a client-side element id; ASP.NET naming containers also change client IDs, so hard-coded client IDs frequently mismatch. 's server-side script registration is a valid fallback for UpdatePanel/partial-postback scenarios, but client-side links are simpler and less likely to be blocked by popup blockers.

A simple, robust option is to emit a link that opens in a new tab/window and carries the ID in the query string. In a TemplateField or ItemTemplate use a HyperLink containing an image:

<asp:HyperLink ID="hlEdit" runat="server"
  NavigateUrl='<%# Eval("UserID", "Edit.aspx?UserID={0}") %>'
  Target="_blank">
  <asp:Image runat="server" ImageUrl="~/images/edit-icon.gif" />
</asp:HyperLink>

For a GridView the HyperLinkField alternative keeps markup even simpler:

<asp:HyperLinkField DataNavigateUrlFields="UserID"
  DataNavigateUrlFormatString="Edit.aspx?UserID={0}"
  Text="Edit" Target="_blank" />

If an ImageButton must be used, bind a client-side window.open directly so the click is a user action (reduces popup-blocker issues). Example inside a data-bound template:

<asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="~/images/edit-icon.gif"
  OnClientClick='<%# String.Format("window.open(\"Edit.aspx?UserID={0}\",\"_blank\",\"width=800,height=600\"); return false;", Eval("UserID")) %>' />

On the edit page, read and validate the ID, then load the record:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack && int.TryParse(Request.QueryString["UserID"], out int id))
    {
        // fetch record for id and populate controls
    }
}

Notes: always validate permissions and input server-side (query strings can be altered). If working inside an UpdatePanel, registering a startup script from server-side (as suggested) is appropriate. Browser behavior (new tab vs window) and popup blocking depend on user settings.

This is what i use if i want to open a new page.

I create a static class like this.

public static void OpenWebSite(string webUrl, Control cntrl)
        {
            ScriptManager.RegisterStartupScript(cntrl, cntrl.GetType(), "Open", "window.open('" + webUrl + "');", true);
        }

And then whenever i want to open a particular web site i do this in c# code
this code below goes within the imagebuttonclick event.

OpenWebSite("yoururl.com?parm=paravalue", this);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.