2012-10-18 32 views
0

我只是想檢查空的文本框,並更改文本框,如果他們在RowEditing事件中爲null。我無法弄清楚這一點。當網格填充時,一些框當然會是空的。另一個問題是我把這個放在正確的事件?空引用和if語句c#

這裏是行編輯事件:

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(); 
} 

這裏是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; 
} 

之前你提到Java腳本或jQuery的。這是一個網頁控制,我嘗試使用java沒有奏效。

我改變了我的代碼如下:

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(); 
     } 
+3

什麼行引發異常? –

+1

我知道這聽起來很迂腐,但我認爲你的意思是JavaScript而不是Java。相似的名字,兩個完全不同的東西。 –

+0

check_grid_boxes方法拋出異常。我正在想我如何關閉If語句只是使用返回是錯誤的? – briskovich

回答

1

DUH !!!!如何回合 - >選擇isnull(泳道,'0'),如< ---。我不敢相信我沒有想到!浪費了6個小時!

1

您一定要到屬於該行內部的文本框的引用正在編輯是這樣的:

GridView1.EditIndex = e.NewEditIndex; 

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

請看編輯。 – briskovich

+0

你現在有什麼問題? –

+0

我仍然收到空引用錯誤。對象未引用到對象的實例。 – briskovich

0

你可以使用的nulldisplaytext財產gridview

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

我猜你會得到一個參考問題,因爲噸他編譯器不知道從哪裏得到gtxtLane和其他。請記住,網格中的每一行都有自己的這些控件版本,因此您需要直接引用它們。您可以在行對象上使用FindControl。您可以從GridViewEditEventArgs獲得行對象的引用。您的代碼會是這個樣子(eGridViewEditEventArgs

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

e.NewEditIndex讓你的行的索引正在編輯,我們用它來在GridView獲取行對象,那麼我們就找到想要的控制,並將它投射到TextBox,然後使用它。沖洗並重復。

+0

我想過這件事情,但試圖這樣做。我沒有想到如何由編譯器提供控件。這很有意義。我遇到了與更新語句相同的問題,不得不使用FindControl將這些框添加爲參數。 – briskovich

+0

@briskovich - 你會遇到這樣的事情與所有中繼器樣式控制。所以這是一個很好的竅門。 –

+0

我做了更改,但仍然獲得空引用。檢查編輯。 – briskovich