简体   繁体   English

null引用with和if语句c#

[英]null reference with and if statement c#

I am just want to check for empty text boxes and change the text the boxes if they are null in RowEditing event. 我只是想检查空文本框,如果在RowEditing事件中它们为空,则将文本更改为框。 I just can't figure this out. 我只是想不出来。 Of course the some of the boxes will be empty when the Grid is populated. 当然,当填充网格时,一些框将为空。 The other question is am I placing this in the right event? 另一个问题是我是否将其置于正确的事件中?

Here is the row editing event : 这是行编辑事件:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{        
    fill_grid();

    //Set the edit index.
    GridView1.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
    check_grid_boxes();
    GridView1.DataBind();
}

Here is the check_grid_boxes Method : 这是check_grid_boxes方法:

protected void check_grid_boxes()
{
    if (gtxtLane.Text == "")
    {
        gtxtLane.Text = "0";
    }
    else if (gtxtCarriers.Text == "")
    {
        gtxtCarriers.Text = "0";
    }
    else if (gtxtREV.Text == "")
    { 
        gtxtREV.Text = "0";
    }
    return;
}

Before you mention Java Script or Jquery. 在您提到Java Script或Jquery之前。 This is a web control and my attempts at using java has not worked. 这是一个Web控件,我尝试使用java无法正常工作。

I changed my code to this : 我将代码更改为:

  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            fill_grid();
            GridView1.EditIndex = e.NewEditIndex;

            var lane = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("gtxtLane");
            var car = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("gtxtCarriers");
            var badcar = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("gtxtBadCarriers");

            if (String.IsNullOrEmpty(lane.Text))
            {
                lane.Text = "0";
            }
            else if (String.IsNullOrEmpty(badcar.Text))
            {
                badcar.Text = "0";
            }
            else if (String.IsNullOrEmpty(car.Text))
            {
                car.Text = "0";
            }
               GridView1.DataBind();
         }

You must get a reference to the TextBoxes that are inside that row being edited like this: 您必须获得对正在编辑的行内的TextBox的引用,如下所示:

GridView1.EditIndex = e.NewEditIndex;

TextBox gtxtLane = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("gtxtLane");

DUH!!!! 杜!!!! how bout --> select isnull(lane,'0') as Lane <---. 怎么回合 - >选择isnull(lane,'0')作为Lane <---。 I can't believe I didn't think of that!! 我简直不敢相信我没想到!! 6 hours wasted!! 浪费了6个小时!!

You could use the nulldisplaytext property of the gridview 您可以使用gridview的nulldisplaytext属性

<asp:boundfield datafield="discounttype"
            nulldisplaytext="0"
            headertext="Discount Type"/>

I'm guessing you're getting a reference problem, because the compiler doesn't know where to get gtxtLane and the rest from. 我猜你得到了一个参考问题,因为编译器不知道从哪里得到gtxtLane和其余的。 Remember that each row in the grid has its own version of these controls, so you will need to reference them directly. 请记住,网格中的每一行都有自己的这些控件版本,因此您需要直接引用它们。 You can use FindControl on the row object. 您可以在行对象上使用FindControl You can get a reference for the row object from GridViewEditEventArgs . 您可以从GridViewEditEventArgs获取行对象的引用。 Your code will look something like this ( e refers to GridViewEditEventArgs ) 你的代码看起来像这样( e指的是GridViewEditEventArgs

var gtxtLane = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("gtxtLane");
if (gtxtLane.Text == "")
{
    gtxtLane.Text = "0";
}

e.NewEditIndex gets you the index of the row being edited, we use that to get the row object from the gridview, then we find the wanted control, and cast it to TextBox , then use it. e.NewEditIndex获取正在编辑的行的索引,我们使用它来从gridview获取行对象,然后我们找到所需的控件,并将其转换为TextBox ,然后使用它。 Rinse and repeat. 冲洗并重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM