Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Datagrid NavigateURL


Message #1 by "Harish" <harish@h...> on Mon, 11 Mar 2002 14:12:53 -0600
Hi,



How do I generate NavigateURL of a datagrid dynamically?



Here is my situation: I have a parent window on which I have a text box

field called "SIC". And I have a search button next to the text box. When

the user clicks on the search button I display the result set, which

contains apart from SIC, Description etc., in a child window as a datagrid.

Now I would like to display a link next to each record in the child window

so when the user clicks on a record the corresponding SIC code can be

automatically populated into parent "SIC" text box. My problem is I can not

figure out how to set datagrid container value to NavigateURL.



Thanks in advance.



Harish.



Message #2 by "Goh Mingkun" <mangokun@h...> on Wed, 13 Mar 2002 03:19:47
For your case, you should run 'Microsoft Visual Studio .NET Documentation'

and read on the 'TemplateColumn class'



It would say:

Use the TemplateColumn column type in a DataGrid control to create a 

column with a customized 



control layout. You can provide a custom appearance for the items section 

of the column by 



using the ItemTemplate.



------------------------------------------------------------

[1st method]

After reading that, here is some suggestion of what you can put in 

your .aspx page for the 



above column.



<asp:DataGrid ...

<ItemTemplate>

<a href="javascript:void(0)" onclick="opener.form1.textbox1.value='<%# 



DataBinder.Eval(Container.DataItem, "SIC_code") %>'">Use code</a>

</ItemTemplate>

... /asp:DataGrid>



(I have not tested whether the above will work.)

One problem of the above method is that your code cannot contain the 

character ' or " because 



they are characters that identify the start and end of a string.



To solve that, u must use "String Escape Codes", visit:

http://www.messiah.edu/acdept/depthome/mathsci/javascri/CODE/chap6/strings.

htm

or, 

http://web-wise-wizard.com/web-lookup-lists/string-escape-codes-

explained.html





Try putting this in a .htm file and test it in a browser.

<script language=javascript>

function passtotextbox(str)

{

form1.text1.value=str

}

</script>

<a href="javascript:void(0)" onclick="passtotextbox('You want a neat box 

like \'this\x22 



one?')">Use code</a>

<form id=form1>

<input type=text id=text1 size=50>

</form>





------------------------------------------------------------

Instead of all the stuff above,

It will be better/faster if you use the [2nd method], which I will be 

telling u now.

In code-behind of the child window, iterate through the dataset and save 

the code of each row 



as a separate textfile, with your SIC as the filename.

And when the user click on 'Use code', u just to pass the SIC(not the code 

anymore) to the 



parent window's function.

This function that should open and load the file into your 'textbox'.

You may or may not have to convert the special characters.



------------------------------------------------------------



> How do I generate NavigateURL of a datagrid dynamically?

> 

> Here is my situation: I have a parent window on which I have a text box

> field called "SIC". And I have a search button next to the text box. When

> the user clicks on the search button I display the result set, which

> contains apart from SIC, Description etc., in a child window as a 

datagrid.

> Now I would like to display a link next to each record in the child 

window so when the user 



clicks on a record the corresponding SIC code can be automatically 

populated into parent "SIC" 



text box. My problem is I can not figure out how to set datagrid container 

value to 



NavigateURL.
Message #3 by "Goh Mingkun" <mangokun@h...> on Wed, 13 Mar 2002 03:20:51
Although the following message is not useful to you, u may want to know it.



If you just want one of your DataGrid Column to be a Hyperlink Column, 

here are the steps to do so:

(This can only be applied if you have created a instance of your dataset 

in designer view.)



In Designer View,

Right-click on your DataGrid,

Select Property Builder...



Go to the General Tab,

Set the 'DataSource' to you dataset instance.

'DataMember' to the table in your dataset instance.

'Data key field' maybe set to your primary data field.



Go to the Columns Tab,

Uncheck 'Create columns automatically at run time'

Add all your normal Data Fields (which does not include the hyperlink 

column) from the 'Available columns' to the 'Selected columns'.

Now add a 'HyperLink column' from the 'Available columns' to the 'Selected 

columns'.

Change 'Text' to any word you want to appear in the column.

For 'URL field', select a data field in your dataset (which should 

contains values like 'http://site.com/details.aspx').

For 'Target', you may choose '_blank' from the dropdownlist, if you the 

hyperlinked page to be in a new window.



In code-behind (probably in the Page_Load event, 

dsResult1 = ws.GetSearchResults(response.querystring("sic"))

DataGrid1.DataBind()

Message #4 by "Goh Mingkun" <mangokun@h...> on Wed, 13 Mar 2002 06:21:24
Sorry, the previous JavaScript code is broken over several lines, causing 

the browser to unable intepete the code.

Paste this in your .htm file instead.



<script language=javascript>

function passtotextbox(str){form1.text1.value=str}

</script>



<a href="javascript:void(0)" onclick="passtotextbox('You want a neat box 

like \'this\x22 one?')">Use code</a>

<form id=form1>

<input type=text id=text1 size=50>

</form>

  Return to Index