How to reference and populate controls on Content Page

Discussion in 'ASP .Net' started by =?Utf-8?B?TW9yaQ==?=, Jul 16, 2006.

  1. I am using masterPage and I need to populate a textbox that is in a content
    control with data from popup page that is not part of the master page. This
    code works if no masterpage is involved. here is the javascript produced:

    <script>window.opener.document.forms[0].txtEndDate.value =
    '7/15/2006';self.close()</script>

    I basically need to populate txtEndDate on the content page.

    if
    (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    {
    string strDate = calDate.SelectedDate.ToShortDateString();
    StringBuilder sb = new StringBuilder();
    sb.Append("<script>window.opener.document.forms[0]." +
    control.Value + ".value = '");
    sb.Append(strDate);
    sb.Append("';self.close()");
    sb.Append("</script>");
    string strCalScript = sb.ToString();
    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    "anything", sb.ToString());
    }
     
    =?Utf-8?B?TW9yaQ==?=, Jul 16, 2006
    #1
    1. Advertising

  2. Hi Mori,

    When you use master pages, the controls take on different IDs. If you
    execute the page and then do View > Source in the browser, can you see the
    "real" name of txtEndDate?

    Depending on where it is, it could be something like:

    ctl00_TextBox1

    or

    ctl00_ContentPlaceHolder1_TextBox1

    Ken
    Microsoft MVP [ASP.NET]

    "Mori" <> wrote in message
    news:...
    >I am using masterPage and I need to populate a textbox that is in a content
    > control with data from popup page that is not part of the master page.
    > This
    > code works if no masterpage is involved. here is the javascript produced:
    >
    > <script>window.opener.document.forms[0].txtEndDate.value =
    > '7/15/2006';self.close()</script>
    >
    > I basically need to populate txtEndDate on the content page.
    >
    > if
    > (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    > {
    > string strDate = calDate.SelectedDate.ToShortDateString();
    > StringBuilder sb = new StringBuilder();
    > sb.Append("<script>window.opener.document.forms[0]." +
    > control.Value + ".value = '");
    > sb.Append(strDate);
    > sb.Append("';self.close()");
    > sb.Append("</script>");
    > string strCalScript = sb.ToString();
    >
    > this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    > "anything", sb.ToString());
    > }
     
    Ken Cox [Microsoft MVP], Jul 16, 2006
    #2
    1. Advertising

  3. Thanks Ken: I have replaced txtEnd with ctl00_ContentPlaceHolder1_txtEndDate
    just as suggested. The javascript emitted is:

    <script>window.opener.ctl00_ContentPlaceHolder1_txtEndDate.value =
    '7/19/2006';self.close()</script>

    The rest of the code now looks like this:
    protected void Change_Date(object sender, System.EventArgs e)
    {
    if
    (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    {
    string strDate = calDate.SelectedDate.ToShortDateString();
    string strEnd = "ctl00_ContentPlaceHolder1_txtEndDate";
    StringBuilder sb = new StringBuilder();
    sb.Append("<script>window.opener." + strEnd + ".value = '");
    sb.Append(strDate);
    sb.Append("';self.close()");
    sb.Append("</script>");
    string strCalScript = sb.ToString();
    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    "anything", sb.ToString());
    }
    }


    However, the textbox is not populated and the popup form never closes.
    What could be wrong?


    "Ken Cox [Microsoft MVP]" wrote:

    > Hi Mori,
    >
    > When you use master pages, the controls take on different IDs. If you
    > execute the page and then do View > Source in the browser, can you see the
    > "real" name of txtEndDate?
    >
    > Depending on where it is, it could be something like:
    >
    > ctl00_TextBox1
    >
    > or
    >
    > ctl00_ContentPlaceHolder1_TextBox1
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    >
    > "Mori" <> wrote in message
    > news:...
    > >I am using masterPage and I need to populate a textbox that is in a content
    > > control with data from popup page that is not part of the master page.
    > > This
    > > code works if no masterpage is involved. here is the javascript produced:
    > >
    > > <script>window.opener.document.forms[0].txtEndDate.value =
    > > '7/15/2006';self.close()</script>
    > >
    > > I basically need to populate txtEndDate on the content page.
    > >
    > > if
    > > (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    > > {
    > > string strDate = calDate.SelectedDate.ToShortDateString();
    > > StringBuilder sb = new StringBuilder();
    > > sb.Append("<script>window.opener.document.forms[0]." +
    > > control.Value + ".value = '");
    > > sb.Append(strDate);
    > > sb.Append("';self.close()");
    > > sb.Append("</script>");
    > > string strCalScript = sb.ToString();
    > >
    > > this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    > > "anything", sb.ToString());
    > > }

    >
    >
    >
     
    =?Utf-8?B?TW9yaQ==?=, Jul 16, 2006
    #3
  4. Hi Mori,

    I think you'll have to some sample code from the pages because it isn't
    clear to me how you're opening the secondary window. Also, in your script,
    try using the JavaScript getElementById method to reference the textbox. The
    ID value is much easier than the Name to use in ASP.NET.

    Ken
    Microsoft MVP [ASP.NET]

    "Mori" <> wrote in message
    news:...
    > Thanks Ken: I have replaced txtEnd with
    > ctl00_ContentPlaceHolder1_txtEndDate
    > just as suggested. The javascript emitted is:
    >
    > <script>window.opener.ctl00_ContentPlaceHolder1_txtEndDate.value =
    > '7/19/2006';self.close()</script>
    >
    > The rest of the code now looks like this:
    > protected void Change_Date(object sender, System.EventArgs e)
    > {
    > if
    > (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    > {
    > string strDate = calDate.SelectedDate.ToShortDateString();
    > string strEnd = "ctl00_ContentPlaceHolder1_txtEndDate";
    > StringBuilder sb = new StringBuilder();
    > sb.Append("<script>window.opener." + strEnd + ".value = '");
    > sb.Append(strDate);
    > sb.Append("';self.close()");
    > sb.Append("</script>");
    > string strCalScript = sb.ToString();
    >
    > this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    > "anything", sb.ToString());
    > }
    > }
    >
    >
    > However, the textbox is not populated and the popup form never closes.
    > What could be wrong?
    >
    >
    > "Ken Cox [Microsoft MVP]" wrote:
    >
    >> Hi Mori,
    >>
    >> When you use master pages, the controls take on different IDs. If you
    >> execute the page and then do View > Source in the browser, can you see
    >> the
    >> "real" name of txtEndDate?
    >>
    >> Depending on where it is, it could be something like:
    >>
    >> ctl00_TextBox1
    >>
    >> or
    >>
    >> ctl00_ContentPlaceHolder1_TextBox1
    >>
    >> Ken
    >> Microsoft MVP [ASP.NET]
    >>
    >> "Mori" <> wrote in message
    >> news:...
    >> >I am using masterPage and I need to populate a textbox that is in a
    >> >content
    >> > control with data from popup page that is not part of the master page.
    >> > This
    >> > code works if no masterpage is involved. here is the javascript
    >> > produced:
    >> >
    >> > <script>window.opener.document.forms[0].txtEndDate.value =
    >> > '7/15/2006';self.close()</script>
    >> >
    >> > I basically need to populate txtEndDate on the content page.
    >> >
    >> > if
    >> > (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    >> > {
    >> > string strDate = calDate.SelectedDate.ToShortDateString();
    >> > StringBuilder sb = new StringBuilder();
    >> > sb.Append("<script>window.opener.document.forms[0]." +
    >> > control.Value + ".value = '");
    >> > sb.Append(strDate);
    >> > sb.Append("';self.close()");
    >> > sb.Append("</script>");
    >> > string strCalScript = sb.ToString();
    >> >
    >> > this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    >> > "anything", sb.ToString());
    >> > }

    >>
    >>
    >>
     
    Ken Cox [Microsoft MVP], Jul 17, 2006
    #4
  5. Here is all my code for anyone wishing to reproduce my situation. The popup
    Calendar page does not use masterpage. Only the page that calls popup does.

    I have very little code in the page that calls the calendar popup. here is
    all I havein the contentPlaceHolder:

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
    runat="Server" >
    <div>
    <table>
    <tr>
    <td align="right">
    Start Date:
    </td>
    <td align="left">
    <asp:TextBox ID="txtStartDate" runat="server" />
    </td>
    <td align="center">
    <a href="javascript:;"
    onclick="window.open('CalPage.aspx?textbox=txtStartDate','cal','width=250,height=225,left=270,top=180')">
    <img src="images/calendar.gif" border="0">
    </a>
    </td>
    </tr>
    <tr>
    <td align="right">
    End Date:
    </td>
    <td align="left">
    <asp:TextBox ID="txtEndDate" runat="server" />
    </td>
    <td align="center">
    <a href="javascript:;"
    onclick="window.open('CalPage.aspx?textbox=txtEndDate','cal','width=250,height=225,left=270,top=180')">
    <img src="images/calendar.gif" border="0">
    </a>
    </td>
    </tr>
    </table>
    </div>
    </asp:Content>

    For the CalendarPopup I have this in the aspx for the Calendar and hidden
    input box:
    <form id="form1" runat="server">
    <div>
    <asp:Calendar ID="calDate" OnSelectionChanged="Change_Date"
    runat="server" />
    <input type="hidden" id="control" runat="server" />
    </div>
    </form>

    I also have this codebehind to extract the value from the input field and
    respond to DateChangeEvent:

    protected void Page_Load(object sender, EventArgs e)
    {
    control.Value = Request.QueryString["textbox"].ToString();
    string strVal = control.Value;
    }

    and this to respond to the SelectionChange in the Calendar:
    protected void Change_Date(object sender, System.EventArgs e)
    {
    if
    (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    {
    string strDate = calDate.SelectedDate.ToShortDateString();
    string strEnd = "ctl00_ContentPlaceHolder1_txtEndDate";
    StringBuilder sb = new StringBuilder();
    sb.Append("<script>window.opener." + strEnd + ".value = '");
    sb.Append(strDate);
    sb.Append("';self.close()");
    sb.Append("</script>");
    string strCalScript = sb.ToString();
    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    "anything", sb.ToString());
    }
    }

    "Ken Cox [Microsoft MVP]" wrote:

    > Hi Mori,
    >
    > I think you'll have to some sample code from the pages because it isn't
    > clear to me how you're opening the secondary window. Also, in your script,
    > try using the JavaScript getElementById method to reference the textbox. The
    > ID value is much easier than the Name to use in ASP.NET.
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    >
    > "Mori" <> wrote in message
    > news:...
    > > Thanks Ken: I have replaced txtEnd with
    > > ctl00_ContentPlaceHolder1_txtEndDate
    > > just as suggested. The javascript emitted is:
    > >
    > > <script>window.opener.ctl00_ContentPlaceHolder1_txtEndDate.value =
    > > '7/19/2006';self.close()</script>
    > >
    > > The rest of the code now looks like this:
    > > protected void Change_Date(object sender, System.EventArgs e)
    > > {
    > > if
    > > (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    > > {
    > > string strDate = calDate.SelectedDate.ToShortDateString();
    > > string strEnd = "ctl00_ContentPlaceHolder1_txtEndDate";
    > > StringBuilder sb = new StringBuilder();
    > > sb.Append("<script>window.opener." + strEnd + ".value = '");
    > > sb.Append(strDate);
    > > sb.Append("';self.close()");
    > > sb.Append("</script>");
    > > string strCalScript = sb.ToString();
    > >
    > > this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    > > "anything", sb.ToString());
    > > }
    > > }
    > >
    > >
    > > However, the textbox is not populated and the popup form never closes.
    > > What could be wrong?
    > >
    > >
    > > "Ken Cox [Microsoft MVP]" wrote:
    > >
    > >> Hi Mori,
    > >>
    > >> When you use master pages, the controls take on different IDs. If you
    > >> execute the page and then do View > Source in the browser, can you see
    > >> the
    > >> "real" name of txtEndDate?
    > >>
    > >> Depending on where it is, it could be something like:
    > >>
    > >> ctl00_TextBox1
    > >>
    > >> or
    > >>
    > >> ctl00_ContentPlaceHolder1_TextBox1
    > >>
    > >> Ken
    > >> Microsoft MVP [ASP.NET]
    > >>
    > >> "Mori" <> wrote in message
    > >> news:...
    > >> >I am using masterPage and I need to populate a textbox that is in a
    > >> >content
    > >> > control with data from popup page that is not part of the master page.
    > >> > This
    > >> > code works if no masterpage is involved. here is the javascript
    > >> > produced:
    > >> >
    > >> > <script>window.opener.document.forms[0].txtEndDate.value =
    > >> > '7/15/2006';self.close()</script>
    > >> >
    > >> > I basically need to populate txtEndDate on the content page.
    > >> >
    > >> > if
    > >> > (!this.Page.ClientScript.IsClientScriptIncludeRegistered("anything"))
    > >> > {
    > >> > string strDate = calDate.SelectedDate.ToShortDateString();
    > >> > StringBuilder sb = new StringBuilder();
    > >> > sb.Append("<script>window.opener.document.forms[0]." +
    > >> > control.Value + ".value = '");
    > >> > sb.Append(strDate);
    > >> > sb.Append("';self.close()");
    > >> > sb.Append("</script>");
    > >> > string strCalScript = sb.ToString();
    > >> >
    > >> > this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
    > >> > "anything", sb.ToString());
    > >> > }
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?TW9yaQ==?=, Jul 18, 2006
    #5
  6. =?Utf-8?B?TW9yaQ==?=

    dip_makwana

    Joined:
    Aug 11, 2010
    Messages:
    1
    i am also facing the same problem the window is not getting closed and values are not posted back to the parent page tect box..this is happenig with master page only
     
    dip_makwana, Aug 11, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.Edwards
    Replies:
    0
    Views:
    1,700
    J.Edwards
    Jan 20, 2006
  2. hazz
    Replies:
    6
    Views:
    50,040
    SkyUCHC
    Jun 9, 2010
  3. Managed Code
    Replies:
    3
    Views:
    7,868
    Eliyahu Goldin
    Jul 31, 2006
  4. Dave L
    Replies:
    3
    Views:
    3,368
    Göran Andersson
    Mar 4, 2010
  5. Enjoy Life
    Replies:
    2
    Views:
    319
    Roland Hall
    Feb 23, 2005
Loading...

Share This Page