Refresh only content using UpdatePanel control - asp.net

Is it possible to use UpdatePanel control with navigating through the left panel (menu)?
In Main.master there are left menu
<dx:ASPxNavBar ID="ASPxNavBar1" runat="server" AutoCollapse="True" EnableAnimation="True" Width="100%">
<dx:NavBarGroup Text="Group">
<Items>
<dx:NavBarItem NavigateUrl="Content/Content1.aspx" Text="Content1"></dx:NavBarItem>
<dx:NavBarItem NavigateUrl="Content/Content2.aspx" Text="Data Subject"></dx:NavBarItem>
</Items>
</dx:NavBarGroup>
</dx:ASPxNavBar>
and I'm trying to use refresh only content using UpdatePanel control
<div class="contentPane">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" updatemode="conditional">
<ContentTemplate>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
but it doesn't work; header and menu still reloading when navigating through the menu.
Content1.aspx and Content2.aspx files have just an usual content inside of <asp:content> tag
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" KeyFieldName="PK" Width="750px">
<Columns>
<dx:GridViewCommandColumn ShowNewButtonInHeader="True" VisibleIndex="0" ShowDeleteButton="True" ShowEditButton="True">
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn FieldName="PK" ReadOnly="True" Visible="false" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>
</asp:Content>

I wonder why you are using an UpdatePanel is that a requeriment?
I would use an ASPxCallbackPanel which is a great replacement for the UpdatePanel and you can use it since you are using DevExpress controls
<script type="text/javascript">
var postponedCallbackRequired = false;
function OnListBoxIndexChanged(s, e) {
if(CallbackPanel.InCallback())
postponedCallbackRequired = true;
else
CallbackPanel.PerformCallback();
}
function OnEndCallback(s, e) {
if(postponedCallbackRequired) {
CallbackPanel.PerformCallback();
postponedCallbackRequired = false;
}
}
</script>
<dx:ASPxListBox runat="server" Height="221px" Width="100%" TextField="Name" DataSourceID="EmployeesDataSource1"
ValueField="EmployeeID" ID="ASPxListBox1" ClientInstanceName="ListBox" BackColor="Transparent">
<Border BorderWidth="0px"></Border>
<ItemStyle>
<Border BorderWidth="0px"></Border>
</ItemStyle>
<ClientSideEvents SelectedIndexChanged="OnListBoxIndexChanged" />
</dx:ASPxListBox>
<dx:ASPxCallbackPanel runat="server" ID="ASPxCallbackPanel1" Height="250px" ClientInstanceName="CallbackPanel" RenderMode="Table">
<ClientSideEvents EndCallback="OnEndCallback"></ClientSideEvents>
<PanelCollection>
<dx:PanelContent ID="PanelContent3" runat="server">
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>
You can see a complete sample code here

Related

checkbox inside a gridview within updatepanel

