简体   繁体   中英

How to get the text value from textbox within a FormView from ascx file in the ascx.cs file

I have an asp: text box in an FormView in an ascx file like this>

<asp:FormView runat="server" ID="myFrmView">

    <asp:TextBox runat="server" ID="txtBox1" size="20" MaxLength="150" >

</FormView>

But in the code-behind ascx.cs file I can't retrieve the value from the textbox

string name2 = Request.Form["txtBox1"];

How to get the value?

您不需要使用Request ,您应该可以直接在后面的代码中访问控件,请尝试:

var value = txtBox1.Text;

Ok i found it.

string value = (myFrmView.FindControl("txtBox1") as TextBox).Text;

What was the thing was the inside a FormView control and that had no id. Id of myFrmView

直接访问控制:

string text = txtBox1.Text;

It's txtBox1.Text. ASP.NET doesn't generate HTML elements with the same names you gave them in your markup file IDs, and referencing Request.Form is a bypass of the robust object model that supports the server-side controls.

If you can't access the control directly with txtBox1.text, then I would check to make sure your ascx file is pointing to the correct code behind file in the <% @Page %> section at the top. There should be a CodeBehind attribute pointing to your [file].ascx.cs.

You may also want to check to make sure that the declared class in your ascx.cs matches the one in the designer.cs file. If they don't, the partial class declaration in the designer.cs won't match and you won't have the properties for your controls.

I'll get this error if I manually rename some files and forget to update the code behind attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM