Wednesday, June 23, 2010

entered value from child window to parent window

From a child window or a small window once opened, we can transfer any user entered value to main or parent window by using JavaScript. You can see the demo of this here. Here the parent window is known as opener. So the value we enter in a child window we can pass to main by using opener.document object model. So if the name of the form in parent window is f1 and the text box where we want the value to be passed is p_name ( in parent window ) then from the child window we can access them by using the object. opener.document.f1.p_name The value for this object can be assigned like this Related Tutorial• Refreshing the parent window• Opening Windowopener.document.f1.p_name.value="Any value";Related Tutorial• Refreshing the parent window• Opening Window We will try to make it interactive so we will assign this to a value entered by the user. Then we will use one input box in child window and name it as c_name. So we can pass the value of the input box of child window to the parent window input box by this line. opener.document.f1.p_name.value = document.frm.c_name.value; We will keep this line inside a function and call this function on click of a button. Inside the function after executing the above line we will add the code to close the child window. Like this .. opener.document.f1.p_name.value = document.frm.c_name.value;self.close();Here is the demo, Click the button ( take care of popup blocker of your browser ) and enter your name in child window opened. Then once closed it will be available at parent window. Your Name Click here to open the child window To open the child window this is the code used in parent window

<form method=post action='' name=f1>

<table border=0 cellpadding=0 cellspacing=0 width=550> <tr>

<td ><font size=2 face='Verdana'>Your Name</font><input type=text name='p_name' size='8'> 

<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " 

onClick=window.open("child3.html","Ratting",

"width=550,height=170,left=150,top=200,toolbar=1,status=1,");>Click here to open the child window</a>

</td></tr> </table></form>

Inside the Child window code is here

<html>

<head>

<script langauge="javascript">

function post_value(){

opener.document.f1.p_name.value = document.frm.c_name.value;

self.close();

}

</script>

<title>(Type a title for your page here)</title>

</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000"> <form name="frm" method=post action=''>

<table border=0 cellpadding=0 cellspacing=0 width=250>

<tr><td align="center"> Your name<input type="text" name="c_name" size=12 value=test> <input type=button value='Submit' onclick="post_value();">

</td></tr>


</table></form>

How-to use ClientIDs in JavaScript

with their parent’s ID.


<div id="parent" runat="server">
<asp:TextBox ID="txtUsername" Runat="server" />
</div>

This isn’t really much of a problem when all you are working with is server-side code, but when you start tinkering with JavaScript, things become quite annoying and get an overall feeling of hackyness.

One solution, which I have used time-and-again, is to use a script block at the end of your page where you create a series of variables that contain the actual IDs. You then use these variables to reach the actual elements via JavaScript.

<script type="text/javascript">
var txtUsernameID = '<%= txtUsername.ClientID %>';
var txtPasswordID = '<%= txtPassword.ClientID %>';
</script>

This solution works fine, but it isn’t exactly pretty and it doesn’t weigh well on my conscience.

Thursday, March 11, 2010

Function to return a table from a delimitted (csv) string

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_ParseCSVString]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fn_ParseCSVString]
GO
create function fn_ParseCSVString
(
@CSVString varchar(8000) ,
@Delimiter varchar(10)
)
returns @tbl table (s varchar(1000))
as
/* select * from dbo.fn_ParseCSVString ('qwe,c,rew,c,wer', ',c,') */
begin
declare @i int ,
@j int
select @i = 1
while @i <= len(@CSVString)
begin
select @j = charindex(@Delimiter, @CSVString, @i)
if @j = 0
begin
select @j = len(@CSVString) + 1
end
insert @tbl select substring(@CSVString, @i, @j - @i)
select @i = @j + len(@Delimiter)
end
return
end
GO

Function to return all non-alphameric characters from a string in a table.

create function GetCtrl
(@str varchar(1000))
returns @t table (offset int, chr int)
as
begin
declare @i int, @j int
select @i = 1, @j = 1
while @i <= len(@str)
begin
select @j = @i
select @i = patindex('%[^a-zA-Z0-9 ]%', substring(@str,@i,len(@str)-@i+1))
if @i = 0
set @i = len(@str) + 10
else
begin
select @i = @i + @j -1
insert @t select @i, ascii(substring(@str,@i,1))
end
select @i = @i + 1
end
return
end
select * from dbo.GetCtrl ('sad%sa*s(sdfg*')
i chr
----------- -----------
4 37
7 42
9 40
14 42

Returns the number of rows in each table in a database.

if exists (select * from sysobjects where id = object_id(N'[dbo].[sp_GetRowsForAllTables]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_GetRowsForAllTables]

GO

Create Procedure sp_GetRowsForAllTables
@DBName varchar(128) = null
as
set nocount on
if @DBName is null
set @DBName = db_name()
create table #a (TableName varchar(128), norows int null, id int identity(1,1))
declare @id int ,
@maxID int ,
@TableName varchar(128) ,
@FKName varchar(128) ,
@cmd nvarchar(1000) ,
@rc int,
@spcmd varchar(1000)
set @cmd = 'exec ' + @DBName + '..sp_executesql N''insert #a (TableName) select TABLE_NAME from information_schema.tables where TABLE_TYPE = ''''BASE TABLE'''' '' '

exec (@cmd)

select @id = 0 , @maxID = max(id) from #a
while @id < @maxID begin select @id = min(id) from #a where id > @id

select @TableName = TableName from #a where id = @id

set @cmd = 'exec ' + @DBName + '..sp_executesql N''update #a

set norows = (select rows from sysindexes where indid in (0,1) and id = object_id(''''' + @TableName + '''''))'

set @cmd = @cmd + ' where #a.id = ' + convert(varchar(10),@id) + '''' exec (@cmd)

if @rc <> 0 or @@error <> 0
begin
raiserror('failed %s',16,-1,@TableName)
return
end
end
select * from #a
drop table #ago

About Me

Bangalore, Karnataka, India