简体   繁体   中英

Regarding an input variable on .aspx page which is not running on server and code behind

I have an input variable which is manipulated using JavaScript on the client side:

<input type="text" id="field1"  value="Sunday, July 30th in the Year 1967 CE" />

How can I use the input value to write to the database on click event in my code-behind?

Instead of using

<input type="text" id="field1"  value="Sunday, July 30th in the Year 1967 CE" />

Change it to

<asp:TextBox runat="server" ID="txtDate"></asp:TextBox>

Then set value in JS using something like:

document.getElementById('<%=txtDate.ClientID%>').value

The in code behind get the value

txtDate.Text

That's a very broad question, but in short you need to add runat="server" to the input so you can get the value in code-behind:

<input type="text" id="field1" runat="server" value="Sunday, July 30th in the Year 1967 CE" />

Code-behind:

protected void Button1_Click(object sender, EventArgs e)
{
    //get the value and perform save logic here
    string val = field1.Value;
} 

It's important to note that after adding runat="server" to the input, in JavaScript you'll need to access the control by it's ClientID:

var el = document.getElementById("<%= field1.ClientID %>");
if (el){
    el.value = "foo";
}

The simplest way to receive data from Client is to Use above <input> in a <form> tag with method attribute set to POST . Then you can receive the data from <input> tag in form collection in postback.

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