参数传递中的#等特殊符号的使用方法

来源:互联网 发布:怎么能才能成为淘宝客 编辑:程序博客网 时间:2024/06/10 15:02

参数传递中的#等特殊符号的使用方法

      突然接到最近给"发那科数控"做的<出入库管理系统>的使用者给我的电话,说有些型号的零件无论如何都无法出库!这下可急死我了.你说不能就都不能啊.怎么还来个全都能用,只是个别的不能出库的呢?随后电话再一次问询了不能出库的型号列表.发现了一个问题:所有的零件号中都有一个"#"号!问题就出在这里!我初步判断了一下,于是去找完程序,看了我写的代码:

      点击要出库的零件号后处理的代码为:

     public int stockid;    public string partid;    protected void Page_Load(object sender, EventArgs e)    {        //初始化信息        stockid = Convert.ToInt32(Request.Params["StockID"].Trim());        partid=Request.Params["PartID"].Trim();        Drip.BLL.Stock stock = new Drip.BLL.Stock();        //显示仓位编码,并将仓位编号写入隐藏        Tx_StockID.Value = stock.GetModel(stockid).StockID.ToString();        Tx_StockNum.Text = stock.GetModel(stockid).StockNum.ToString();        //显示仓位信息结束        StockLostAmount.Text = stock.GetModel(stockid).StockAmount.ToString();        //显示货物信息        Drip.BLL.PartAct partact = new Drip.BLL.PartAct();        PartLostAmount.Text = partact.GetModel(partid).Amount.ToString();        Tx_PartID.Text = partid.ToString();        Tx_PartID.ReadOnly = true;               //显示货物入库信息        Drip.BLL.InStore instore = new Drip.BLL.InStore();        Tx_InStoreTime.Text = instore.GetModel(Convert.ToInt32(Request.QueryString["InStoreID"].Trim())).InStoreTime.ToShortDateString();        Tx_InStoreTime.ReadOnly = true;        Tx_InBillNum.Text = instore.GetModel(Convert.ToInt32(Request.QueryString["InStoreID"].Trim())).InBillNum.ToString().Trim();        Tx_InBillNum.ReadOnly = true;        IsInBook.SelectedValue = instore.GetModel(Convert.ToInt32(Request.QueryString["InStoreID"].Trim())).IsInBook.ToString().Trim();            }
再看一下参数是怎么传递过来的:
<asp:HyperLinkField HeaderText="操作" Text="出库" DataNavigateUrlFormatString="OutStoreStep2.aspx?StockID={0}&amp;PartID={1}&amp;InStoreID={2}" DataNavigateUrlFields="货位标识,零件号,InStoreID"></asp:HyperLinkField>
这一弄就明白了.再通过Response.Write先对接收的参数进行读取.发现如果参数为A07C-B705-1001#8976的话,传递后的PartID值只为A07C-B705-1001.百思不得其解啊..当时是网也不能上,电话也不能打.最后把partid=Request.Params["PartID"].Trim();改成partid=Request.Params1].Trim();还是不行.正在想放弃等晚上能上网再改时一下记起.#应该进行编码后再传递!! 于是,马上改代码: 传递代码改成:(先将HyperLinkField改成模板列)
 <asp:TemplateField HeaderText="出库">        <ItemTemplate>            <asp:HyperLink  runat="server" Text="出库" NavigateUrl='<%# "~/OutStoreStep2.aspx?StockID="+ Eval("货位标识") + "&PartID=" + Server.UrlEncode((string)Eval("零件号")) +"&InStoreID="+Eval("InStoreID")%>' ></asp:HyperLink>        </ItemTemplate>    </asp:TemplateField>

这样一来,就对传递进行了编码了. 在参数传递过程中,经常对数据要进行编码传递,特别是在进行中文参数传递和特殊字符参数传递中.最好进行编码.同时,本文也提交其它相关技巧:

1.Url传递中文参数解决方案 

        // Response.Write(Server.HtmlDecode("aaaaaaaaaaaaaaaaaaaaa<font >ass  ssssss</font>"));        // <%...# Server.HtmlEncode((string)DataBinder.Eval(Container.DataItem,"内容")) %>//Url传递中文参数解决方案 a href="Admin_SmallSort.aspx?iProductBigSort_ID=<%...# Eval("iProductBigSort_ID")%>&sProductBigSort=<%...# Server.UrlEncode(Eval("sProductBigSort").ToString())%>"><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "~/DeleteUser.aspx?UserID="+ Server.UrlEncode((string)Eval("UserID")) + "&UserName=" + Server.UrlEncode((string)Eval("UserName")) %>' >
<%#   GetEncode(DataBinder.Eval(Container.DataItem,   "Name").ToString())%>          public   string   GetEncode(string   str)       {           return   Server.UrlEncode(str);           //return   HttpUtility.UrlEncode(str);       }
2.获取上一个页面控件的值
     if (Page.PreviousPage!=null)         {             TextBox a = (TextBox)Page.PreviousPage.FindControl("TextBox1");             a.Text;          }
3.比较日期大小
DateTime t1 = DateTime.Now; //DateTime t2 = Convert.ToDateTime("9:10:59");int a = DateTime.Compare(t1, t2); //a=1,t1大//从此实例中减去指定的日期和时间SubtractDateTime t1 = System.DateTime.Now; //系统时间DateTime t2 = Convert.ToDateTime(Row["dtime"].ToString()); //最后在线时间TimeSpan d3= t2.Subtract(t1);int a = d3.Minutes;   //t2与t1的差几分钟 获取由当前 TimeSpan 结构表示的整分钟数
 4.判断字符串是否为空 判断字符串是否为空String.IsNullOrEmpty(str) == True   则说明字符串为空(null 和 

"");

 

 TextBox1.Text = "aaa";     if (TextBox1.Text == string.Empty)   // TextBox1.Text=null 或 ="" 都为true     {         Response.Write("空");     }     else     {         Response.Write("非空");     }

 

原创粉丝点击