Validation to find out if datagrid has any rows

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to run some validation on the on-click event of a button to check
and see whether a datagrid has any items inserted into it and if it does then
I'd like it to work as normal. However if it has no items in it then i'd like
it to display the error message below and not move forward. Here is the code
I have but it doesn't seem to work. I think i've got the wrong logic. Can
someone help me work this out.
Thanks

private void cicmdPerformSearch_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
if (this.dgSearchAddresses.Items.Count == 0)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
Context.Items["TransferAddresses"] = addresses;
Server.Transfer("../Internet/WebForm2.aspx");
}
else
{
pnlMessages.Visible = true;
this.lblMessage.Text = "You must select at least one address";
}
}
 
Hi Stephen,

Do you have viewstate turned on for the datagrid or do you reload on
postback? Depending on how your viewstate is setup, I believe your .Count
would be -0- until reloaded on postback. Maybe during the initial load you
can store the .Count in a single variable in viewstate and then read that on
the cicmdPerformSearch_Click event.

-Mike S.
 
Back
Top