我的代码在c#中不能用于转发器控制验证 [英] My code is not working in repeater control validation in c#

查看:62
本文介绍了我的代码在c#中不能用于转发器控制验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

转发器控制如下



课程金额主要代码

RPST 400 PSTF
RPSCRB 500 PSTF
MFA 600 AMC



i想验证第2行和第3行主码必须相同。

我写了如下代码

  protected   void  Button1_Click( object  sender,EventArgs e)
{
string code = string .Empty;
string code1 = string .Empty;
code =( this .rptcart.Items [ 2 ]。FindControl( Majorcode as Label).Text;
code1 =( this .rptcart.Items [ 3 ]。FindControl( Majorcode as Label).Text;

if (code!= code1)
{
lblmsg.Text = 主要代码必须相同;
return ;
}
}



当我运行上面的代码时,显示错误如下

  object 引用 set  object的实例



以上错误在下面的行显示如下

 code =( this  .rptcart.Items [ 2 ]。FindControl(  Majorcode as  Label).Text; 



请帮助我从上面的代码我犯了什么错误。

解决方案

你想要比较列 Majorcode 的第2行和第3行,所以你应该读取索引1和2,而不是2和3.

 code =( this  .rptcart.Items [ 1 ]。FindControl(  Majorcode as  Label).Text; 
code1 =( this .rptcart.Items [ 2 ]。FindControl( Majorcode as Label).Text;


Repeater control as follows

Course   Amount   Majorcode

RPST     400    PSTF
RPSCRB   500    PSTF
MFA      600     AMC


i want to validate the 2nd row and 3rd row Majorcode must the same be same.
for that i written a code as follows

protected void Button1_Click(object sender, EventArgs e)
{
    string code = string.Empty;
    string code1 = string.Empty;
    code = (this.rptcart.Items[2].FindControl("Majorcode") as Label).Text;
    code1 = (this.rptcart.Items[3].FindControl("Majorcode") as Label).Text;

    if (code != code1)
    {
        lblmsg.Text = "Majorcode must be the same";
        return;
    }
}


When i run the above code shows errors as follows

object reference not set to an instance of object.


The above error shows in below line as follows

code = (this.rptcart.Items[2].FindControl("Majorcode") as Label).Text;


please help me from my above code what is the mistake i made.

解决方案

As you want to compare the column Majorcode of 2nd and 3rd Row, so you should read the index 1 and 2, not 2 and 3.

code = (this.rptcart.Items[1].FindControl("Majorcode") as Label).Text;
code1 = (this.rptcart.Items[2].FindControl("Majorcode") as Label).Text;


这篇关于我的代码在c#中不能用于转发器控制验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