简体   繁体   中英

How would I pass a value from .aspx to .aspx.cs?

I am working on my first Project using C# and .NET ASP Web application. I have managed to connect to a SQL database, but how would I pass the input from.aspx (in html) to.aspx.cs (in C#)? ie (.aspx)

First Name:

The id=firstName, name=fname

(.aspx.cs) I have my SQL connection in the protected void Page_Load(object sender, EventArgs e). How would i retrieve the firstName so that I can insert it into the SQL table?

Hopefully I make sense, if you need any further clarification please don't hesitate to ask.

Thanks in advance:)

Its been some time, but in your ASPX page

Option 1 : Simple Eval in place and bind to a code behind method GetName

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

Then in your Code behind

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

Option 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM