我这是在客户端使用JavaScript操作的输入变量的.aspx页面上输入变量:对于未在服务器和代码运行背后
<input type="text" id="field1" value="Sunday, July 30th in the Year 1967 CE" />
我如何使用输入值在我的代码隐藏中在点击事件中写入数据库?
我这是在客户端使用JavaScript操作的输入变量的.aspx页面上输入变量:对于未在服务器和代码运行背后
<input type="text" id="field1" value="Sunday, July 30th in the Year 1967 CE" />
我如何使用输入值在我的代码隐藏中在点击事件中写入数据库?
这是一个很广泛的问题,但总之你需要添加runat="server"
的输入,让你可以在代码中隐藏的价值:代码隐藏
<input type="text" id="field1" runat="server" value="Sunday, July 30th in the Year 1967 CE" />
:
protected void Button1_Click(object sender, EventArgs e)
{
//get the value and perform save logic here
string val = field1.Value;
}
需要注意的是,在将输入runat="server"
添加到输入之后,在JavaScript中,您需要通过它的ClientID访问控件:
var el = document.getElementById("<%= field1.ClientID %>");
if (el){
el.value = "foo";
}
而不是使用像在JS使用
<input type="text" id="field1" value="Sunday, July 30th in the Year 1967 CE" />
将其更改为
<asp:TextBox runat="server" ID="txtDate"></asp:TextBox>
然后设置值:在代码
document.getElementById('<%=txtDate.ClientID%>').value
的背后获得的价值
txtDate.Text
从客户端接收数据的最简单方法是使用<input>
以上的<form>
标签,method
属性设置为POST
。然后您可以在回发中从表单集合中收到<input>
标记中的数据。
您是否在使用CGI或您的后端?关于这个问题,特别是“C”是什么? – RageD
“C”这个问题被错误地标记为特别容易:) –