打開Visual Studio 2015
首先,我們需要打開Visual Studio 2015。在開始菜單中找到并點(diǎn)擊打開該程序。創(chuàng)建新項(xiàng)目在Visual Studio的界面中,選擇“文件”-gt;“新建”-gt;“項(xiàng)目”,彈出一個(gè)新建項(xiàng)目
首先,我們需要打開Visual Studio 2015。在開始菜單中找到并點(diǎn)擊打開該程序。
創(chuàng)建新項(xiàng)目
在Visual Studio的界面中,選擇“文件”-gt;“新建”-gt;“項(xiàng)目”,彈出一個(gè)新建項(xiàng)目的窗口。
選擇Web應(yīng)用程序模板
在新建項(xiàng)目窗口中,選擇“ Web應(yīng)用程序”,然后點(diǎn)擊確定按鈕。
選擇模板
在模板選擇界面中,選擇“WebForm模板”,點(diǎn)擊確定按鈕。
添加新建項(xiàng)
在解決方案資源管理器中,右鍵點(diǎn)擊解決方案,選擇“添加”-gt;“新建項(xiàng)...”來添加一個(gè)新的Web窗體。
添加Web窗體
在新建項(xiàng)窗口中,選擇Web窗體,并設(shè)置名稱,然后點(diǎn)擊添加按鈕。
在文件中添加代碼
在打開的文件中,添加以下代碼:
```
lt;%@ Page Language"C" AutoEventWireup"true" CodeBehind"" Inherits"WebApplication11.WebForm1" %gt;
lt;!DOCTYPE htmlgt;
lt;html xmlns""gt;
lt;head runat"server"gt;
lt;meta http-equiv"Content-Type" content"text/html; charsetutf-8" /gt;
lt;titlegt;獲取自動(dòng)生成的編號lt;/titlegt;
lt;script src"Scripts/jquery-1.10.2.js"gt;lt;/scriptgt;
lt;scriptgt;
function fRandomBy(under, over) {
switch (arguments.length) {
case 1: return parseInt(Math.random() * under 1);
case 2: return parseInt(Math.random() * (over - under 1) under);
default: return 0;
}
}
$(function () {
var random fRandomBy(1, 100);
alert(random);
$("lt;% %gt;").val(random);
});
lt;/scriptgt;
lt;/headgt;
lt;bodygt;
lt;form id"form1" runat"server"gt;
lt;divgt;
lt;asp:Button ID"btnRandom" runat"server" Text"獲取自動(dòng)生成的編號" OnClick"btnRandom_Click" /gt;
lt;/divgt;
lt;asp:HiddenField ID"hd" runat"server" /gt;
lt;/formgt;
lt;/bodygt;
lt;/htmlgt;
```
在后臺文件中添加代碼
在后臺文件中,添加以下代碼:
```c
using System;
using ;
using ;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication11
{
public partial class WebForm1 :
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRandom_Click(object sender, EventArgs e)
{
string random ; // 獲取前臺js產(chǎn)生的自動(dòng)編號
Response.Write(random);
}
}
}
```
在瀏覽器中查看效果
右鍵點(diǎn)擊,選擇“在瀏覽器中打開”。點(diǎn)擊按鈕后,你就會(huì)看到生成的值在頁面上顯示了出來。
通過以上步驟,你就可以在C后臺代碼中獲取由前端自動(dòng)生成的編號了。希望這篇經(jīng)驗(yàn)對你的工作和學(xué)習(xí)有所幫助!