如何使用 Javascript 参数触发回发?
这就是我正在做的事情:使用 jquery,我在页面上弹出一个“表单”,让用户搜索公司的分支机构并选择他们想要的分支机构。当表单弹出时,他们可以在文本框中输入内容,然后它会向服务器发出 AJAX 请求,返回他们输入内容的前 n 个结果,这些结果将被放入一个列表中。我希望用户能够通过单击显示“选择”或其他内容的链接来选择一个,此时我希望它执行回发,使分支选择器控件将其 SelectedBranch 属性更改为新选择的分支。我现在已经通过硬编码的 LinkButton 列表完成了这一切,但是如何使用 jquery 插入的动态链接列表做同样的事情呢?
This is what I'm doing: Using jquery, I'm popping up a "form" over the page that lets the user search for branches of the company and select the one they want. When the form pops up, they can type in a textbox, and it will do AJAX requests back to the server to return the top n results for what they've entered, and those results will be put into a list for them. I want the user to be able to select one by clicking a link that says "select" or something, and at that point I want it to do a PostBack have the Branch Selector control that this is in change it's SelectedBranch property to the newly selected branch. I've got this all working right now with a hard coded list of LinkButtons, but how do I do the same thing with a dynamic list of links inserted with jquery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看为硬编码 LinkButton 发出的 HTML。您将看到单击时每个函数都会调用 JavaScript __doPostBack 函数。我相信这个函数有两个参数:一个控件 ID 和一个额外的命令参数,您可以将其用于您自己的目的。
我建议向页面添加一个控件,其唯一的工作是处理动态链接的事件。然后,当您使用 jquery 创建链接时,让每个链接都调用 __doPostBack,为第一个参数传递事件处理控件的 ID,为第二个参数传递一些其他字符串,用于标识单击了哪个链接。在处理控件的 Click 事件中,查看第二个参数值并执行您需要执行的操作。
Look at the HTML that gets emitted for your hard coded LinkButtons. You'll see that each one calls the JavaScript __doPostBack function when clicked. I believe this function takes two arguments: a control ID and an extra command argument you can use for your own purposes.
I would suggest adding a single control to the page whose only job is handling events for the dynamic links. Then, when you are creating the links with jquery, make each one call __doPostBack, passing the event handling control's ID for the first argument and some other string for the second argument that identifies which link was clicked. In the Click event for the handling control, look at the second argument value and do what you need to do.
简短的回答是……你不知道。
ASP.NET 依赖 Viewstate 来获取控件的当前状态,包括 DropDownList 或类似控件中的项目。动态更新客户端上的列表不会修改视图状态,因此在后端不可用。
一般的解决方法是添加一个隐藏字段,该字段通过客户端的 js 更新/存储当前选择。然后从后端的这个字段读取它,而不是List.SelectedValue。
The short answer is... you don't.
ASP.NET relies on the Viewstate for the current state of the controls, including items in a DropDownList or similar control. Dynamically updating a list on the client will not modify the viewstate, so will not be available on the back end.
The general workaround for this is to just add a hidden field which updates/stores the current selection via js on the client side. Then read it from this field on the backend rather than List.SelectedValue.