开发者

How can I reach form values from Content Page in ASP .NET

开发者 https://www.devze.com 2022-12-19 03:14 出处:网络
I have a master page which has a <form runat=\"server\"> line and a ContentPlaceHolder in it. I am trying create a content page using this master page whitch has text boxes in it. But I cannot

I have a master page which has a <form runat="server"> line and a ContentPlaceHolder in it.

I am trying create a content page using this master page whitch has text boxes in it. But I cannot reach values of theese text boxes using Request.Form["textbox"] from that conent page. I need to use Request.Form["ctl00$ContentPlaceHolder1$textbox"].

Is this the nor开发者_Go百科mal way to do it? If not what might be I am doing wrong?

By the way I am using same content page to process form values.

So I guess my question is actually: How can I access form values of a Content Page within the same content page?


With ASP.NET, you are not really supposed to know or care about HTTP requests and posted form values. The framework encapsulates these things, allowing you to deal with your TextBox as a component in a Windows GUI environment.

In your code, you are able to get and set its value using its Text property:

string whatsInThatBox = myTextBox.Text;
myTextBoxText = "Now, let's write something else here...";

Usually, you should only care about the cryptic names and ids of the rendered <input> elements if you need to add client side code referencing the elements.


Assuming you have standard ASP.Net controls, you can either access the control's value in the code behind by using

Dim x as string = Me.txtMyTextBox.Text

If you want to use it in script it's very similar

<%

Dim x as string = Me.txtMyTextBox.Text

%>

You shouldn't need to use Request.Form because the values of these controls are maintained in the ViewState of the page.


You could make a usercontrol (containing the text box) then add a public property to this control which returns the string value of the text box. Replace the text box on the master page with this user control and then you can get the property from any content pages.


You can make the controls accessible, take them out of the designer and make them public in your master page's code-behind file:

public MyMasterPageClass 
{
  public TextBox textbox;
}

To access it in the content page:

var text = ((MyMasterPageClass )Master).textbox.Text;

Or alternatively, in your contentpage's markup use the @MasterType directive:

<%@ MasterType VirtualPath="~/masters/MyMasterPage.master”" %>

And in the page, you don't need the cast:

var text = Master.textbox.Text;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号