在后面的代码中填充静态Web方法中的选择

时间:2015-07-07 09:49:28

标签: c# jquery asp.net

我必须动态填充文件后面的代码中的选择。我能够从Page_load执行此操作,但无法通过静态Web方法执行此操作。它说" 访问非静态字段需要一个对象引用..."

 <div style="margin-left: 24%; display: inline-block" runat="server">
  <label style="margin-top: 3%">Select Voice Product</label>
  <button type="button" style="margin-top: 3%;" class="qual toDisable" id="btnAddVoicetToSelected" onclick="btnAddVoice()">Add Voice</button>
 <select id="selectedVoiceProducts" runat="server" name="VoiceProducts" style="margin-top: 3%;">
<option label="Select Voice Product" value="-1">Select</option>
 </select>
      </div>

代码背后:

[WebMethod]

pulic static void Populate(){
    selectedVoiceProducts.Items.Add(new ListItem("SomeItem","SomeValue");//error here
}

1 个答案:

答案 0 :(得分:1)

错误消息说明了一切。 您正在从静态方法访问服务器控件(实例),这是不可能的。

如果要传递选项 - 返回一个ArrayList并让调用者使用此信息

查看详细解决方案here

相关问题