0

I am submitting inputs from gridview to database on button click. Here, I am checking if a particular column value is left blank or not on submit. If it is blank, I need to diplay an alert message with particular row number. i.e. if in row no 3 the col3 value is left blank and submitted, I should get message :Please fill col3 value in row no 3"

As of now I dont know how to display the row number.

This is my code :

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    Label col = (Label)GridView1.Rows[i].FindControl("col3");
    if(col.Text == string.Empty)
    {
    StringBuilder s = new StringBuilder();
    s.Append("<script language='javascript'>");
    s.Append("alert(' Please fill col3 value!!')");
    s.Append("</script>");
    RegisterStartupScript("Please fill col3 value !!'", s.ToString());
    }
    else
   {
    // rest code
   }
}
4

1 に答える 1

0

以下のようにコードを変更します。すべての行を反復処理し、空の col3 列をすべて見つけて、それらを 1 つのアラートに表示します。

StringBuilder s = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
    Label col = (Label)GridView1.Rows[i].FindControl("col3");
    if(string.IsNullOrEmpty(col.Text))
    {
        s.Append("Please fill col3 value in row no " + i.ToString() +"\\n");
    }
}   
if (!string.IsNullOrEmpty(s.ToString()))
{
    string script = "alert('" + s.ToString() + "');";
    ClientScript.RegisterStartupScript(this.GetType(), "alertmessage", script, true);
}
于 2012-11-05T20:25:11.350 に答える