Yasir Attiq Butt – Bloggs! – Returning Values from PopUP in ASP.net

🙂 Few days back i got a problem that i want to get values from pop up using javascript from ASP.net application. from the help of and guidance of my colleages i figure out a way of doing it . here are steps to do it.

– First Create a parent page
– Second Create a Popup Page

1. Add a hidden html text box control to page where you need the values (Parent). Set it to runat server (hidValue)

2. Add a HTML Button control to the page where you need values (Parent). set it to runat server (btndummy)

3. Add the javascript to open the popup window set it size and attributes (Parent).

function SKUWindow()
{
window.open(‘SKULookup.aspx’,null,
‘height=300,width=362,status=yes,toolbar=no,menubar=no,location=no’);
}

4. Add javascript caller function in to the button from you need to generate popup in my case there was a grid where i need to have button on every row here is the code ‘Check if it is an item or alterning item then run the code below (Parent)
Dim btnLookup As Button
Dim i As Integer
btnLookup = e.Item.FindControl(‘btnLookup’)
If Not IsNothing(btnLookup) Then
btnLookup.Attributes.Add(‘OnClick’,’javascript:return SKUWindow();’)
End If

if Button or any other thing else then
Button.Attributes.add(‘OnClick’, ‘javascript:return SKUWindow();’)

5. In the Popup window form add the javascript to get and return the values to the parent form. here is the sample.
function SetValues(SKU, Description, Price)
{
var Value = SKU + “/” + Description + “/” + Price;
window.opener.document.forms[0].hidValue.value=Value;
window.opener.document.forms[0].btndummy.click();
window.close();
return false;
}

Add this function to any place where you need to call the actions to of send values in my case there was hyperlink with Select. (Child)

Dim hypSel As HyperLink
Dim i As Integer
hypSel = e.Item.FindControl(“lnkSelect”)
If Not IsNothing(hypSel) Then
hypSel.NavigateUrl = “javascript:SetValues(‘” & e.Item.DataItem(“SKU”) & “‘,'” & e.Item.DataItem(“Price”) & “‘,'” & e.Item.DataItem(“Description”) & “‘)”
End If

if Button or any other thing else then
Button.Attributes.add(“OnClick”, “javascript:SetValues(“Value1″,”Value2″,”Value3″)”)

The above function receives all 3 values as parameters which i need to send to parent page. Concatinate the values into one string and set these values to parent forms hiddenTextbox control created in step 1 and call the onClick event of HTML button control created in step 2. Close the current window and return false to end call.

When you will click the Select button current window will be closed and the parent form will call the OnClick Event of HTML button control created in step 2 and values of the pop up will be written to the HTML text box. Add code the event in server side to get the values from HTMl Text box and do what ever you want with them in my case i create a row of item and add them to a a database and show on the grid. so here is the code

Dim drSKU As DataRow
Dim strValue As String

strValue = hidValue.Value

Dim arrChar(0) As Char
Dim arrValues() As String

dtSKU = CType(ViewState.Item(“SKU”), DataTable)

arrChar(0) = “/”
arrValues = strValue.Split(arrChar)

If arrValues.Length 0 Then
drSKU = dtSKU.NewRow
drSKU(0) = Request.QueryString(“CustomerNo”)
””
End If

i figure out that some time it could be a requirement from the client so it is very easy way to get values from the server. I would like to thank Ihsan and danish for the guidance.

Thanks and Regards

Yasir Attiq Butt

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s