Reputation: 4607
I have a gridview which has a column of textboxes called 'Quantity'. Now, I want to execute an event handler when the text in the column changes.
This is the code for the gridview:
<asp:GridView ID="GridView_Products" runat="server" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"
HorizontalAlign="Center" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="Textbox_Quantity" runat="server" Width="30px" OnTextChanged="Text_ChangedEvent"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<img src="Images/<%# Eval("Image_URL") %>" width="80" height="100" alt="Image" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
This is the Text_ChangedEvent event handler:
protected void Text_ChangedEvent(object sender, EventArgs e)
{
Validation val = new Validation();
TextBox textbox_quantity = ((TextBox)(sender));
GridViewRow row = ((GridViewRow)(textbox_quantity.NamingContainer));
if (textbox_quantity.Text.Equals("0") == true)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The quantity cannot be 0!";
}
else
{
if (val.IsNumeric(textbox_quantity.Text) == false)
{
Label_Error.Visible = true;
Label_Error.Text = "Error Message: The quantity must be numeric!";
}
else
{
total = total + (Convert.ToDouble(textbox_quantity.Text) * Convert.ToDouble(row.Cells[5].Text));
transaction.Add(textbox_quantity.Text);
}
}
}
Why is the event handler not executing?
Upvotes: 0
Views: 1235
Reputation: 3231
The event will fire when you post back to the page, when something changes client side there is no way for the server to know something has changed until the results are posted back.
Upvotes: 1
Reputation: 677
You need to set AutoPostBack="True"
.
You should consider doing validation on the client side using JavaScript.
Upvotes: 1