나는 행이 다른 과목에서 해당 표시에 대한 학생과 열을 나타내는 GridView
을가집니다. 학생 수는 다양합니다. 따라서 GridView
의 행 수를 알 수 없습니다. 사용자는 마크를 입력 할 때 런타임에 학생 수를 지정합니다. 각 행에는 각 제목에 대한 TextBox가 있습니다.ASP.NET의 GridView 셀에서 동적으로 생성 된 TextBox의 내용을 검색하는 방법
사용자가 계산 버튼을 클릭하면 모든 TextBoxes
의 값을 검색해야합니다.
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
List<TextBox> list = new List<TextBox>();
if (ViewState["Table"] != null)
Assessments = (DataTable)ViewState["Table"];
int count = 1;
foreach (DataRow row in Assessments.Rows)
{
TextBox txt = new TextBox();
txt.ID = "AsTxt";
txt.Text = string.Empty;
txt.TextChanged += OnTextChanged;
e.Row.Cells[count].Controls.Add(txt);
count += 2;
listd.Add((e.Row.DataItem as DataRowView).Row[0].ToString() + "Txt");
}
}
}
그리고 계산 버튼 :
protected void CalculateBtn_Click(object sender, EventArgs e)
{
GridViewRow rr = GridView2.Rows[0];
TextBox rrrr = (rr.FindControl("AsTxt") as TextBox);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + rrrr.Text + "')", true);
}
항상 NullReferenceException이를 반환과 같은 코드가 작동하지 않았다, 즉 RRRR가 null입니다
나는 다음 코드를 시도했다.아무도 도와 줄 수 있습니까?
한 줄에는 여러 개의 'AsTxt'가 있다고 생각합니다. – Aria
@aria no, foreach 블록을 제거해도 문제가 지속됩니다. –
행 0이'AsTxt'를 가지고 있는지 확인하고'TextBox box1 = (TextBox) Gridview1.Rows [0] .Cells [1] .FindControl ("AsTxt");' – Aria