简体   繁体   English

从JavaScript调用C#函数背后的代码

[英]Call code behind c# function from javascript

I know this has been asked, but none of the numerous answers fit with my situation. 我知道有人问过这个问题,但是众多答案都不符合我的情况。 So I will humbly ask if someone can walk me through this again gently. 因此,我会谦虚地问,是否有人可以再次温柔地引导我进行此操作。

Environment: .Net 4 web app using c#, javascript, jquery, sql server, and entity framework. 环境:.net 4 Web应用程序,使用c#,javascript,jquery,sql服务器和实体框架。

Question: I need to be able to allow a user to "quick add" a value into a table that is bound to a drop down box without losing values a user has already entered onto current page. 问题:我需要允许用户将值“快速添加”到绑定到下拉框的表中,而不会丢失用户已经输入到当前页面的值。 I don't care if I have to do a postback or use jquery or use ajax, etc. I need the functionality first - prettiness later ... the scenario is described below if you care to keep reading. 我不在乎是否必须执行回发或使用jquery或使用ajax等。我首先需要功能-稍后需要美化...如果您希望继续阅读,请在下面描述该方案。 With all that being said, I would love for this to be smooth and not "clunky" for the user. 话虽这么说,我希望它对用户来说是平滑的而不是“笨拙的”。

Scenario: I have a page where the user is able to enter many pieces of information into textboxes, static drop down boxes, and drop down boxes that are bound to other tables that will all get combined and inserted into one table. 场景:我有一个页面,用户可以在其中将很多信息输入到文本框,静态下拉框和绑定到其他表的下拉框中,这些表将全部合并并插入到一个表中。 Well, it never fails, a user could be filling in the page and when they get to one of the table bound drop down boxes - a value is missing or they haven't added something item yet. 好吧,它永远不会失败,用户可以填写页面,并且当他们到达一个表绑定下拉框时-缺少值或尚未添加任何项目。 I would like to add a button or link or something next to that drop down box that would allow the user to "quick add" an item to the table that fills that drop down box and then refresh the drop down box so they can choose that new value from the list ... all while not losing the other data on the screen they already entered. 我想在该下拉框旁边添加一个按钮或链接或其他内容,以使用户可以“快速添加”一个项目到填充该下拉框的表中,然后刷新该下拉框,以便他们可以选择列表中的新值...所有这些都不会丢失他们已经输入的屏幕上的其他数据。

What I've tried: -Tried WebMethod option. 我试过的:-尝试过WebMethod选项。 Currently, this web page calls some custom "bind" methods in the code behind to fill drop down boxes so they are not filled directly from objects from entity framework. 当前,该网页在后面的代码中调用一些自定义的“绑定”方法来填充下拉框,因此不会直接从实体框架的对象中填充它们。 So that means using a static method will not work for a solution because part of the static function would need to call the bind method to refresh the drop down box with the newly inserted values. 因此这意味着使用静态方法将不适用于解决方案,因为静态函数的一部分将需要调用bind方法以使用新插入的值刷新下拉框。

-Tried PageMethod option. -尝试使用PageMethod选项。 Some of the other samples used Page Method settings, but my site uses master and content pages and the posts seem to state that the page method route was not going to work. 其他一些示例使用了“页面方法”设置,但是我的网站使用的是母版页和内容页面,这些帖子似乎表明该页面方法路由无法正常工作。

Make sense? 说得通?

Add an UpdatePanel to the page. UpdatePanel添加到页面。 The update panel should wrap the drop down to be added, as well as the textbox/button for adding a new entry to that drop down. 更新面板应包装要添加的下拉菜单,以及用于在该下拉菜单中添加新条目的文本框/按钮。 You would want to have a different UpdatePanel for each dropdown/button pair, if applicable. 您可能希望每个下拉/按钮对都有一个不同的UpdatePanel(如果适用)。

When they click the button inside of the update panel you can read the textbox, add an extra item to the drop down, and send an update to the database. 当他们单击更新面板内部的按钮时,您可以阅读文本框,在下拉列表中添加一个额外的项目,然后将更新发送到数据库。 (I'd manually add the new value to the drop down rather than updating the database and re-binding, if possible, but it may not be.) (如果可能的话,我会手动将新值添加到下拉列表中,而不是更新数据库并重新绑定,但这可能不是。)

The magic of UpdatePanels will make sure that this is all asynchronous and so doesn't disturb the user working on the page. UpdatePanels的魔力在于确保所有这些都是异步的,因此不会打扰用户在页面上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM