ASP.Net 根据其他字段自动填充字段

发布于 2024-10-02 22:25:07 字数 389 浏览 0 评论 0原文

我刚刚转向 Web 开发,需要知道如何使用 asp.net 和 vb.net 实现以下要求。

我的表单中有三个字段,由用户填写。根据这三个值,我需要自动填充第四个字段。我计划通过以下方式实现此目的

  1. 编写一个单独的类文件,其中包含一个函数,用于根据第 1 个 3 个输入计算第 4 个字段的可能值。此函数可以返回 1-10 之间的某个值。因此,我决定对第 4 个字段使用下拉菜单,并允许用户选择适当的值。

  2. 在第三个字段的 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

  1. 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.

  2. 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 技术交流群。

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

发布评论

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

评论(3

始终不够爱げ你 2024-10-09 22:25:07

您可能需要考虑使用 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.

吻安 2024-10-09 22:25:07

您需要 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.

∝单色的世界 2024-10-09 22:25:07

一个简单的方法是使用 jQuery 作为 UI(这样您就不必担心冗长的 javascript 并处理浏览器兼容性,因为它已经为您处理好了)并让它调用服务器来获取数据。对于服务器来说,最简单的方法是返回 JSON 来循环值。

包含您的 jQuery:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

然后添加 JavaScript 的句柄:(

<script type="text/javascript">
function autoPopulate() {
   var value1 = $('#ddl1').val();
   var value2 = $('#ddl2').val();
   var value3 = $('#ddl3').val();
   var url = 'path/to/your/file.aspx?value1=' + value1 + '&value2=' + value2 + '&value3=' + value3;
   $.getJSON(url, function(data) {
      data == null ? return false : data = eval(data);
      var ddl = $('#ddl4')[0];
      for (i = 0; i < data.length; i++) {
         var option = new Option(data[i][0], data[i][1]);
         if ($.browser.msie) {
            ddl.add(option);
         } else {
            ddl.add(option, null);
         }
      }
   }
}
</script>

是的,我知道我使用了本机循环,但今天我有点懒:))

现在,对于您的服务器端代码,您将希望您的页面代码返回数据格式为:

[['value1','text1'],['value2','text2'],['value3','value3']]

所以类似:

<script type="vb" runat="server">
Private Sub Page_Init()
   // get your data
   // loop through it and add in values
   // ex.
   Dim result As String = "[" //start multi-dimensional array
   For Each Item As String In data
      result += String.Format("['{0}','{1}'],", _value, _text)
   Next
   result = result.SubString(0, result.Length - 1) // removes trailing comma
   result += "]" // closes off m-array
   Response.Write(result)
   Response.Flush()
End Sub
</script>

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:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

Then add in a handle for the JavaScript:

<script type="text/javascript">
function autoPopulate() {
   var value1 = $('#ddl1').val();
   var value2 = $('#ddl2').val();
   var value3 = $('#ddl3').val();
   var url = 'path/to/your/file.aspx?value1=' + value1 + '&value2=' + value2 + '&value3=' + value3;
   $.getJSON(url, function(data) {
      data == null ? return false : data = eval(data);
      var ddl = $('#ddl4')[0];
      for (i = 0; i < data.length; i++) {
         var option = new Option(data[i][0], data[i][1]);
         if ($.browser.msie) {
            ddl.add(option);
         } else {
            ddl.add(option, null);
         }
      }
   }
}
</script>

(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:

[['value1','text1'],['value2','text2'],['value3','value3']]

so something like:

<script type="vb" runat="server">
Private Sub Page_Init()
   // get your data
   // loop through it and add in values
   // ex.
   Dim result As String = "[" //start multi-dimensional array
   For Each Item As String In data
      result += String.Format("['{0}','{1}'],", _value, _text)
   Next
   result = result.SubString(0, result.Length - 1) // removes trailing comma
   result += "]" // closes off m-array
   Response.Write(result)
   Response.Flush()
End Sub
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文