2016-10-07 8 views
0

私はGridViewを持っており、グリッドを動的にバインドしています。このグリッドでは、2番目のセルを編集可能にしたいと思います。これを行うことができます。また、テキストボックスを変更した後、[送信]ボタンをクリックします。ここで私の問題は、ボタンのクリックイベントです私はテキストボックスの値を取得することはできません。ここDTYでgridview asp.netから値を取得中にエラーが発生しました

コード

<asp:GridView ID="DGridView" runat="server" Font-Size="Small" Width="40%" PageSize="4" ShowHeader="False" OnRowDataBound="DGridView_RowDataBound" AutoPostBack="True" />       

protected void DGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     TextBox txtseed = new TextBox(); 
     txtseed.ID = "txtseed"; 
     txtseed.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "seed")); 
     e.Row.Cells[1].Controls.Add(txtseed); 
    } 

} 


protected void butSubmit_Click(object sender, EventArgs e) 
{ 
    for (int i = 0; i < DGridView.Rows.Count; i++) 
    { 
     strDNo = DGridView.Rows[i].Cells[0].Text; 

     dty = DGridView.Rows[i].Cells[1].FindControl("txtseed").ToString(); 
    } 
} 

は、いずれかのヘルプは、エラーをすることができます投げていますか?

+0

エラーの詳細を記入できますか? –

+0

その付与オブジェクト参照エラー – poc

+0

この行にエラーが発生しました "dty = DGridView.Rows [i] .Cells [1] .FindControl(" txtseed ")。ToString();"? –

答えて

0

テキストを取得する前に、オブジェクトをTextBoxにキャストする必要があります。データソースの設定を忘れないでください。あなたはそれが動作しているこのコードに従うことができます。

public class Test 
    { 
     public string Seed { get; set; } 
    } 

protected void Page_Load(object sender, EventArgs e) 
    { 
     List<Test> test = new List<Test>(); 
     test.Add(new Test() {Seed = "ssss"}); 
     test.Add(new Test() { Seed = "aaaa" }); 
     DGridView.DataSource = test; 
     DGridView.DataBind(); 
    } 



    protected void DGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       TextBox txtseed = new TextBox(); 
       txtseed.ID = "txtseed"; 
       txtseed.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "seed")); 
       e.Row.Cells[0].Controls.Add(txtseed); 
      } 

     } 

protected void Button1_OnClick(object sender, EventArgs e) 
    { 
     for (int i = 0; i < DGridView.Rows.Count; i++) 
     { 
      var strDNo = DGridView.Rows[i].Cells[0].Text; 

      TextBox dty =(TextBox)DGridView.Rows[i].Cells[0].FindControl("txtseed"); 
      var z = dty.Text; 
     } 
    } 
+0

あまりにもSystem.NullReferenceExceptionを与える:オブジェクト参照は、オブジェクトのインスタンスに設定されていません。私たちはオートポストを正しいものに戻さなければなりませんか? – poc

関連する問題