I have been looking at the numerous posts regarding the use of the <form> tag in Master Pages as well as their associated content pages. Nothing really fits what I have been able
to find. Any assistance would be greatly appreciated. Here is my situation.
I am using VS 2008 3.5 and coding in C#.
I have a web site that uses master pages. On one of my pages there are two text boxes. Next to each text box is a button that when clicked opens a smaller pop up window that allows
the user to look up information stored in a database. The smaller form uses a gridview listing of the information returned from the database query generated by the value the user enters into a text box on the popup form. The user clicks on the selected row
to populate text box on the opening page.
This process works just dandy when Master pages are not in the equation. However, because I am using a JavaScript code snippet in the RowCommand of the gridview to populate the
textbox, the JavaScript is unable to access the control. Here is the JavaScript code:
"window.opener.form1.txtOrigin.value = txtValue;\n"
+ //<--- this is where the script cannot see form1.txtOrigin
"window.close();" +
"</script>";
int index =
Int32.Parse((string)e.CommandArgument);
string tVal = Lst.Rows[index].Cells[0].Text;
txtZip.Text = tVal;
ClientScript.RegisterStartupScript(cstype,
"CloseThisUp", javaScript);
}
The content page and master page are not special, nothing out of the ordinary in how they were constructed. What is not happening is the JavaScript is telling me the "window.opener.form1.txtOrigin.value"
is null or not an object. I will assume that is because the <form id="form1" runat="server"> exists on the master page and not the content page.
The main form is quite busy now and there really is not any clean or neat way to add a gridview to it and skip the popup window. As a matter of fact, this process is used in a
couple of places in the web site.
If someone would be kind enough to either point out how I could effectively reference the opener form's control (txtOrigin) here or provide another method to get the selected
gridview value on the popup window into the main form's textbox control, I would be very grateful.
The popup form does not use the master page.
Thanks in Advance for any assistance or direction.
When you are using master pages your controls get renamed in html, so although your server-side control is called
txtOrigin, the resulting control is called something else on the client. If you view->source you'll see this.
What you have to do is have your js use the control's ClientID property. Your master page can write out javascript that creates a variable from the clientid;
<script type="text/javascript">
var txtOriginID = '<%=txtOrigin.ClientID %>';
</script>
And the js your child page creates will use that var
AidyF, Thank you. I actually just managed to gut through this. I ran across an article. My problem was in the client's idea of what the control was called, just as you explained it here. Finally got it to work. Many thanks.
nmzike
Member
3 Points
5 Posts
MasterPage content pages' <form> elements
May 01, 2012 02:08 PM|LINK
Hi,
I have been looking at the numerous posts regarding the use of the <form> tag in Master Pages as well as their associated content pages. Nothing really fits what I have been able to find. Any assistance would be greatly appreciated. Here is my situation.
I am using VS 2008 3.5 and coding in C#.
I have a web site that uses master pages. On one of my pages there are two text boxes. Next to each text box is a button that when clicked opens a smaller pop up window that allows the user to look up information stored in a database. The smaller form uses a gridview listing of the information returned from the database query generated by the value the user enters into a text box on the popup form. The user clicks on the selected row to populate text box on the opening page.
This process works just dandy when Master pages are not in the equation. However, because I am using a JavaScript code snippet in the RowCommand of the gridview to populate the textbox, the JavaScript is unable to access the control. Here is the JavaScript code:
protected void Lst_RowCommand(object sender, GridViewCommandEventArgs e)
{
Type cstype = this.GetType();
string javaScript ="<script language=JavaScript>\n" +
"var txtValue = document.getElementById('txtZip').value;\n" +
"window.opener.form1.txtOrigin.value = txtValue;\n" + //<--- this is where the script cannot see form1.txtOrigin
"window.close();" +
"</script>";
int index = Int32.Parse((string)e.CommandArgument);
string tVal = Lst.Rows[index].Cells[0].Text;
txtZip.Text = tVal;
ClientScript.RegisterStartupScript(cstype,
"CloseThisUp", javaScript);
}
The content page and master page are not special, nothing out of the ordinary in how they were constructed. What is not happening is the JavaScript is telling me the "window.opener.form1.txtOrigin.value" is null or not an object. I will assume that is because the <form id="form1" runat="server"> exists on the master page and not the content page.
The main form is quite busy now and there really is not any clean or neat way to add a gridview to it and skip the popup window. As a matter of fact, this process is used in a couple of places in the web site.
If someone would be kind enough to either point out how I could effectively reference the opener form's control (txtOrigin) here or provide another method to get the selected gridview value on the popup window into the main form's textbox control, I would be very grateful.
The popup form does not use the master page.
Thanks in Advance for any assistance or direction.
Noel
AidyF
Star
9250 Points
1578 Posts
Re: MasterPage content pages' <form> elements
May 01, 2012 03:32 PM|LINK
When you are using master pages your controls get renamed in html, so although your server-side control is called txtOrigin, the resulting control is called something else on the client. If you view->source you'll see this. What you have to do is have your js use the control's ClientID property. Your master page can write out javascript that creates a variable from the clientid;
<script type="text/javascript"> var txtOriginID = '<%=txtOrigin.ClientID %>'; </script>And the js your child page creates will use that var
The alternative is for your Master page to expose the ClientID property and your child page to use it;
"var txtValue = document.getElementById('" + ((MasterType)Master).OriginClientID + "').value;\n" +nmzike
Member
3 Points
5 Posts
Re: MasterPage content pages' <form> elements
May 01, 2012 04:42 PM|LINK
AidyF, Thank you. I actually just managed to gut through this. I ran across an article. My problem was in the client's idea of what the control was called, just as you explained it here. Finally got it to work. Many thanks.
Noel