Reputation:
I'm trying to validate a GridView
using its onRowCommand
.
When I select a button, it needs to validate that rows delivery date
and shows an error message inline if invalid.
Currently, my GridView
is setup like this:
<asp:GridView ID="gvDistribution" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1000px" DataKeyNames="id" AllowPaging="True" OnPageIndexChanging="gvDistribution_PageIndexChanging" PageSize="5" OnRowCommand="gvDistribution_OnRowCommand" OnRowDataBound="gvDistribution_RowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="name" HeaderText="Beneficiary Name"></asp:BoundField>
<asp:BoundField DataField="packingDate" HeaderText="Packing Date" DataFormatString="{0:dd/M/yyyy}"></asp:BoundField>
<asp:BoundField DataField="deliveryDate" HeaderText="Delivery Date" DataFormatString="{0:dd/M/yyyy}" />
<asp:TemplateField HeaderText="Status" ItemStyle-Width="120px">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="View" Text="View Details" />
<asp:ButtonField CommandName="Action" Text="Action" />
<asp:TemplateField HeaderText="" ItemStyle-Width="120px">
<ItemTemplate>
<asp:Label ID="lblError" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code behind for GridView
onRowCommand
:
protected void gvDistribution_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int rowNo = int.Parse(e.CommandArgument.ToString());
string distributionID = this.gvDistribution.DataKeys[rowNo].Value.ToString();
if (e.CommandName == "View")
{
Response.Redirect("PackingDetails.aspx?id=" + distributionID);
}
else if(e.CommandName == "Action")
{
DistributionPacking distPack = new DistributionPacking();
distPack = packBLL.getDateDetailByDistributionID(distributionID);
DateTime deliveryDate = distPack.deliveryDate;
foreach (GridViewRow g in gvDistribution.Rows)
{
if (deliveryDate > DateTime.Today)
{
Label lblError = (Label)g.FindControl("lblError");
lblError.Text = "Delivery date has not reach yet!";
}
else
{
bool result = packBLL.updateDeliveryStatus(distributionID);
if (!result)
{
//Call the retrieve all distributions without any filtering again
List<DistributionPacking> allDistribution = new List<DistributionPacking>();
allDistribution = packBLL.getAllDistribution();
gvDistribution.DataSource = allDistribution;
gvDistribution.DataBind();
}
}
}
}
}
The issue is that when the delivery date
gets validated, an error message is displayed in every row, not just the one needing validation.
Could someone show me how to force only one message in the validated row?
Upvotes: 0
Views: 1016
Reputation: 460158
Then replace the loop beginning with
foreach (GridViewRow g in gvDistribution.Rows){}
with
if(gvDistribution.SelectedRow != null)
{
if (deliveryDate > DateTime.Today)
{
Label lblError = (Label)gvDistribution.SelectedRow.FindControl("lblError");
lblError.Text = "Delivery date has not reach yet!";
}
else
{
// ...
}
}
Upvotes: 3