combobox on selected index changed only fires when press enter - asp.net

Hello as the title states my OnSelectedIndexChanged event only fires when you select the item then press Enter. My combobox and gridview get their values from a datasource. OnSelectedIndexChanged the gridview filters but the combobox always has all the values. Any ideas why this happening?
asp
<asp:combobox ID="cbCustomer" runat="server" AutoCompleteMode="SuggestAppend" AutoPostBack="True"
DataSourceID="dataSourceCBCust" DataTextField="CUST_NAME" DataValueField="CUST_NAME" MaxLength="0"
style="display: inline;" OnSelectedIndexChanged="cbCustomer_SelectedIndexChanged">
</asp:combobox>
<asp:UpdatePanel ID="upCustomer" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cbCustomer" EventName ="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="gvCustomer" runat="server" AllowPaging="True" DataSourceID="dataSourceGVCust"
AutoGenerateColumns="True" Visible="true" ShowHeaderWhenEmpty="True">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
vb
Protected Sub cbCustomer_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbCustomer.SelectedIndexChanged
'do stuff
End Sub

Related

How to set processing time event between SelectedIndexChanged and Normal Panel Update events?

i'm a vb.net developer and i'm facing with asp.net, so I'm a beginner with asp. I've triggered an event to SelectedIndexChanged of a list box; event is like that:
Protected Sub cmb_year_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmb_year.SelectedIndexChanged
Label1.BackColor = Drawing.Color.Red
UpdatePanel1.Update()
End Sub
My problem is that pass about 4 seconds between my manual index change and the real panel update. I've set UpdateMode of panel Conditional.
Server runs locally and I'm using default developing environment (IIS express).
There is something that I don't understand about asp.net dynamics?
Use update panel with triggers.
Sample aspx mark up:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:DropDownList runat="server" ID="ddl" OnSelectedIndexChanged="cmb_year_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Text="Item 1" />
<asp:ListItem Text="Item 2" />
</asp:DropDownList>
<asp:UpdatePanel runat="server" ID="up1" UpdateMode="Conditional" ChildrenAsTriggers="False">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl" EventName="SelectedIndexChanged"/>
</Triggers>
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
Sample code behind:
Protected Sub cmb_year_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmb_year.SelectedIndexChanged
Label1.BackColor = Drawing.Color.Red
End Sub

Control event not firing from within updatepanel

I have a listbox that is being updated via a timer and working as expected inside an UpdatePanel.
However I cannot get the selectedindexchanged event to fire. I presume this is something to do with the partial postback. Does anybody know what I can do to make this work?
When I move it out of the UpdatePanel it works fine. However obviously I cannot do partial postbacks.
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
<asp:ListBox ID="ListBox_JobPositions" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged" runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
UPDATE:
Have now tried the below change, the timer event is still working but the selectedindexchanged event is not. I am getting lost with this.
<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
<asp:ListBox ID="ListBox_JobPositions" runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>
Here is the event that does not fire when the listbox is inside the UpdatePanel but does work when it is not.
protected void ListBox_JobPositions_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("test");
}
The reason you are not getting the event is that, your change event is not causing the PostBack. Your postback is caused by the timer.
The event asp.net receives is the timer event and not the ListBox event.
To resolve the issue, you should set AutoPostBack to true. This will cause the ListBox to do a PostBack as soon as the data changes and your event should fire.
<asp:ListBox ID="ListBox_JobPositions" AutoPostBack="True"
OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged"
runat="server" Height="750px" Width="300px"
DataSourceID="sqlDataSource"
DataTextField="Company"
DataValueField="Pid">
</asp:ListBox>
Since you have set the UpdateMode to Conditional, you should also set the ChildrenAsTriggers to true. This way way the List causes a PostBack, that too will be a partial update.
<asp:UpdatePanel ID="UpdatePanel" runat="server"
UpdateMode="Conditional"
ChildrenAsTriggers="True">
Works now, had to manually specify Async and Full Postback triggers. Thanks for your help.
<asp:UpdatePanel ID="UpdatePanel" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="500"></asp:Timer>
<asp:ListBox ID="ListBox_JobPositions" runat="server" Height="750px" Width="300px" DataSourceID="sqlDataSource" DataTextField="Company" DataValueField="Pid" OnSelectedIndexChanged="ListBox_JobPositions_SelectedIndexChanged" AutoPostBack="True"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
<asp:PostBackTrigger ControlID="ListBox_JobPositions" />
</Triggers>
</asp:UpdatePanel>

AsyncPostBackTrigger in a Repeater outside UpdatePanel

I am trying to get a cbxSupplement trigger updatepanel refresh, but am not sure if I am using a wrong EventName or it is just impossible to do it with CheckBox. If I replace CheckBox with Button, it works fine.
<asp:Repeater ID="repSupplements" runat="server">
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbxSupplement" />
</ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel runat="server" ID="up1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="repSupplements" EventName="CheckedChanged" />
</Triggers>
<ContentTemplate>
//Get checked items
</ContentTemplate>
</asp:UpdatePanel>
Since CheckBox controls inside repeater are not available at design time you should register them with ScriptManager.RegisterAsyncPostBackControl method. This method requires ScriptManager either on page or on master page.
Create a handler for Repeater.OnItemCreated event and there register newly created CheckBox. The code is following (note that CheckBox should have AutoPostBack property set to true):
<asp:Repeater ID="repSupplements" runat="server"
OnItemCreated="repSupplements_ItemCreated">
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbxSupplement" AutoPostBack="True" />
</ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel runat="server" ID="up1">
<Triggers>
</Triggers>
<ContentTemplate>
//Get checked items
</ContentTemplate>
</asp:UpdatePanel>
Codebehind:
protected void repSupplements_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var control = e.Item.FindControl("cbxSupplement");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}
This should do what you want.

combobox alternative with same functionality

Hello I have a combobox filled with CUSTOMERS then an updatepanel with a gridview also filled with CUSTOMERS (same as combobox). The user selects a customer from the combobox and then the gridview gets updated, the combobox always has all the customers. The problem I am having is the update panel is only firing the combobox selectedindexchanged the first time the user selects a customer, so if they would like to change the customer nothing will happen. Debugging it doesn't fire at all. It does work if I place the combobox in the updatepanel, but it is slow.
Is there any alternative to combobox with the same search/dropdown/autocomplete functionality?
Here's my current code if you want to see it
<asp:ComboBox ID="cbCustomer" runat="server" AutoCompleteMode="SuggestAppend"
AutoPostBack="True" DataSourceID="dataSourceCBCust" DataTextField="CUST_NAME"
DataValueField="CUST_NO" MaxLength="0" style="display: inline;">
</asp:ComboBox>
<asp:UpdatePanel ID="upCustomer" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cbCustomer" EventName ="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="gvSiteAddress" EventName ="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<br />
<asp:GridView ID="gvCustomer" runat="server" AllowPaging="True" DataSourceID="dataSourceGVCust"
AutoGenerateColumns="True" DataKeyNames="CUST_NO" Visible="False">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Backend
Protected Sub cbCustomer_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles cbCustomer.SelectedIndexChanged
dataSourceGVCust.SelectCommand = _
ConfigurationManager.AppSettings("SelectCustomer") _
& " WHERE CUST_NO LIKE '%" _
& cbCustomer.Text.TrimEnd _
& "%' ORDER BY CUST_NAME"
gvCustomer.Visible = True
End Sub

asp.ImageButton OnClick function inside GridView within an updatePanel

I have a GridView which is continually rebound using a timer and is within an updatePanel so the page doesn't refresh continually (each row has a countdown sequence so the gridview needs to continually get updated)
Inside the gridview i have an imagebutton with an OnClick method. Previously to get the OnClick method to fire I would make sure the gridView wasn't in an UpdatePanel and that the pageload databinding of the gridview was in an "If Not IsPostBack".
With the Timer though i can't have the gridview binding an an "If Not IsPostBack" and it needs to be in an UpdatePanel.
Is there a way to use an UpdatePanel and "If Not IsPostBack" and still get the OnClick method to be called?
Thanks
Here's some of the code, if my explanation didn't make complete sense:
UpdatePanel/Timer/GridView
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Timer ID="timerCountDown" runat="server" Interval="1000" OnTick="timerCountDown_Tick"></asp:Timer>
<asp:GridView ID="gridBuildQueue" runat="server" AutoGenerateColumns="False"
GridLines="none" ShowFooter="false" ShowHeader="false" Width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="cmdCancelBuild" ImageUrl="~/images/cancel.jpg" OnClick="ImageButton_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
OnTick timer method:
Protected Sub timerCountdown_Tick(ByVal sender As Object, ByVal e As EventArgs)
Me.gridBuildQueue.DataBind()
End Sub
ImageButton_Click method (which is currently never called):
Protected Sub ImageButton_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Dim imageButton As ImageButton = CType(sender, ImageButton)
Dim row As GridViewRow = CType(imageButton.NamingContainer, GridViewRow)
Dim button As ImageButton = DirectCast(row.FindControl("cmdCancelBuild"), ImageButton)
etc...
End Sub
You need to use GridView control events especially - RowCommand event.
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:GridView
ID="gridBuildQueue"
runat="server"
AutoGenerateColumns="False"
GridLines="none"
ShowFooter="false"
ShowHeader="false"
Width="100%"
onrowcommand="gridBuildQueue_RowCommand"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton
runat="server"
ID="cmdCancelBuild"
ImageUrl="~/images/cancel.jpg"
CommandName="cmd"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gridBuildQueue" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
Code behind:
protected Sub gridBuildQueue_RowCommand(ByVal sender As Object,ByVal e as GridViewCommandEventArgs)
if e.CommandName="cmd" Then
....
End If
End sub
In addition to RowCommand event, you must have to add AsyncPostBackTrigger entry for RowCommand event. (Set UpdatePanel.Triggers collection).
DEMO:
Markup
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Timer ID="Timer1" runat="server" Interval="1000">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
Code-Behind
Dim lst As New List(Of String)
Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Label1.Text = DateTime.Now
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If IsNothing(Session("lst")) Then
Session("lst") = lst
End If
GridView1.DataSource = lst
GridView1.DataBind()
End If
End Sub
Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
lst = Session("lst")
lst.Add(DateTime.Now.ToString())
GridView1.DataSource = lst
GridView1.DataBind()
End Sub

Resources