This is a collection written in Asp.net.Program. The problems we encounter are gradually accumulated here. When we encounter these problems in the future, we will have an impression that we can come here to find a solution.
Directory
A. Why can't I retrieve the value in the control using request. Params after refreshing the dashboard page?
1. Why can't I retrieve the value in the control using request. Params after refreshing the dashboard page?
Today, I encountered a very strange phenomenon in the following parent board page:
< ASP: Content ID = "Content2" Contentplaceholderid = "Contentplaceholder1" Runat = "Server" >
< ASP: button ID = "Button2" Runat = "Server" Text = "Button" />
< ASP: Label ID = "Label1" Runat = "Server" Forecolor = "# Ccffcc" Text = "BBB" > </ ASP: Label >
< ASP: textbox ID = "Textbox1" Runat = "Server" Text = "BBB" > </ ASP: textbox >
</ ASP: Content >
Edit the following in the page_load function:Code:
Protected Void Page_load ( Object Sender, eventargs E)
{
Label1.text = Request. Params [ " Textbox1 " ];
}
According to your understanding, after clicking the button, label1 will display the value in textbox1. But there is no result. The reason is:
Msdn explanation: Any control that implements this interface creates a new namespace. In this new namespace, the ID attribute of all child controls must be unique throughout the application. The tag provided by this interface allows a server control instance to be dynamically generated with a unique name in a web server control that supports data binding. These controls include the repeater, DataGrid, datalist, checkboxlist, changepassword, loginview, menu, sitemapnodeitem, and radiobuttonlist controls.
Other explanations:
. Request. form gets the value based on clientid instead of ID. Nested in controls such as repeater or formview, clientid is its own ID plus the ID of the container control as the prefix, so it is different from the ID itself.
Because the container control is nested, findcontrol at the page level cannot be found. You need to implement findcontrol on the Container Control.
The preceding example should be modified:
Protected Void Page_load ( Object Sender, eventargs E)
{
Textbox TB = (Textbox) page. master. findcontrol ( " Contentplaceholder1 " ). Findcontrol ( " Textbox1 " );
Label1.text = TB. text;
}
Of course, the above for the server control, it seems redundant, you can directly use label1.text = textbox1.text.
However, if it is an HTML control, it will be more effective. For example:
< ASP: Content ID = "Content2" Contentplaceholderid = "Contentplaceholder1" Runat = "Server" >
< ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" > </ ASP: scriptmanager >
< ASP: updatepanel ID = "Updatepanel1" Runat = "Server" Updatemode = "Conditional" >
< Contenttemplate >
< Input Type = Hidden ID = "Dynamic_usercontrol_hidden1" Runat = "Server" /> // Html Control
< ASP: button ID = "Button1" Runat = "Server" Text = "Button" Onclick = "Button#click" Onclientclick = "Showcontrol ()" />
</ Contenttemplate >
</ ASP: updatepanel >
</ ASP: Content >
Above, you can obtain the value in dynamic_usercontrol_hidden1 after refreshing the page, as shown in the following code:
Protected Void Page_load ( Object Sender, eventargs E)
{
// Nested container for findcontrol on the container
Htmlinputhidden hihidden = (Htmlinputhidden) updatepanel1.contenttemplatecontainer. findcontrol ( " Dynamic_usercontrol_hidden1 " );
Label1.text = Hihidden. value;
}
Then there is a javascript problem:
If you use the following statement in the script, the error "empty or not object" is returned.
Document. getelementbyid ("textbox1"). Value
The main reason is that the Control ID is changed after the page is generated. You can use the following statement
Document. getelementbyid ("<% = textbox1.clientid %>"). value;
Specifically, you can query the following urls:
Http://www.itstrike.cn/Home/Article/Asp.net-in-the-master-page-ID-of-the-control-treatment
Http://topic.csdn.net/u/20080503/18/205102e1-7f6c-4858-970e-f7b9ce390b0e.html
Http://msdn.microsoft.com/zh-cn/library/system.web.ui.inamingcontainer%28VS.85%29.aspx