I have a gridview with a checkbox in first column. I set checkbox property of autopostback="true". The gridview is inside the updatepanel. When checkbox is checked i want to make one panel as visible, which panel is outside the update panel. I check the code with check point, which is go through the code, but its not working. Can any one help me?..
Thanks in advance.
My code is here for your reference...
HTML Code:
<asp:Panel ID="ploperation" runat="server" CssClass="plop" Visible="False">
<asp:LinkButton ID="lbtnasspam" runat="server" CssClass="panelbtn" Font-Names="Calibri"
Font-Size="14px" Font-Underline="False" OnClick="lbtnasspam_Click">Report As Spam</asp:LinkButton>
</asp:Panel>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:GridView ID="gvmail" runat="server" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkchild" runat="server" AutoPostBack="true" OnCheckedChanged="chkchild_CheckedChanged"/>
</ItemTemplate>
<ItemStyle Width="15px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
C# Code:
protected void chkchild_CheckedChanged(object sender, EventArgs e)
{
ploperation.Visible = true;
}
You can use initializeRequest - this event is raised when an asynchronous post back occurs(When you check the checkbox in your gridview it initiates an async postback because it's inside an update panel).
Just change ploperation to a div instead of <asp:Panel and you can use javascript to show/hide it:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(initializeRequest);
function initializeRequest(sender, args) {
document.getElementById('ploperation').style.display = 'block';
}
</script>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:GridView ID="gvmail" runat="server" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkchild" runat="server" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle Width="15px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<div id="ploperation" style="display:none;" runat="server">
<asp:LinkButton ID="lbtnasspam" runat="server" CssClass="panelbtn" Font-Names="Calibri"
Font-Size="14px" Font-Underline="False" OnClick="lbtnasspam_Click">Report As Spam</asp:LinkButton>
</div>
</form>
Alternatively you can stick to the way you've done it, just place <asp:Panel inside UpdatePanel -> ContentTemplate
update panel does partial page loading....
check link
so if you want to show your panel than include it also in your update panel.
Put the UpdatePanel on the control you want to change, not on the GridView. Do not forget to Reference the GridView on the Triggers section. For example, if you want to change a label text use the following code:
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvmail" />
</Triggers>
</asp:UpdatePanel>
<asp:GridView ID="gvmail" runat="server" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkchild" runat="server" AutoPostBack="true" OnCheckedChanged="chkchild_CheckedChanged"/>
</ItemTemplate>
<ItemStyle Width="15px" />
</asp:TemplateField>
</Columns>
</asp:GridView>

RadGrid inside RadWindow having issues in grid sorting

In my aspx page I have a RadPageView which contains RadWindow and the RadGrid is inside the RadWindow. (ie. RadMultiPage -> RadPageView -> RadWindow -> RadGrid).
<telerik:RadWindow EnableShadow="true" ShowContentDuringLoad="false" runat="server"
ReloadOnShow="true" Title="Standard Text: Add Observation." OpenerElementID="lnkObservationsText"
Behaviors="None" VisibleStatusbar="false" EnableViewState="true" ID="rdWndObservationText"
Skin="Web20" Modal="true" Width="600">
<ContentTemplate>
<div class="RadModalMainDiv">
<div>
<p>
Help text to go here....</p>
</div>
<div class="divStandardTextButtonList">
<asp:Button ID="btnObservationsTextSelect" runat="server" Text="Select" CssClass="btnStandardText"
OnClientClick="return ObservationStandardText_Confirm()" />
<asp:Button ID="btnObservationsTextCancel" runat="server" Text="Cancel" CssClass="btnStandardText" />
</div>
<asp:Panel ID="pnl1" runat="server">
<div>
<telerik:RadGrid ID="radGdObservationsText" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" GridLines="None" PageSize="10" Width="100%">
<MasterTableView CommandItemDisplay="None" Name="ParentGrid">
<Columns>
<telerik:GridClientSelectColumn UniqueName="ClientSelectColumn">
</telerik:GridClientSelectColumn>
<telerik:GridBoundColumn DataField="description" HeaderText="Observation Description"
Visible="true">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
<PagerStyle Mode="NextPrevAndNumeric" />
<ClientSettings>
<Selecting AllowRowSelect="True" />
<ClientEvents OnRowSelected="SetObservationStandardText" />
</ClientSettings>
</telerik:RadGrid>
</div>
</asp:Panel>
</div>
</ContentTemplate>
</telerik:RadWindow>
When I try to sort the grid clicking on the header, the page refreshes and the RadWindow disappears. Sorting happens coz when i reopens the RadGrid the rows appea sorted. I need the RadGrid to be sroted and remain in the same state.
Thanks for all helps in prior.
You have to reopen the RadWindow manually in order for this to work. I can suggest that you try an easier approach. Remove the OpenerElementID and modify the button that open the RadWindow and the other one that closes it as shown below:
Click handler for the button that open/close the window:
void lnkObservationsText_Click(object sender, EventArgs e)
{
rdWndObservationText.VisibleOnPageLoad = !rdWndObservationText.VisibleOnPageLoad;
}
Click handler code for the close button that resides in the RadWindow itself
void btnObservationsTextCancel_Click(object sender, EventArgs e)
{
rdWndObservationText.VisibleOnPageLoad = false;
}
In this case you won't need to use ReloadOnShow.
Hope this helps. Good luck :)

DataBind GridView OnKeyUp/Down

