簡體   English   中英

從gridview asp.net獲取價值時出錯

[英]Error while getting value from gridview asp.net

我有一個GridView,並且正在動態綁定網格。 在此網格中,我想使第二個單元格可編輯。我能夠做到這一點,在修改文本框后,我將單擊“提交”按鈕。 在這里,我的問題是在按鈕單擊事件中,我無法獲取文本框值。

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

這里dty拋出錯誤,請問有什么可以幫助的嗎?

在獲取文本之前,您需要將對象投射到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;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM