如何将值从.aspx文件发送到.cs文件

本文关键字:文件 cs aspx | 更新日期: 2023-09-27 17:56:59

我在网页上做了一个链接,比如

<a href="Products.aspx?id='something'">

现在,此 ID 发送到页面Products.aspx我希望页面的Products.aspx.cs文件中的 id 值,以便我可以编写类似

select * from Categories Where CategoryID = 'something'

如何将值从.aspx文件发送到.cs文件

尝试使用以下内容,下面我提到了在代码中获取查询字符串值。

        string Id = string.Empty;
        if (Request.QueryString["id"] != null)
        {
            Id = Request.QueryString["id"].ToString();
        }
        var query = " select* from Categories Where CategoryID = '" + Id + "'";
您可以使用

如下所示Query String发送 id:

if(Request.QueryString["id"] != null)
{
     string value = Request.QueryString["id"].ToString();
}

您有很多方法可以将 id 从一页传递到另一个页面名称

产品.aspx还是产品.aspx.cs

病解释双向一个是Session,另一个是QueryString.

会话考试

会话:在页面 A 上分配 ID 的值

会话["id"]='某物';

在第 Products.aspx 页上获取价值

字符串 Val=Session["id"]。ToString();

查询字符串考试

查询字符串:在页 A 上分配 ID 的值

字符串 DymanicURL = 字符串。Format('"Products.aspx?id={0}'", Val); Response.Redirect(DymanicURL);

在第 Products.aspx 页上获取价值

字符串 x = 请求.查询字符串["id"];

欲了解更多信息 http://www.dotnetperls.com/querystring和 https://msdn.microsoft.com/en-us/library/6c3yckfw.aspx

您可以使用以下语法在目标产品 cs 页面中获取 ID 值

string ID = Request.QueryString["id"].tostring();

您可以获取以描述的方式传递的查询字符串值

var categoryId = Request.QueryString["CategoryId"]

当然,在继续之前,您需要检查它是否为空或为空......

尝试使用以下方法

string Qvalue="";
if(Request.QueryString["id"] != null) {
     Qvalue = Request.QueryString["id"].ToString(); }

您的查询应如下所示

select * from Categories Where CategoryID = '"+Qvalue+"'

如果你想将ID从一个页面发送到另一个CS页面,那么你可以使用属性

class Person
{
    private string name;  // the name field
    public string Name    // the Name property
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
}

在ASPX页面上,您可以对以下属性进行值:

Person person = new Person();
person.Name = "Joe";  // the set accessor is invoked here
OR
person.Name = txt_Name.Text;  // the set accessor is invoked here  

之后在CS页面上获取数据。

System.Console.Write(person.Name);  // the get accessor is invoked here
OR
string Value= person.Name;  // the get accessor is invoked here