asp.net Dropdownlist条件回发

发布于 2024-12-24 00:59:05 字数 236 浏览 6 评论 0原文

我有一个下拉列表,并有一个回发的事件 selectedIndexChanged,我希望能够在用户更改下拉列表中的值时向用户显示一条消息,根据消息的输入,我将决定是否必须回发。

显示的消息将是您确定吗?如果他选择“是”,我将继续回发,如果他说“否”,我将取消回发并分配先前选择的值。

我已经搜索了很多,但无法找到解决方案,我想是否有一个 javascipt 函数可以确定是否需要回发,这可能会有所帮助,我想

谢谢

I have a dropdownlist and have an event selectedIndexChanged which postsback, i want to be able to show a message to the user whenever he changes the value in dropdownlist, based on the input from the message i will decide if i have to postback or not.

The message shown would be are you sure? if he selects yes i would continue with postback, if he says no, i would cancel the postback and assign the previous value as selected.

I have searched alot but cant figure out a solution to this, i think if there is a javascipt function which determines if a postback is required or not that could help i guess

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落叶缤纷 2024-12-31 00:59:05
// get a reference to the DropDownList
var selectlistId = '<%= ddlYourList.ClientID %>',
    selectlist = document.getElementById(selectlistId);

// attach to the onchange event
selectlist.onchange = function() {

  // decide whether to execute the __doPostBack event, which submits the
  // form back to the server
  if(confirm("Are you sure you want to do this?")){
     __doPostBack(selectlistId, '');
  }
};
// get a reference to the DropDownList
var selectlistId = '<%= ddlYourList.ClientID %>',
    selectlist = document.getElementById(selectlistId);

// attach to the onchange event
selectlist.onchange = function() {

  // decide whether to execute the __doPostBack event, which submits the
  // form back to the server
  if(confirm("Are you sure you want to do this?")){
     __doPostBack(selectlistId, '');
  }
};
一口甜 2024-12-31 00:59:05

您可以非常简单地停止或取消下拉列表的回发。只需在页面加载事件上添加此 JavaScript 即可。

protected void Page_Load(object sender, EventArgs e)
{
    DropDownList1.Attributes.Add("OnChange", "if (!confirm('Change this?')){return};");
}

You can stop can cancel postback of dropdownlist very simply.Just add this javascript on page load event.

protected void Page_Load(object sender, EventArgs e)
{
    DropDownList1.Attributes.Add("OnChange", "if (!confirm('Change this?')){return};");
}
知你几分 2024-12-31 00:59:05

为了完成此任务,您需要使用带有自定义客户端 Javascript 的 CustomValidator 来控制回发。

您可以阅读 4Guys 上的这篇文章 通过客户端验证器 JavaScript 示例讨论不同的验证器以获得一个想法。

但核心解决方案是使用自定义验证器仅在表单有效时控制回发。

In order to accomplish this task you will need to use a CustomValidator with custom client side Javascript to control the post back.

You can read this article on 4Guys discussing the different validators with a client side validator JavaScript sample to get an idea.

But the core solution would be using a custom validator to control the post back only when the form is valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文