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
Related
I have the next gridview:
<asp:UpdatePanel ID="upCustomer" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnCustomerSearch" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="gvPersons" EventName="PageIndexChanging" />
</Triggers>
<ContentTemplate>
<asp:TextBox ID="txtCode" runat="server" />
<asp:Button ID="btnCustomerSearch" runat="server" Text="Search" OnClick="btnCustomerSearch_Click" />
<asp:GridView ID="gvPersons" runat="server" AutoGenerateColumns="False" AllowPaging="True"
PageSize="10" GridLines="Vertical" EmptyDataText="No results"
ShowHeaderWhenEmpty="True" OnRowDataBound="gvPersons_RowDataBound"
OnPageIndexChanging="gvPersons_PageIndexChanging"
onselectedindexchanging="gvPersons_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="Personid" />
<asp:BoundField DataField="Name" HeaderText="Nombre" />
<asp:BoundField DataField="Phone" HeaderText="Telefono" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
</asp:GridView>
</ContentTemplate>
I need the next funcionality:
When I click btnCustomerSearch, I want an Ajax update (works)
When I click pagination, I want an Ajax update (works)
When I click a row (gvPersons_SelectedIndexChanging), I want a postback update, but this doesnt work.
Is posible to get an asyncpostback in some events in a gridview and postback in anothers?
Insert a Button inside your update panel and define a PostBackTrigger for it
<asp:UpdatePanel ID="upCustomer" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
...
<asp:PostBackTrigger ControlID="PostBackButton" />
</Triggers>
<ContentTemplate>
...
<asp:Button ID="PostBackButton" runat="server" Text="Button" OnClick="PostBackButton_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Insert a TemplateField on your grid like this
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="button" onclick="javascript:__doPostBack('<%= PostBackButton.ClientID %>',<%# ((GridViewRow)Container).RowIndex %>)" value="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
When the user clicks on any button inside the grid, we will have to manually fire the Click event of the PostBackButton passing the RowIndex as argument.
On the server side
protected void PostBackButton_Click(object sender, EventArgs e)
{
string argument = Request.Form["__EVENTARGUMENT"];
gvPersons.SelectedIndex = Convert.ToInt32(argument);
}
Now we just have to hide the PostBackButton...
protected void Page_Load(object sender, EventArgs e)
{
// ...
PostBackButton.Style.Add("display", "none");
}
...and disable the event validation of the page, otherwise we will get an ArgumentException when the user selects a row
<%# Page Language="C#" ... EnableEventValidation="false" %>
Yes. It is possible.
As stated here, you can try this
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnCustomerSearch" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="gvPersons" EventName="PageIndexChanging" />
<asp:PostBackTrigger ControlID="gvPersons" EventName="SelectedIndexChanging" />
</Triggers>
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
We have the following code in an UpdatePanel.
<asp:UpdatePanel
ID="UpdatePanelSearch"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<p>Parent Search:
<asp:TextBox ID="TextBoxSearch" runat="server" Width="207px"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" Text="Search" />
</p>
</ContentTemplate>
</asp:UpdatePanel>
The code in the VB file looks like this to handle clicking the Search button so a GridView will display data based on the value entered into the TextBox.
The GridView is also in a separate UpdatePanel:
Protected Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
GridViewParentsSummary.DataSource = theTableAdapter.GetData(strSearchText)
End Sub
We would like to create a trigger to update the GridView if that is the correct thing to do here.
Here is the GridView:
<ContentTemplate>
<asp:GridView
ID="GridViewParentsSummary"
runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID"
PageSize="3"
>
<Columns>
<asp:BoundField
DataField="FatherName"
HeaderText="Father's Name"
SortExpression="FatherName" />
<asp:BoundField
DataField="MotherName"
HeaderText="Mother's Name"
SortExpression="MotherName" />
<asp:ButtonField
ButtonType="Button"
CommandName="Select"
Text="Select This Parent" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Can you show the needed code required to make the correct trigger that will refresh the GridView?
If the GridView is in another UpdatePanel it should also update when another UpdatePanel updates. By default, the UpdatePanel.UpdateMode property is set to Always and this will cause all UpdatePanel in page to refresh.
However, this is not always the desired behavior so many time you'll change it to Conditional which means that the UpdatePanel will be refreshed only if one of its triggers was fired. In that case, you need to add this line in the ButtonSearch_Click method:
gridUpdatePanel.Update() 'assuming gridUpdatePanel is the UpdatePanel with the grid
for more information about the UpdateMode property look here:
http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.updatemode.aspx
I was trying to update the content of a modal dialog, and this code works for me:
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
<asp:UpdatePanel ID="upNewUpdatePanel" runat="server">
<ContentTemplate>
<asp:Label ID="updateLabel" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="updateSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
However, when I try to place the LinkButton inside a gridview, like so:
<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
<asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
<asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
</asp:TemplateField>
</Columns>
</asp:GridView>
This does not work, I get an error saying: A control with ID 'updateSomething' could not be found for the trigger in UpdatePanel 'upNewUpdatePanel'.
How can I use the ImageButton inside the gridview?
Try and add the asp:AsyncPostBackTrigger to the asp:GridView's OnRowCommand event and handle the link button click in that event
<asp:GridView ID="grdListUsers" runat="server" onRowCommand="grdListUsers_RowCommand">
<asp:TemplateField>
<asp:LinkButton ID="updateSomething" CommandName="update-something" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</asp:TemplateField>
</asp:GridView>
and in the cs create the event like this
protected void grdListUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "update-something")
{
grdListUsers.SelectedIndex = Convert.ToInt32(e.CommandArgument);
}
}
You could add a trigger of the gridview
<Triggers>
<asp:PostBackTrigger ControlID="gridview1" />
</Triggers>
Add another Update Panel surrounding your link button just like below.
<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
<asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
<asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:UpdatePanel ID="aa" runat="server">
<ContentTemplate>
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="updateSomething"/>
</Triggers>
</asp:UpdatePanel>
</asp:TemplateField>
</Columns>
</asp:GridView>
You could set the UpdatePanel's UpdateMode to Conditional and update it manually from the UpdateButton_Click-Handler:
<asp:UpdatePanel ID="UdpFormPanel" runat="server" UpdateMode="conditional" ChildrenAsTriggers="false" >
LinkButton's Click-Event handler:
Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As EventArgs)
'blah....
upNewUpdatePanel.Update()
End Sub
I have an asp.net gridview that is originally bound to a sqldatasource control, but when the user presses an external button, it instead gets the contents of a datatable rather than a SQLdatasource control. I therefore had to write code in the PageIndexChanging event of the gridview to allow for paging. My code is as follows:
Protected Sub gvEvents_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvEvents.PageIndexChanging
gvEvents.PageIndex = e.NewPageIndex
gvEvents.DataBind()
This worked beautifully until I added an AJAX update panel so the whole page wouldn't postback every time it paged, and paging stopped working. I debugged it and discovered that it is actually calling the PageIndexChanging event, but nothing is happening.
I searched the web and found a few people with the same problem, but their solutions did not work for me. There was one on this site whose problem was solved by the following:
In PageIndexchanging event, where you bind data to grid, make sure, data is again fetched from the DB
I don't know what that means; my data was being bound as demonstrated above. I have "enable paging" set to true and EnableSortingAndPagingCallbacks set to false.
I would really appreciate if someone can help me. I am including my markup for the updatepanel below. Thank you so much!
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ibtnSearch" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="gvEvents" runat="server" DataKeyNames = "intID"
AutoGenerateColumns="False" AllowPaging="True" GridLines="None" CellPadding="10"
ForeColor="#333333" PageSize="6" DataSourceID="defaultDS" >
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<!-- put code block inside label? To set the formatter to include year if
it's next year? -->
<asp:Label ID="Label1" runat="server"
Text = '<%# RepeatingMethods.DetermineOngoing(CType(Eval("dtmEventStartDate"), DateTime) , CType(Eval("dtmEventEndDate"), DateTime))%>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("chvAgeRange") %>'> </asp:Label> <br />
<asp:Label ID="Label3" runat="server" Text= '<%# Eval("chvState") %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="intId"
DataNavigateUrlFormatString="EventDetail.aspx?intId={0}"
DataTextField="chvEventName" />
<asp:BoundField DataField="chvBriefDescription" HeaderText="Description"
SortExpression="chvBriefDescription" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White"></FooterStyle>
<PagerStyle HorizontalAlign="Center" BackColor="#FFCC66" ForeColor="#333333"></PagerStyle>
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy"></SelectedRowStyle>
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White"></HeaderStyle>
<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
</asp:GridView>
<asp:Label ID="lblError" runat="server"></asp:Label>
<br />
</ContentTemplate>
</asp:UpdatePanel>
In PageIndexchanging event, where you bind data to grid, make sure, data is again fetched from the DB
I don't know what that means; my data was being bound as demonstrated above.
It means that you need to fetch your data again in your code behind page. You are using a SQLdatasource in your design/html page, so you need to remove that and use a SQL Connection, SQL Command, etc. to fetch your data and then set that as your control's datasource.
Something like below:
http://www.aspnettutorials.com/tutorials/database/db-grid-aspnet2-vb.aspx
Your code should look something like this
Protected Sub Page_Load(...)
gvEvents.PageIndex = 0
LoadData();// loads initial data
end sub
private sub LoadData()
'' do your SQL Conn and Command here
'' set your datasource of gridview here
end sub
Protected Sub gvEvents_PageIndexChanging(...) Handles gvEvents.PageIndexChanging
gvEvents.PageIndex = e.NewPageIndex
LoadData()
gvEvents.DataBind()
end sub
For anyone who stumbles across this issue, I encountered it this AM and this post was misleading (atleast for my scenario). For me, I simply had to add a PageIndexChanging Event as a trigger for my datagrid for the updatepanel trigger's, and it resolved my issue.
Controls that Are Not Compatible with UpdatePanel Controls
The following ASP.NET controls are not compatible with partial-page updates, and are therefore not supported inside an UpdatePanel control:
GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.
http://www.asp.net/Ajax/Documentation/Live/overview/UpdatePanelOverview.aspx
Just update the AJAX Panel after databind().
assume id of the Update Panel is AJAXPanel
Protected Sub gvEvents_PageIndexChanging(...) Handles gvEvents.PageIndexChanging
gvEvents.PageIndex = e.NewPageIndex
LoadData()
gvEvents.DataBind()
// The below line refreshes the update panel..
AJAXPanel.Update()
end sub