I have a gridview that will display customer information. I want it to update the results as the user types first and/or last name into the textboxes. Basically, to databind the gridview to it's datasource with the updated characters from the textboxes as the user types.
CODE EDITED:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Process.aspx.cs" Inherits="Reservations.WebForm3" %>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>Processing...</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="firstname" runat ="server" class="inputLarge" ontextchanged="firstname_TextChanged" />
<asp:TextBox ID="lastname" runat="server" class="inputLarge" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="457px">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="CustID" HeaderText="Cust ID">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="FirstName" HeaderText="First Name">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="Last Name">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="City" HeaderText="City">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts></Scripts>
</asp:ScriptManager>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="firstname" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
protected void firstname_TextChanged(object sender, EventArgs e)
{
InventoryAppSoapClient inst = new InventoryAppSoapClient();
DataSet ds = inst.getCustomer(firstname.Text, "none");
GridView1.DataSource = ds;
GridView1.DataBind();
}
Everything works except the TextChanged event only first the first time. If I go back and change the text and tab off, nothing happens.
Have you tried adding an event handler for the OnTextChanged event of your text box.
Inside you could force the databind like so
Private void firstName_OnTextChanged(object sender, EventArgs e)
{
//Your datasource and parameters are already defined on the front end so there is no
//setup required
existing.DataBind();
}
Same goes for your text box lastName
Private void lastName_OnTextChanged(object sender, EventArgs e)
{
//Your datasource and parameters are already defined on the front end so there is no
//setup required
existing.DataBind();
}
And don't forget to add the methods to the OnTextChange attribute for each textbox in the front end. Like so.
<asp:TextBox ID="firstname" OnTextChanged="firstName_OnTextChanged" runat ="server" class="inputLarge"/>
<asp:TextBox ID="lastname" OnTextChanged="lastName_OnTextChanged" runat="server" class="inputLarge" />
Also, if you embed this code in an update Panel and use the AJAX control toolkit (free) the grid will update seamlessly. Otherwise it won't be pretty if you use classic asp techniques. The page will keep posting back every time you press a key. It will get annoying.

Can't find GridView in ASP

I am brand new to asp and downloaded some example code of a shopping cart. When I created a new project in Visual Studio, added all the files in and tried to compile I got the following error.
Error 1 The name 'gvShoppingCart' does not exist in the current context c:\users\slaphappysmoker\documents\visual studio 2010\Projects\Shopcart\Shopcart\ViewCart.aspx.cs 21 3 Shopcart
However I can see where gvShoppingCart gets defined in the ViewCart.aspx file.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ViewCart.aspx.cs" Inherits="ViewCart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Shopping Cart</title>
<link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h1>Shopping Cart</h1>
< Back to Products
<br /><br />
<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." GridLines="None" Width="100%" CellPadding="5" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand">
<HeaderStyle HorizontalAlign="Left" BackColor="#3D7169" ForeColor="#FFFFFF" />
<FooterStyle HorizontalAlign="Right" BackColor="#6C6B66" ForeColor="#FFFFFF" />
<AlternatingRowStyle BackColor="#F8F8F8" />
<Columns>
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />
<asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>' style="font-size:12px;"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UnitPrice" HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
<asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
</Columns>
</asp:GridView>
<br />
<asp:Button runat="server" ID="btnUpdateCart" Text="Update Cart" OnClick="btnUpdateCart_Click" />
</div>
</form>
</body>
</html>
Here is the code that is calling the GridView in the ViewCart.aspx.cs file:
protected void BindData() {
// Let's give the data to the GridView and let it work!
// The GridView will take our cart items one by one and use the properties
// that we declared as column names (DataFields)
gvShoppingCart.DataSource = ShoppingCart.Instance.Items;
gvShoppingCart.DataBind();
}
How can I get the ViewCart.aspx.cs code file to recognize the ViewCart.aspx and the GridView contained within?
Edit: Added the function that calls the gridview.
Try to make sure that the gridview has a reference in the designer.cs file
protected global :: System.Web.UI.WebControls.GridView gvShoppingCart;

PopUpExtender on ImageButton inside GridView problem

I have GridView on my asp.net page, one column in that grid is ImageButton (TemplateField with ID="imbReserve"). On click on that button I want to show PopUp, but when I put TargetControlId="imbReserve" I get error message " A control with ID 'imbReserve' could not be found". How to achieve this, on click on button inside Grid show PopUp ?
Have a look at these 2 articles, that just helped me out with this problem
Article 1: A More Traditional approach
The Following is paraphrased from the above article
The Page Code:
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
Width="95%">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:Button ID="btnViewDetails" runat="server" Text="Details" OnClick="BtnViewDetails_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="customerid" HeaderText="ID" />
<asp:BoundField DataField="companyname" HeaderText="Company" />
<asp:BoundField DataField="contactname" HeaderText="Name" />
<asp:BoundField DataField="contacttitle" HeaderText="Title" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<ajaxToolKit:ModalPopupExtender
ID="mdlPopup" runat="server" TargetControlID="pnlPopup" PopupControlID="pnlPopup"
CancelControlID="btnClose" BackgroundCssClass="modalBackground" />
<asp:Panel ID="pnlPopup" runat="server" Width="500px" style="display:none">
<asp:UpdatePanel ID="updPnlCustomerDetail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
[Your Content Here]
</ContentTemplate>
</asp:UpdatePanel>
<div align="right" style="width:95%">
<asp:Button
ID="btnSave" runat="server" Text="Save" />
<asp:Button ID="btnClose" runat="server" Text="Close" Width="50px" />
</div>
</asp:Panel>
Note that the GridView is wrapped in an update panel also that the Target Control ID for the Modal Extender is the same as the Pop Up Control ID. This is because the ModalPopUp Extender needs a target control ID and I think this solution is a better plan than using a hidden button.
Now For the Code Behind:
protected void BtnViewDetails_Click(object sender, EventArgs e)
{
// Do Anything you need to the contents of the update panel
// update the contents in the detail panel
this.updPnlCustomerDetail.Update();
// show the modal popup
this.mdlPopup.Show();
}
Article 2:Uses Clientside Javascript
The Following is paraphrased from the above article
The client side Javascript
<script type="text/javascript">
// keeps track of the delete button for the row
// that is going to be removed
var _source;
// keep track of the popup div
var _popup;
function showConfirm(source){
this._source = source;
this._popup = $find('mdlPopup');
// find the confirm ModalPopup and show it
this._popup.show();
}
function okClick(){
// find the confirm ModalPopup and hide it
this._popup.hide();
// use the cached button as the postback source
__doPostBack(this._source.name, '');
}
function cancelClick(){
// find the confirm ModalPopup and hide it
this._popup.hide();
// clear the event source
this._source = null;
this._popup = null;
}
Page Code:
<asp:GridView ID="gvToDoList" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Item" HeaderText="Description" />
<asp:BoundField DataField="IsCompleted" HeaderText="Complete?" />
<asp:TemplateField ControlStyle-Width="50px" HeaderStyle-Width="60px" >
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server"
OnClientClick="showConfirm(this); return false;"
OnClick="BtnDelete_Click" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<ajaxToolKit:ModalPopupExtender BehaviorID="mdlPopup" runat="server"
TargetControlID="div" PopupControlID="div"
OkControlID="btnOk" OnOkScript="okClick();"
CancelControlID="btnNo" OnCancelScript="cancelClick();"
BackgroundCssClass="modalBackground" />
<div id="div" runat="server" align="center" class="confirm" style="display:none">
<img align="absmiddle" src="Img/warning.jpg" />Are you sure you want to delete this item?
<asp:Button ID="btnOk" runat="server" Text="Yes" Width="50px" />
<asp:Button ID="btnNo" runat="server" Text="No" Width="50px" />
</div>
Again note that the Target Control ID for the Modal Pop Up Extender is the same as the Pop Up Control ID. Also note the OnClientClick attribute on the button in the template field, make sure your include "return false;".
All that is needed in the code behind it the onClick (or onCommand) event handler to do what you need to do.
I've tried both of these approaches successfully.
Hope one of these two works for you.
The problem is related to the fact that the real ID is getting changed when the page get rendered to the client.
Open your page source in the browser and look at the ID that is generated from asp.net. Then use that ID inside the TargetControlID property
This kind of problem is present with all the templated controls in ASP.NET
A cleaner way will be to bind the TargetControlID property of the ModalPopupExtendr on Page Load where you can use the ClientID property dinamically generated
modalPopupExtender.TargetControlID = myTemplateControl.ClientID;

Resources