asp.net 保存、修改没有 runat=server控件的控件值的
ASP.NET中保存和修改没有runat=server控件的值:一种解决方案
在Web开发中,有时我们需要处理一些不在服务端控件中的值,这些值可能存在于HTML元素中。以下是一个解决方案,通过JavaScript和C代码实现值的保存和修改。
JavaScript部分:
```javascript
function Save() {
var value1 = document.getElementById('1Box').value;
var value2 = document.getElementById('2Box').value;
var TxtValue = value1 + "■" + value2;
document.getElementById('hiddenInput').value = TxtValue;
return true;
}
window.onload = function show() {
var TxtValue = document.getElementById('hiddenInput').value;
if (TxtValue != "") {
var EachValue = TxtValue.split('■');
document.getElementById('1Box').value = EachValue[0];
document.getElementById('2Box').value = EachValue[1];
}
return true;
}
```
HTML部分:
```html
```
C部分:
在页面加载时调用Save函数保存值,并在服务器端进行处理:
```csharp
void LoadData() //页面加载
{
btnSave.Attributes.Add("onclick", "javascript:Save();");
string[] strTxtValues = new string[2];
strTxtValues[0] = dr["Column1"].ToString(); //假设你的数据行对象为dr,并且你想获取的列为Column1和Column2。根据实际情况修改。
strTxtValues[1]=dr["Column2"].ToString();
string strValues = strTxtValues[0];
for (int i = 1; i < strTxtValues.Length; i++)
{
strValues += "■" + strTxtValues[i];
}
this.hiddenInput.Value = strValues; //假设你的隐藏字段名为hiddenInput。根据实际情况修改。
}
private void SaveOrUpdate() //保存、修改
{
string[] strTxtValues = this.hiddenInput.Value.Split('■');
string value1 = strTxtValues[0];
string value2 = strTxtValues[1];
//接下来你可以使用这些值进行保存或更新操作。
}
```
上述解决方案利用JavaScript处理前端数据的保存,然后通过隐藏字段将数据传递到服务器端进行处理。这是一种常见的前后端数据交互方式,适用于处理不在服务端控件中的值。请注意根据实际的项目需求进行相应的调整和优化。