ASP.Net 根据其他字段自动填充字段
我刚刚转向 Web 开发,需要知道如何使用 asp.net 和 vb.net 实现以下要求。
我的表单中有三个字段,由用户填写。根据这三个值,我需要自动填充第四个字段。我计划通过以下方式实现此目的
编写一个单独的类文件,其中包含一个函数,用于根据第 1 个 3 个输入计算第 4 个字段的可能值。此函数可以返回 1-10 之间的某个值。因此,我决定对第 4 个字段使用下拉菜单,并允许用户选择适当的值。
在第三个字段的 onchange 函数中调用上述函数,并使用返回值填充第四个字段。我计划在数组字段中获取返回值。(这需要回发吗?)
如果有更好的方法来实现这一点,请告诉我如何实现。
谢谢。
I've just moved to web development and need to know how i can implement below requirement using asp.net and vb.net.
I have three fields in a form which are filled by users. Based on these three values, i need to auto-populate the 4th field. I have planned to implement this in the following way
Write a separate class file with a function to calculate the possible values for the 4th fields based on 1st 3 inputs. This function can return some where between 1-10 values. So I've decided to use drop-down for 4th field, and allow users to select the appropriate value.
Call the above function in onchange function of 3rd field and take and use the return values to populate the 4th field. I'm planning to get the return values in array field.(Does this need a post back?)
Please let me know how if there is better way to implement this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要考虑使用 Javascript 来完成此操作。您可以使用纯 Javascript 轻松读取和控制字段,或者使用像 jQuery (我最喜欢的)这样的漂亮库。如果您这样做,则不需要回发,并且第四个字段将立即更新。 (对您的用户来说很好)
您也可以在大多数情况下使用 ASP.NET 来完成此操作。据我所知,ASP.NET 中的“onchange”仍然需要 Javascript,它只是为您完成了其中的一些工作。当你改变某些东西时肯定会发生回发。
You may want to consider doing this with Javascript. You could read and control the fields pretty easily with pure Javascript, or using a nice library like jQuery (my favorite). If you did it this way, no post-back would be required and the 4th field would update immediately. (Nice for your users)
You can also do it with ASP.NET for the most part. "onchange" in ASP.NET still requires Javascript as far as I know, it just does some of it for you. A post-back will definitely happen when you change something.
您需要 javascript 或在表单元素上设置 autopostback=true 。
从用户的角度来看,最好的办法是使用 javascript 填充要显示的字段,但是在提交表单时使用后端函数来验证它。这将确保用户没有更改该值。
You need javascript or to set autopostback=true on your form elements.
From a user perspective the best thing is to use javascript to populate the field for display, BUT when the form is submitted use your backend function to validate it. This will make sure the user didn't change the value.
一个简单的方法是使用 jQuery 作为 UI(这样您就不必担心冗长的 javascript 并处理浏览器兼容性,因为它已经为您处理好了)并让它调用服务器来获取数据。对于服务器来说,最简单的方法是返回 JSON 来循环值。
包含您的 jQuery:
然后添加 JavaScript 的句柄:(
是的,我知道我使用了本机循环,但今天我有点懒:))
现在,对于您的服务器端代码,您将希望您的页面代码返回数据格式为:
所以类似:
An easy way is to use jQuery for the UI (that way you don't have to worry about long winded javascript and deal with browser compatibility as it's already taken care of for you) and have it call to the server for the data. For the server, your easiest route is to return JSON for looping values.
Include your jQuery:
Then add in a handle for the JavaScript:
(Yes, I know I used a native loop but I'm little lazy here today :) )
Now, for your server side code you'll want your code your page to return data in the format of:
so something like: