Reputation: 1044
I have a Gridview control in asp.net with C#.
In my application when I press the edit button from Gridview I need to open the information in another window. At this moment I use 'Response.Redirect("..")' but it opens in the same window.
I've tried:
protected void OutputGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
outputGridView.EditIndex = e.NewEditIndex;
GridViewRow row = outputGridView.Rows[outputGridView.EditIndex];
string url = "http://localhost/MyPage.aspx";
Response.Write("<script>");
Response.Write("window.open('" + url + "')");
Response.Write("<" + "/script>")
e.Cancel = true;
}
But with no luck.
What's the best way to do this?
Upvotes: 2
Views: 6369
Reputation: 5128
Instead of writing directly to the response stream try this:
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenNewWindow", "window.open(...)", true);
Upvotes: 0
Reputation:
ASPX: Create a gridview like this
<asp:GridView runat="server" AllowPaging="True" AutoGenerateColumns="False" ID="gvSticker"
Width="100%" EmptyDataText="No sticker found for this Fisher. Click on add to Add a new sticker."
HorizontalAlign="Left" ShowFooter="True" ShowHeaderWhenEmpty="True" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" PageSize="3"
OnRowDataBound="gvSticker_RowDataBound" OnPageIndexChanging="gvSticker_PageIndexChanging">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<table style="width: 100%">
<tr align="left">
<td style="width: 15%">
Sticker Year
</td>
<td style="width: 15%">
Sticker Number
</td>
<td style="width: 15%">
Issue Date
</td>
<td style="width: 35%">
Issue Type
</td>
<td style="width: 20%">
Status
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table style="width: 100%">
<tr align="left">
<td style="width: 15%">
<%# Eval("YearOfIssue")%>
</td>
<td style="width: 15%">
<asp:HyperLink ID="hlStickerNumber" runat="server" Text='<%#Eval("StickerNumber")%>' Style="cursor: hand; text-decoration:underline"></asp:HyperLink>
</td>
<td style="width: 15%">
<%# Eval("DateOfIssue", "{0:MM/dd/yyyy}")%>
</td>
<td style="width: 35%">
<%# Eval("RegistrationType")%>
</td>
<td style="width: 20%">
<asp:Label ID="lblStickerStatus" runat="server" Text='<%# Eval("StickerStatus")%>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle HorizontalAlign="Left" BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle HorizontalAlign="Left" ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
In data bound event add the code.
protected void gvSticker_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (Session["FisherId"] != null)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblStatus = (Label)e.Row.FindControl("lblStickerStatus");
if (e.Row.RowIndex == 0)
{
if (lblStatus.Text.Contains("Active"))
{
btnAddSticker.Enabled = false;
HyperLink hlStickerNum = (HyperLink)e.Row.FindControl("hlStickerNumber");
if (!string.IsNullOrEmpty(hlStickerNum.Text.Trim()))
{
string urlWithParameters = "Stickers.aspx?StickerId=" + hlStickerNum.Text; hlStickerNum.Attributes.Add("OnClick", "popWinNote('" + urlWithParameters + "')");
}
}
else
{
btnAddSticker.Enabled = true;
btnVoidSticker.Enabled = true;
}
}
}
}
else
{
btnAddSticker.Enabled = true;
btnVoidSticker.Enabled = true;
}
}
And in aspx page add a function in script tag PopWinNote to open a show modal dialog if you need a small window. This code is not actually your required code but might give you idea regarding the implementation of logic
Upvotes: 0
Reputation: 55200
Try this
protected void OutputGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
outputGridView.EditIndex = e.NewEditIndex;
GridViewRow row = outputGridView.Rows[outputGridView.EditIndex];
string url = "http://localhost/MyPage.aspx";
StringBuilder sb = new StringBuilder();
sb.AppendLine("<script type='text/javascript'>");
sb.AppendLine("window.open('" + url + "')");
sb.AppendLine("<" + "/script>");
ClientScript.RegisterStartupScript(this.GetType(), "myjs", sb.ToString(), false);
//or if the gridview is inside an updatepanel do the code given below
//ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "myjs", sb.ToString(), false);
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception err)
{
//handle error here
//Elmah.ErrorSignal.FromCurrentContext().Raise(err);
}
}
As I have commented in the code above, if you are using ScriptManager, uncomment and use this
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "myjs", sb.ToString(), false);
I have tested it and its working.
Upvotes: 2
Reputation: 2898
This code will open the window in new window. for this you need to use the ScriptManager
string BrowserSettings = "status=no,toolbar=no,menubar=no,location=no,resizable=no,"+
"titlebar=no, addressbar=no, width=600 ,height=750";
string URL = "http://localhost/MyPage.aspx";
string scriptText = "window.open('" + URL + "','_blank','" + BrowserSettings + "');";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ClientScript1", scriptText, true);
Upvotes: 1