猿问

如何将值从 .aspx 传递到 .aspx.cs?

我正在使用 C# 和 .NET ASP Web 应用程序开发我的第一个项目。我已成功连接到 SQL 数据库,但如何将输入从 .aspx(html 中)传递到 .aspx.cs(C# 中)?即(.aspx)

名字:

id=firstName, name=fname

(.aspx.cs) 我的 SQL 连接位于 protected void Page_Load(object sender, EventArgs e) 中。我如何检索firstName,以便将其插入到SQL 表中?

希望我说得有道理,如果您需要任何进一步的说明,请随时询问。


炎炎设计
浏览 91回答 1
1回答

慕的地6264312

已经有一段时间了,但是在您的 ASPX 页面中选项 1:Eval就地简单并bind采用代码隐藏方法GetName<%# Eval GetName(("FirstName").ToString()) %>然后在你的代码后面protected string GetName(object name){&nbsp; return "From codebehind";}选项 2:// 2 A-- Client Side, this can be a more complex collection of grid items.&nbsp;// Alternatively, you can also use a simple text box.&nbsp;&nbsp;<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"&nbsp; &nbsp; onrowcommand="GridView1_RowCommand">&nbsp; &nbsp; <Columns>&nbsp; &nbsp; &nbsp; &nbsp; <asp:BoundField DataField="Name" HeaderText="Name" />&nbsp; &nbsp; &nbsp; &nbsp; <asp:BoundField DataField="Add1" HeaderText="Add1" />&nbsp; &nbsp; &nbsp; &nbsp; <asp:BoundField DataField="Add2" HeaderText="Add2" />&nbsp; &nbsp; &nbsp; &nbsp; <asp:TemplateField>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:Panel ID="pnlCustomer" runat="server">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:TextBox runat="server" ID="txtCustName"></asp:TextBox>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </asp:Panel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; </asp:TemplateField>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:TemplateField>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:Button text="click" runat="server" ID="b1" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; </asp:TemplateField>&nbsp; &nbsp; </Columns>&nbsp; &nbsp;</asp:GridView>//2-- B Code behind Server ASPX.csprotected void Page_Load(object sender, EventArgs e) {&nbsp; &nbsp; if (!IsPostBack) {&nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable("tblTest");&nbsp; &nbsp; &nbsp; &nbsp; DataRow dr;&nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("CompanyName", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Add1", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Add2", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; dr = dt.NewRow();&nbsp; &nbsp; &nbsp; &nbsp; dr["CompanyName"] = "Tykt.work";&nbsp; &nbsp; &nbsp; &nbsp; dr["Add1"] = "Address1";&nbsp; &nbsp; &nbsp; &nbsp; dr["Add2"] = "Add 2";&nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(dr);&nbsp; &nbsp; &nbsp; &nbsp; GridView1.DataSource = dt.DefaultView;&nbsp; &nbsp; &nbsp; &nbsp; GridView1.DataBind();&nbsp; &nbsp; }}protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {&nbsp; &nbsp; GridViewRow row = (GridViewRow)(((Button) e.CommandSource).NamingContainer);&nbsp; &nbsp; int index = row.RowIndex;&nbsp; &nbsp; //((TextBox)GridView1.Rows[index].Cells[3].Controls[1])&nbsp; &nbsp; string strName = ((TextBox)((Panel) GridView1.Rows[index].Cells[3].Controls[1]).Controls[1]).Text.ToString();&nbsp; &nbsp; Response.Write(strName);}
随时随地看视频慕课网APP

相关分类

Html5
我要回答