Friday, September 30, 2005

How to open new window using �HyperLinkColumn� in datagrid.

How to open new window using “HyperLinkColumn” in datagrid.

Client Side

Opening window from Hyperlink column problem comes “In Parent Window text [Object] comes and child window open properly” When you try to open window like this

Example:

DataNavigateUrlFormatString ="javascript:varedit window.open('LoyaltyRewardDetail.aspx?RewardID={0}',

'childwindow','width=600,height=450,scrollbars=yes, resizable=yes')"

Solution

Insead of “varedit” type “void” keyword then parent window will remain same.

Example;

<asp:BoundColumn

DataField="InvoiceId"

HeaderText="Invoice Id" >

</asp:BoundColumn>

<asp:HyperLinkColumn

DataTextField="programname"

DataNavigateUrlField="RewardID"

HeaderText="Program Name"

DataNavigateUrlFormatString =

"javascript:void window.open('LoyaltyRewardDetail.aspx?RewardID={0}',

'childwindow','width=600,height=450,scrollbars=yes, resizable=yes')" >

</asp:HyperLinkColumn>

Server Side

1) Add OnItemDataBound="PointHistoryBinding" into datagrid.

2) Bound the column as it in client side.

2) write code behind C#.net (in this way you can add multiple parameter in queryString

protected void LoyalityHistoryBinding( object sender, DataGridItemEventArgs e )

{

if( e.Item.ItemType == ListItemType.Header )

{

return;

}

string ProgID = e.Item.Cells[5].Text;

string RetailerId = e.Item.Cells[6].Text;

string ProgName = e.Item.Cells[3].Text;

string VersionId = e.Item.Cells[4].Text;

string ReachDate = e.Item.Cells[1].Text;

string passPageLink = "<a href='#'

onclick=\"window.open('LoyaltyDetail.aspx?ProgId={0}&ReachDate={1}&ProgName={2}&VersionId={3}&RetailerId={4}','childwindow','width=600,height=450,

scrollbars=yes, resizable=yes');\">{2}</a>";

string passType = Convert.ToString( DataBinder.Eval(e.Item.DataItem, "ProgramName") ).Trim();

e.Item.Cells[3].Text = String.Format(passPageLink,ProgID,

ReachDate,ProgName,VersionId,RetailerId,passType);

}

Another way to use Hyperlink column in datagrid with more then one parameter QueryString

("How do I specify more than one parameter for my HyperlinkColumn?")

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">

<Columns>

<asp:TemplateColumn HeaderText="Sample Column">

<ItemTemplate>

<asp:Hyperlink runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ProductName").ToString()%>' NavigateUrl='<%# "page1.aspx?id=" + DataBinder.Eval(Container.DataItem,"ProductId").ToString() + "&Name=" + DataBinder.Eval(Container.DataItem,"ProductName").ToString()%>' ID="Hyperlink1" NAME="Hyperlink1"/>

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:DataGrid>





Ritesh Kumar Kesharwani

A D I T I , B A N G A L O R E
Software Professional
Cell : 011-91-9845657636






Yahoo! for Good
Click here to donate to the Hurricane Katrina relief effort.

No comments: