呵呵,如此错误。让我琢磨不透!

博客展示了两段C#代码,一段用于从URL中截取部分内容,另一段在页面加载时根据URL进行判断并设置控件属性。但代码运行出现System.ArgumentOutOfRangeException异常,错误出现在字符串截取方法处,索引和长度超出字符串范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是用notepad(记事本)写的代码
None.gif using  System;
None.gif
using  System.IO;
None.gif
None.gif
public   class  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public static void Main(string[] arg)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
if(arg.Length<1)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
string url=arg[0];
InBlock.gif      
int begin=url.LastIndexOf("/")+1;
InBlock.gif      Console.WriteLine(begin);
InBlock.gif      
int end=url.LastIndexOf("?")-url.LastIndexOf("/")-1;
InBlock.gif      Console.WriteLine(end);
InBlock.gif      Console.WriteLine(url.Substring(begin,end));
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}
 
用来截获字符子串,参数在arg里面,运行正常。
下面是在asp.net里面写的语句
None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
// 在此处放置用户代码以初始化页面        
InBlock.gif
            string url=Request.Url.ToString().Trim();
InBlock.gif    
if(url.Length>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
int begin=url.LastIndexOf("/")+1;
InBlock.gif                                
string compUrl="bookdetail.aspx";
InBlock.gif                
if(url.Substring(begin,15)==compUrl)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                
InBlock.gif                    
this.DataList1.RepeatDirection=RepeatDirection.Horizontal;
InBlock.gif                    
this.DataList1.RepeatColumns=20;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            DataList1.DataSource
=new guanli.Component_TuShu().GetTypeList();;
InBlock.gif            DataList1.DataBind();
ExpandedBlockEnd.gif        }
这是想实现动态改变我们的Datalist的外观,当请求的页面不同时,它的显示也是不同的。



但是,它在运行时提示如下错误:
None.gif 异常详细信息: System.ArgumentOutOfRangeException: 索引和长度必须引用该字符串内的位置。参数名:length
None.gif
None.gif源错误: 
None.gif
None.gif
None.gif行 
28 :                  // int end=url.LastIndexOf("?")-url.LastIndexOf("/")-1;            
None.gif
行  29 :                  string  compUrl = " bookdetail.aspx " ;
None.gif行 
30 :                  if (url.Substring(begin, 15 ) == compUrl)
ExpandedBlockStart.gifContractedBlock.gif行 
31 :                  dot.gif {                
InBlock.gif行 
32:                     this.DataList1.RepeatDirection=RepeatDirection.Horizontal;
InBlock.gif 
InBlock.gif
InBlock.gif源文件: f:\myweb\myprg\typelist.ascx.cs    行: 
30 
InBlock.gif

实在是琢磨不透。能给点建议不?

转载于:https://www.cnblogs.com/3zfp/archive/2004/11/09/61935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值