How can i make a list of value window in asp.net 2.0 with c# and how i get value from GridView of Child Page to a server control [TextBox] in a Parent window.

Dani AI

Generated

The original question from asked how to open a "list of values" window and return a GridView selection into a parent TextBox. pointed to the ASP.NET forum but there was no follow-up example here. Two practical, proven patterns are below — both work in ASP.NET WebForms 2.0 and C#. Pick the one that fits your browser/support requirements.

Pattern 1 — popup + window.opener (recommended, cross-browser)
This keeps the logic simple: parent exposes a JS function to receive the value; child calls that function and closes itself.

// parent page (markup includes a TextBox with ID txtSelected)
function OpenLookup() {
  window.open('Lookup.aspx', 'lookup', 'width=600,height=400');
}
function SetLookupValue(val) {
  document.getElementById('<%= txtSelected.ClientID %>').value = val;
  // optional: __doPostBack('<%= txtSelected.UniqueID %>', '');
}
// Lookup.aspx.cs - GridView SelectedIndexChanged
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = GridView1.SelectedRow.Cells[1].Text;
    string safe = value.Replace("\\", "\\\\").Replace("'", "\\'").Replace("\r", "\\r").Replace("\n", "\\n");
    string script = "if (window.opener && !window.opener.closed) { window.opener.SetLookupValue('" + safe + "'); window.close(); }";
    ClientScript.RegisterStartupScript(this.GetType(), "returnValue", script, true);
}

Pattern 2 — modal dialog with returnValue (older, IE-centric)
Parent calls window.showModalDialog and child sets window.returnValue before closing. Note: showModalDialog is deprecated in modern browsers.

Troubleshooting & cautions

  • Parent and child must be same origin (no cross-domain).
  • If using MasterPages, always use ClientID when accessing elements from JS.
  • Sanitize/escape values before emitting into JS to prevent script errors or injection.
  • Some browsers block popups; consider an in-page modal (jQuery UI, Bootstrap) for better UX.

Recommended Answers

All 2 Replies

jbhjvghv

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.