【问题标题】:How to send a value from .aspx file to .cs file如何将值从 .aspx 文件发送到 .cs 文件
【发布时间】:2016-07-14 09:56:07
【问题描述】:

我在网页上做了一个链接

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

现在这个发送 id 到一个页面Products.aspx 我想要页面的Products.aspx.cs 文件中的这个 id 值,这样我就可以编写类似的查询

select * from Categories Where CategoryID = 'something'

【问题讨论】:

标签: c# asp.net .net visual-studio web


【解决方案1】:

尝试使用以下方法,下面我提到在后面的代码中获取查询字符串值。

        string Id = string.Empty;

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

        var query = " select* from Categories Where CategoryID = '" + Id + "'";

【讨论】:

  • 那时我才刚刚开始 Web 开发,对这些事情一无所知,但现在这个问题被标记为 -2,因此我无法提出新问题。我该如何摆脱它?
【解决方案2】:

您可以使用Query String 发送 id,如下所示:

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

【讨论】:

    【解决方案3】:

    您有很多方法可以将 id Form One Page 传递给另一页 Name

    Products.aspx 或 Products.aspx.cs

    我会解释两种方式 一个是Session,另一个是QueryString

    会话示例......

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

    Session["id"]='something';

    在页面上获取价值Products.aspx

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

    查询字符串示例......

    QueryString:在页面 A 上分配 ID 的值

    string DymanicURL = string.Format("Products.aspx?id={0}", Val); Response.Redirect(DynamicURL);

    在页面上获取价值Products.aspx

    字符串 x = Request.QueryString["id"];

    更多信息http://www.dotnetperls.com/querystringhttps://msdn.microsoft.com/en-us/library/6c3yckfw.aspx

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 此请求需要任何库吗?因为它下面有一条红线。
      【解决方案5】:

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

      var categoryId = Request.QueryString["CategoryId"]
      

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

      【讨论】:

        【解决方案6】:

        尝试使用以下内容

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

        您的查询应如下所示

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

        【讨论】:

          【解决方案7】:

          如果您想将 id 从一个页面发送到另一个 CS 页面,那么您可以使用 properties

          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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-31
            • 1970-01-01
            • 1970-01-01
            • 2020-09-20
            相关资源
            最近更新 更多