【发布时间】:2011-12-22 18:35:37
【问题描述】:
我正在开发一个网页,供客户请求豁免某些要求。有 14 个复选框的列表,每个复选框都有两个后续复选框。检查其中哪些被选中的代码将放置在提交按钮中。这个方法在后面的代码中。这是(到目前为止)的代码
[WebMethod]
public void SendForm(string email)
{
if (string.IsNullOrEmpty(email))
{
throw new Exception("You must supply an email address.");
}
else
{
if (IsValidEmailAddress(email))
{
bool[] desc = new bool[14];
bool[] local = new bool[14];
bool[] other = new bool[14];
for (int i = 1; i <= 14; i++)
{
desc[i] = ((CheckBox)Page.FindControl("chkDesc" + i.ToString())).Checked;
local[i] = ((CheckBox)Page.FindControl("chkLocal" + i.ToString())).Checked;
other[i] = ((CheckBox)Page.FindControl("chkOther" + i.ToString())).Checked;
/* Do stuff here */
}
}
else
{
throw new Exception("You must supply a valid email address.");
}
}
}
我收到错误“非静态字段、方法或属性所需的对象引用...”
在 aspx 页面中,我有按钮和在代码隐藏中调用按钮的 javascript/ajax。此处显示:
<table width='750' align='center'>
<tr>
<td align='left'>
<label>Please Provide your email address:</label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align='left'>
</td>
</tr>
<tr>
<td align='left'>
<fieldset id="Fieldset">
<button onclick="SendForm();">
Send</button>
<button onclick="CancelForm();">
Cancel</button>
</fieldset>
</td>
</tr>
</table>
</form>
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" />
<script type="text/javascript">
function SendForm() {
var email = $get("txtEmail").value;
PageMethods.SendForm(email, OnSucceeded, OnFailed);
}
function OnSucceeded() {
$get("Fieldset").innerHTML = "<p>Thank you!</p>";
}
function OnFailed(error) {
alert(error.get_message());
}
</script>
【问题讨论】:
-
整个页面没有通过网络方式回传。
-
对于初学者来说,让你的 int 1 从 0 开始摆脱
-
确实,这会更好地放在后面的代码中并将数组作为参数传递
-
为什么提交按钮使用AJAX方式?除非你的意思不是“提交”?
-
编辑帖子以更准确地描述问题