【问题标题】:How would I pass a value from .aspx to .aspx.cs?如何将值从 .aspx 传递到 .aspx.cs?
【发布时间】:2020-07-15 01:51:27
【问题描述】:

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

名字:

id=firstName,name=fname

(.aspx.cs) 我在受保护的 void Page_Load(object sender, EventArgs e) 中有我的 SQL 连接。我将如何检索 firstName 以便将其插入 SQL 表中?

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

提前谢谢:)

【问题讨论】:

    标签: c# html asp.net


    【解决方案1】:

    已经有一段时间了,但在您的 ASPX 页面中

    选项 1:简单的 Evalbind 到方法后面的代码 GetName

    <%# Eval GetName(("FirstName").ToString()) %>
    

    然后在你的代码后面

    protected string GetName(object name)
    {
      return "From codebehind";
    }
    

    选项 2

    // 2 A-- Client Side, this can be a more complex collection of grid items. 
    // Alternatively, you can also use a simple text box.  
    
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Add1" HeaderText="Add1" />
            <asp:BoundField DataField="Add2" HeaderText="Add2" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Panel ID="pnlCustomer" runat="server">
                        <asp:TextBox runat="server" ID="txtCustName"></asp:TextBox>
                    </asp:Panel>
                </ItemTemplate>
            </asp:TemplateField>
    
             <asp:TemplateField>
                <ItemTemplate>
                  <asp:Button text="click" runat="server" ID="b1" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
       </asp:GridView>
    
    //2-- B Code behind Server ASPX.cs
    
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            DataTable dt = new DataTable("tblTest");
            DataRow dr;
    
            dt.Columns.Add("CompanyName", typeof(string));
            dt.Columns.Add("Add1", typeof(string));
    
            dt.Columns.Add("Add2", typeof(string));
            dr = dt.NewRow();
    
            dr["CompanyName"] = "Tykt.work";
            dr["Add1"] = "Address1";
    
            dr["Add2"] = "Add 2";
            dt.Rows.Add(dr);
    
            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
        GridViewRow row = (GridViewRow)(((Button) e.CommandSource).NamingContainer);
        int index = row.RowIndex;
        //((TextBox)GridView1.Rows[index].Cells[3].Controls[1])
        string strName = ((TextBox)((Panel) GridView1.Rows[index].Cells[3].Controls[1]).Controls[1]).Text.ToString();
        Response.Write(strName);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多