how to pass a value from a database field or TextBox via <input value=> tag

Gary Taylor 0 Reputation points
2023-12-08T11:30:13.1366667+00:00

I want to use the <input> tag to pass a string to another site/app. I've already managed this with two other webpages but in both cases the value is taken from a fixed list of values from a radio-button list.

In this scenario I want to pass the content of the value-field from a database field or a textbox in the .aspx page.

Is there a simple way to achieve this as I have a limited amount of time to try to implement this.

For example, and I realise neither of these would work, <input id="total" value=objDatabaseTable.DatabaseTable_Total> or <input id="total" value=tbTotal.Text>

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,577 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. QiYou-MSFT 4,326 Reputation points Microsoft Vendor
    2023-12-15T09:08:00.6566667+00:00

    Hi @Gary Taylor

    First, you want to use the <input> tag to pass a string to another site/app. Do your requirements transfer the value from one project to another? For example, the input value of Project1/Webform1 .aspx is passed to the input of Project2/Webform2.aspx?

    Then I can provide you with two methods:

    1.Via Querystring

    Add "runat="server"" to make this control a server control.

     <input id="input1" runat="server" type="text" value="Test" />
    

    This way you can handle this control in aspx.cs.

    Next, we make the URL of the page to be sent and the value to be sent into a URL with QueryString for transmission.

    string url;
     url = "https://localhost:44394/WebForm2?value=" + input1.Value;
     Response.Redirect(url);
    

    Then we can get the QueryString via Request.QueryString.

    input1.Value = Request.QueryString["value"];
    

    URL1

    Here's the full code:

    Webapplication1/WebForm1.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                   This is WebApplication1/WebForm1.aspx
                <input id="input1" runat="server" type="text"  />
                <asp:Button runat="server" ID="button1" OnClick="button1_Click" Text="Submit" />
            </div>
        </form>
    </body>
    </html>
    
    using System;
    using System.Collections.Generic;
    using System.Data.SqlTypes;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
               
            }
    
            protected void button1_Click(object sender, EventArgs e)
            {
                string url;
                url = "https://localhost:44394/WebForm2?value=" + input1.Value;
                Response.Redirect(url);
            }
        }
    }
    

    Webapplication2/WebForm2.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                This is WebApplication2/WebForm2.aspx
                <input type="text" id="input1" runat="server" />
            </div>
        </form>
    </body>
    </html>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication2
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                input1.Value = Request.QueryString["value"];
            }
        }
    }
    

    2.But this method will cause the value to be leaked, and you can also do this by using the sqlcommand database operation method.

    Best regards,

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.