ListView inside an UpdatePanel not getting rebound - c#

I have a ASP.NET ListView inside an UpdatePanel.
In the ItemTemplate there are 2 buttons with Commands "UpdateCart" & "RemoveCart".
In both the commands, the datasource of ListView (an SQL DataSource) gets rebound.
There is no InsertCommand/UpdateCommand/DeleteCommand on the SqlDataSource
My problem is :
Whenver these buttons are clicked : the datasource does not get refreshed.
however I can see from debug mode that values are indeed passed to the datasource selecting method correctly.
The relevant ASPX Page :
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" DisplayAfter="10" runat="server" AssociatedUpdatePanelID="UpnlMain">
<ProgressTemplate>
<div class="divWaiting">
<asp:Label ID="lblWait" runat="server" Text=" Please wait... " />
<asp:Image ID="imgWait" runat="server" ImageAlign="Middle" ImageUrl="~/Image/AjaxLoading.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpnlMain" runat="server">
<ContentTemplate>
<asp:ListView ID="lvMain" runat="server" DataSourceID="sdsMain" DataKeyNames="Code"
OnItemCommand="lvMain_ItemCommand">
<EmptyDataTemplate>
<div class="col-xs-12">
<span>No Items in Cart.</span>
</div>
</EmptyDataTemplate>
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("Code") %>' Visible="false" />
Product " <asp:Literal ID="ltrStyleNo" runat="server" Text='<%# Eval("StyleNo") %>' />
Qty : <asp:TextBox ID="txtQty" runat="server" type="number" CssClass="txtQty OnlyNumeric form-control text-center input-sm"
Text='<%# Eval("Qty") %>'></asp:TextBox>
<asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="~/image/update.png" CssClass="btn btnUpdateCart"
CommandName="UpdateCart" AlternateText="Update Qty" />
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/image/delet.png" CssClass="btn"
CommandName="DeleteCart" AlternateText="Remove Item" />
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource ID="sdsMain" runat="server" ConnectionString="<%$ ConnectionStrings:CurrentConnectionString %>"
SelectCommand="pGetItemsForCart" SelectCommandType="StoredProcedure" OnSelecting="sdsMain_Selecting"
UpdateCommand="NoUpdate" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter DefaultValue="-1" Name="WoSCodeCSV" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:Parameter DefaultValue="-1" Name="WoSCodeCSV" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The Code Behind Code :
protected void sdsMain_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters[0].Value = GetCartCodes(); //gives some values in csv from Session ..
}
protected void lvMain_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "UpdateCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
TextBox txtQty = e.Item.FindControl("txtQty") as TextBox;
//Some update in a Session
BindListView();
}
else if (e.CommandName.ToLower() == "DeleteCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
//Some update in a Session
BindListView();
}
else if (e.CommandName.ToLower() == "RefreshCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
BL.StyleSearchHelper.PriceItem(Page, lblCode.Text);
BindListView();
}
}
void BindListView()
{
sdsMain.DataBind();
lvMain.DataBind();
}

Try replace your buttons outside the updatepanel. Page will not getting refresh when the buttons are in updatepanel. sorry i can't make comment so I post here as an answer.

Related

How to correctly get cell value in gridview after clicking on a linkbutton inside a gridview and enable correct async postback to display a modal

I am making a reservation system and currently stuck on the following situation.
Basically I have a GridView which is loaded with the reservation ID and two LinkButton which opens a modal popup to enable the customer to view the details.
What I want to achieve is that when I click any LinkButton, it will show the ID from the first cell of a GridViewRow.
I have tried these methods here, here, here, and here but it still doesn't work. Is there any way to do it, Aside from using a SELECT Command and does involve async postback or if async postback is not possible with this, any fix recommended?
Here is my GridView code:
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvReservationSummaries"/>
</Triggers>
<ContentTemplate>
<h2>Reservations</h2>
<br />
<asp:GridView ID="gvReservationSummaries" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
AllowPaging="True" ShowHeader="False" CellPadding="5" CellSpacing="5"
onrowcommand="Gridview1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"><%# Eval("Master_Reservation_ID")%></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="ViewReservationDetails" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'>View Reservation Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="ViewReceiptDetails" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'>View Receipt Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DB_9E4ABD_GratchiOBRSConnectionString2 %>"
SelectCommand="SELECT [Master_Reservation_ID] FROM [Master_Reservation] WHERE User_Unique_ID = #uuid">
<SelectParameters>
<asp:SessionParameter Name="uuid" SessionField="uid" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:Button ID="btnShowModal" runat="server" Text="Show Modal 1 (Debug Purposes Only)" onclick="btnShowModal_Click" />
<!--Here, put the details of the reservaton, plus options to edit reservation.-->
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
<asp:Panel ID="reservationSummaryModal" runat="server" Width="500px" Height="500px" style="display: none; background-color:Black; color:#CCC;">
<asp:Button ID="btnExitRs" runat="server" Text="Hide Modal (Debug Purposes Only)" onclick="btnExitAct_Click" />
<asp:Label ID="lblReservationSummary" runat="server" Text="Label"></asp:Label>
</asp:Panel>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="HiddenField1" PopupControlID="reservationSummaryModal" BackgroundCssClass="modalBg" CancelControlID="btnExitRs">
</asp:ModalPopupExtender>
<asp:HiddenField ID="HiddenField1" runat="server" />
And here is my codebehind:
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewReservationDetails")
{
//GridViewRow gvr = gvReservationSummaries.Rows[Convert.ToInt32(e.CommandArgument)];
gvReservationSummaries.SelectedIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow gvr = gvReservationSummaries.Rows[gvReservationSummaries.SelectedIndex];
lblReservationSummary.Text = "The id is: " + gvr.Cells[0].Text.ToString();
ModalPopupExtender1.Show();
}
else if (e.CommandName == "ViewReceiptDetails")
{
gvReservationSummaries.SelectedIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow gvr = gvReservationSummaries.Rows[gvReservationSummaries.SelectedIndex];
lblReservationSummary.Text = "The id is: " + gvr.Cells[0].Text.ToString();
ModalPopupExtender1.Show();
}
else
{
}
}

Get values from dropdown menu's, run query and add values to formview c#

I have a asp.net webform which contains a formView, 3 dropdown menus and a submit button. The dropdown menus get their values from the database.
When the user clicks the submit button values from the dropdown menus should be run through our query and display the outcome in the formView. This is not happening.
When we give standard values for other, meat and vegetables in the callSelectProduct() we can see the correct output in the form view, but this is on page load.
This is the click method from the submit button:
protected void getRecipe(object sender, EventArgs e)
{
string ddlOther = DropDownOther.SelectedValue;
string ddlVegetables = DropDownVegetables.SelectedValue;
string ddlMeat = DropDownMeat.SelectedValue;
int ddlIntOther = int.Parse(ddlOther);
int ddlIntVegetables = int.Parse(ddlVegetables);
int ddlIntMeat = int.Parse(ddlMeat);
Business.Class1.callSelectProduct(ddlIntOther, ddlIntMeat, ddlIntVegetables);
}
This is callSelectProduct():
The Debug.WriteLine gives the good values back in the debug console, but then the page reloads because of the submit button click and then the Debug.WriteLine gives 0 0 0 back. I think that's why I don't see anything in the FormView. Because the combination of 0 0 0 will not return anything.
public static void callSelectProduct(int other, int meat, int vegetables)
{
SelectProduct(other, meat, vegetables);
}
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
public static Data.SouthWind.SelectRecipesFromIngredientsDataTable SelectProduct(int otherGet, int meatGet, int vegetablesGet)
{
int other = otherGet;
int meat = meatGet;
int vegetables = vegetablesGet;
System.Diagnostics.Debug.WriteLine("This is class 1 Other " + other + " Vegetable " + vegetables + " Meat " + meat);
DataAccess.SouthWindTableAdapters.SelectRecipesFromIngredientsTableAdapter tableAdaptertest = new DataAccess.SouthWindTableAdapters.SelectRecipesFromIngredientsTableAdapter();
return tableAdaptertest.GetData(other, meat, vegetables);
}
This is our webform:
<form id="form1" runat="server">
<div>
<asp:FormView ID="RecipeFormView" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1">
<EditItemTemplate>
RecipeName:
<asp:TextBox ID="RecipeNameTextBox" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
RecipeName:
<asp:TextBox ID="RecipeNameTextBox" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RecipeName:
<asp:Label ID="RecipeNameLabel" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectProduct" TypeName="Business.Class1">
<SelectParameters>
<asp:Parameter Name="otherGet" Type="Int32" />
<asp:Parameter Name="meatGet" Type="Int32" />
<asp:Parameter Name="vegetablesGet" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="DropDownOther" runat="server" DataSourceID="ObjectDataSource2" DataTextField="IngredientName" DataValueField="IngredientId">
</asp:DropDownList>
<asp:DropDownList ID="DropDownVegetables" runat="server" DataSourceID="SelectVegetables" DataTextField="IngredientName" DataValueField="IngredientId" Height="16px">
</asp:DropDownList>
<asp:DropDownList ID="DropDownMeat" runat="server" DataSourceID="SelectMeat" DataTextField="IngredientName" DataValueField="IngredientId">
</asp:DropDownList>
<asp:ObjectDataSource ID="SelectMeat" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectMeat" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="SelectVegetables" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectVegetables" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectOther" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:Button ID="Button1" runat="server" OnClick="getRecipe" Text="Button" UseSubmitBehavior="False" />
<br />
</div>
</form>
Any help is welcome!
Are you use !IsPostBack in outer of you dropdown binding method in inside of pageload event ?
for
void page-load()
{
if(!IsPostBack)
{
//Call all dropdownlist binding method
}
}

Get Index Item from a ListView control

ASPX
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Themes] WHERE ([Theme] = #Theme) ORDER BY [Price]">
<SelectParameters>
<asp:Parameter DefaultValue="Dubai-and-Beyond" Name="Theme" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ListView ID="theme5" runat="server" DataSourceID="SqlDataSource5">
<LayoutTemplate>
<div id="itemPlaceHolderContainer" runat="server">
<span id="itemPlaceHolder" runat="server"></span>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<asp:Image ID="Destinationimage" runat="server"
ImageUrl='<%# Eval("ID", "~/CMS/ThemesHandler.ashx?ID={0}")+"&img=1"%>'
AlternateText="Destination_Image" Height="140px" Width="179px" />
<asp:Label ID="lblcountry" runat="server" Text='<%#Eval("Country") %>' />
</div>
<div class="hotel_name">
<asp:Label ID="lblcountry" runat="server" Text='<%#Eval("Country") %>' /></div>
<asp:ImageButton ID="imgbtn5" runat="server" ImageUrl="images/book_nw.png"
OnClick="imgbtn5_Click" AlternateText="get_quote"/></div>
</ItemTemplate>
</asp:ListView>
Code behind
protected void imgbtn5_Click(object sender, EventArgs e)
{
ListViewItem item = theme5.Items[0];
Label country = (Label)item.FindControl("lblcountry");
string con = country.Text.ToString();
Session["country"] = con.ToString();
Response.Redirect("Get_Quote.aspx");
}
Here, Theme5 is Listview. There is a Label and imgbutton in EACH listview item.
I want to transfer label value on imgbutton click event.
Problem here is i am not able to identify Row index of listview item.
You can make use of CommandName and CommandArgument of your ImageButton within your ListView. You may than access the according item from within your ItemCommand event.
<asp:ImageButton ID="imgbtn5" runat="server" ImageUrl="images/book_nw.png"
OnClick="imgbtn5_Click" AlternateText="get_quote"
CommandName="YOUR_COMMAND_NAME"
CommandArgument='<%#Eval("ANY_COLUMN_OF_SOURCE") %>' />
And within your codeBehind file
protected void theme5_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "YOUR_COMMAND_NAME"))
{
string arg = e.CommandArgument; // do whatever you want
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
}
}

Two updatePanels and popup extender which should not disappear after postback

On my MasterPage I have 2 update panels which are surrounded with Panels. Two of them contain 'Details View' controls and some buttons.
On the other hand, I have one UpdatePanel which contains image buttons and link buttons.
The idea is that I'm fetching from the database the messages ( 2 kinds), showing them on the Page. When the user clicks on a button (LinkButton or ImageButton), he or she sees a 'Popup Control'. On the popup control, he or she can see the message details and if needed, cancel them or approve.
Here is where I am stuck. If I remove the ImageButtons from the UpdatePanels, I won't be able to refresh them without a full postback.
Should I have 'popup extensions' in the UpdatePanel with the ImageButtons, but then when I click on the button from 'popup panel' - it disappears ( there is no full postback, it just disappears) - it should just change the DetailsView page.
How do I make it work?
Thanks in advance !
(I need this solution because I want to use a timer to refresh LinkButtons )
here is my code behind :
protected void Page_Load(object sender, EventArgs e)
{
try
{
//here im pulling data from database and binding it with 'details view' controls, its not big deal so i think i don't have to show it?
wyswietl_powiadomienia_o_wydarzeniach();
wyswietl_ilosc_zaproszen_do_przyjaciol();
wyswietl_ilosc_nieodczytanych_wiadomosci();
}
catch (Exception)
{
}
}
protected void ButtonWczesniej_Click(object sender, EventArgs e)
{
DetailsViewEventsRequests.PageIndex = DetailsViewEventsRequests.PageIndex - 1;
ButtonDalej.Enabled = true;
wyswietl_powiadomienia_o_wydarzeniach();
}
protected void ButtonDalej_Click(object sender, EventArgs e)
{
//
DetailsViewEventsRequests.PageIndex = DetailsViewEventsRequests.PageIndex + 1;
ButtonWczesniej.Enabled = true;
wyswietl_powiadomienia_o_wydarzeniach();
}
protected void ButtonInvLeft_Click(object sender, EventArgs e)
{
DetailsViewIfFriends.PageIndex = DetailsViewIfFriends.PageIndex - 1;
}
protected void ButtonInvRight_Click(object sender, EventArgs e)
{
DetailsViewIfFriends.PageIndex = DetailsViewIfFriends.PageIndex + 1;
}
And my aspx: (only one updatepanel with detail's view because 2nd one is very simillar)
<div id="NotifyAreaWhite">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="NotifyAreaDiv">
<div id="NotifyDivMail">
<div id="NotifyLeftMSG"><asp:ImageButton ID="ImageButtonNotifyMsg" runat="server"
ImageUrl="~/images/msg.png" PostBackUrl="~/wiadomosci.aspx"
ToolTip="Wyslij wiadomosc" /></div>
<div class="NotifyRight"> <asp:LinkButton ID="LabelNotifyMsgNo" runat="server" Text="0" Font-Size="Large" PostBackUrl="~/wiadomosci.aspx"/></div>
</div>
<div class="NotifyDiv">
<div id="NotifyLeftFrend" class="NotifyLeft"> <asp:ImageButton ID="ImageButtonNotifyFrends" runat="server"
ImageUrl="~/images/friends.png"
ToolTip="Zaproszenia od znajomych." /></div>
<div id="NotifyRightFrend" class="NotifyRight"><asp:LinkButton ID="LabelNotifyFrendsNo" runat="server" Text="0" Font-Size="Large"/></div>
</div>
<div class="NotifyDiv">
<div id="NotifyLeftWyd" class="NotifyLeft"> <asp:ImageButton ID="ImageButtonWydarzenia" runat="server" ImageUrl="~/images/event.png" ToolTip="Zaproszenia do wydarzen." /></div>
<div id="NotifyRightWyd" class="NotifyRight"> <asp:LinkButton ID="LabelNotifyEventsNo" runat="server" Text="0" Font-Size="Large"/></div>
</div>
</div>
<asp:ModalPopupExtender ID="PanelZaproszeniaEventy_ModalPopupExtender"
runat="server" Enabled="true" OkControlID="ButtonZamknijOkno" CancelControlID="ButtonZamknijOkno"
TargetControlID="ImageButtonWydarzenia" PopupControlID="PanelZaproszeniaEventy"
BackgroundCssClass="NotifyPageTloClass"/> //extender showing Panel
<asp:ModalPopupExtender ID="PanelZaproszeniaEventy_ModalPopupExtenderCyfra"
runat="server" Enabled="true" OkControlID="ButtonZamknijOkno" CancelControlID="ButtonZamknijOkno"
TargetControlID="LabelNotifyEventsNo" PopupControlID="PanelZaproszeniaEventy"
BackgroundCssClass="NotifyPageTloClass"/>
<asp:ModalPopupExtender ID="PanelProsbyOznajomosc_ModalPopupExtender"
runat="server" Enabled="true" OkControlID="ButtonFrendCloseNotifier" CancelControlID="ButtonFrendCloseNotifier"
TargetControlID="ImageButtonNotifyFrends" PopupControlID="PanelProsbyOznajomosc"
BackgroundCssClass="NotifyPageTloClass"/>
<asp:ModalPopupExtender ID="PanelProsbyOznajomosc_ModalPopupExtenderCyfra"
runat="server" Enabled="true" OkControlID="ButtonFrendCloseNotifier" CancelControlID="ButtonFrendCloseNotifier"
TargetControlID="LabelNotifyFrendsNo" PopupControlID="PanelProsbyOznajomosc"
BackgroundCssClass="NotifyPageTloClass"/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:Panel ID="PanelZaproszeniaEventy" runat="server" Width="318px" CssClass="NotifyWydTlo" >
<asp:UpdatePanel ID="UpdatePanelZaproszeniaEventy" runat="server" RenderMode="Block">
<ContentTemplate>
<asp:DetailsView ID="DetailsViewEventsRequests" runat="server" DataKeyNames="Charakterystyka" AutoGenerateRows="False" Height="17px" Width="313px" HorizontalAlign="Center" onitemcreated="DetailsViewEventsRequests_ItemCreated">
<Fields>
<asp:BoundField DataField="UserName" HeaderText="Zalozyciel" SortExpression="Nazwisko" />
<asp:BoundField DataField="Miasto" HeaderText="Gdzie?" SortExpression="Miasto" />
<asp:BoundField DataField="Data_ZalozeniaWydarzenia" HeaderText="Data wyslania" SortExpression="Miasto" />
</Fields>
<FooterTemplate>
</FooterTemplate>
<HeaderTemplate>
<div>
<div style="float:left;">
<asp:Label ID="LabelNazwaWydarzenia" runat="server"
Text='<%# Eval("Nazwa_Wydarzenia") %>'></asp:Label>
</div>
<div style="float:right; margin-left:5px;">
<asp:Button ID="ButtonZobacz" runat="server" CssClass="myButton" Text="Zobacz Wydarzenie" Font-Size="X-Small" Width="150px" ClientIDMode="AutoID" OnClick="ButtonZobacz_click" UseSubmitBehavior="True"/>
</div>
</div>
</HeaderTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px; color:#FF5041; margin-left:auto; margin-right:auto;">
<tr>
<td>Nie masz zadnych zaproszen.</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:DetailsView>
<table style="margin-left:auto; margin-right:auto;">
<tr>
<td>
<asp:Button ID="ButtonWczesniej" Width="69px" Height="41px" runat="server" Text="<<" OnClick="ButtonWczesniej_Click" CssClass="myButton" /></td> //button previous msg
<td>
<asp:Button ID="ButtonDalej" Width="69px" Height="41px" runat="server" Text=">>" ///button next msg
OnClick="ButtonDalej_Click" CssClass="myButton"/></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<div style="margin-left:auto; margin-right:auto; width: 112px;">
<asp:Button CssClass="myButton" ID="ButtonZamknijOkno" Width="100%" //button closing popup
Height="41px" runat="server" Text="ZAMKNIJ"/>
</div>
</asp:Panel>
I hope i described it well. Sorry For my weak english and some Polish words in code :)
As i should do at the begining i made this example on empty page and try something else.. mode="Conditional" did the trick.
I should give that code before, not that long one :)
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:HyperLink ID="HyperLinkPanelOn" runat="server">click here to show popup</asp:HyperLink>
<asp:ModalPopupExtender ID="PanelZaproszeniaEventy_ModalPopupExtenderCyfra"
runat="server" Enabled="true" OkControlID="ButtonClose" CancelControlID="ButtonClose"
TargetControlID="HyperLinkPanelOn" PopupControlID="Panel1" BackgroundCssClass="NotifyPageTloClass" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Panel ID="Panel1" runat="server">
<asp:UpdatePanel ID="UpdatePanelPopUp" runat="server">
<ContentTemplate>
<asp:Button runat="server" Text="postback" />
<asp:Button runat="server" Text="postback" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="ButtonClose" runat="server" Text="Zamknij" />
</asp:Panel>
So simple.. I thought it's something bigger :/

TextBoxes in UpdatePanel are causing PageRequestManagerServerErrorException

I'm trying to create a page that has some information in databound labels with and edit button. When the edit button is clicked, the information is replaced with TextBoxes bound to the same data. The data can then be modified, saved back to the DB and the TextBoxes replaced with updated Labels.
To start with, and to keep things simple, all I have is an UpdatePanel with a DataList and two buttons: EditButton and CancelButton (CancelButton is hidden by default). The DataList's ItemTemplate has two Panels: ViewPanel and EditPanel (EditPanel is hidden by default). When EditButton is clicked, I hide EditButton and the DataList's Items' ViewPanel, and show CancelButton and the DataList's Items' EditPanel.
Not a problem. Once this is done however, the CancelButton button will not work, throwing a PageRequestManagerServerErrorException.
Through some fiddling, I worked out this happens because there are databound text boxes on the EditPanel. If I don't bind data to the text boxes, everything works perfectly. Why doesn't this work?
Here's my code:
UpdatePanelTest.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="UpdatePanelTest.aspx.cs" Inherits="WebLetterViewer.UpdatePanelTest" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:SqlDataSource ID="AllLettersDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ORMSTestConnectionString %>"
SelectCommand="SELECT * FROM [Letters] WHERE ([id] = #id)">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenLetterID" DefaultValue="1" Name="id" PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:HiddenField ID="HiddenLetterID" runat="server" Value="1" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:DataList ID="LettersDataList" runat="server" DataSourceID="AllLettersDataSource">
<ItemTemplate>
<asp:Panel ID="ViewPanel" runat="server">
<h2>Data1:</h2>
<asp:Label ID="data1Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data1") %>' Width="500px" />
<h2>data2:</h2>
<asp:Label ID="data2Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data2") %>' Width="500px" />
<h2>data3:</h2>
<asp:Label ID="data3Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data3") %>' Width="500px" />
</asp:Panel>
<asp:Panel ID="EditPanel" runat="server" Visible="False">
<h2>data1:</h2>
<asp:TextBox ID="data1TextBox" runat="server" Height="100px" Text='<%# Eval("data1", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
<h2>data2:</h2>
<asp:TextBox ID="data2TextBox" runat="server" Height="100px" Text='<%# Eval("data2", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
<h2>data3:</h2>
<asp:TextBox ID="data3TextBox" runat="server" Height="100px" Text='<%# Eval("data3", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="EditButton" runat="server" onclick="EditButton_Click" Text="Edit" />
<asp:Button ID="CancelButton" runat="server" onclick="CancelButton_Click" Text="Cancel" Visible="False" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
UpdatePanelTest.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebLetterViewer{
public partial class UpdatePanelTest : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}
protected void EditButton_Click(object sender, EventArgs e){
foreach (DataListItem item in LettersDataList.Items){
item.FindControl("ViewPanel").Visible = false;
item.FindControl("EditPanel").Visible = true;
}
EditButton.Visible = false;
CancelButton.Visible = true;
UpdatePanel1.Update();
}
protected void CancelButton_Click(object sender, EventArgs e){
foreach (DataListItem item in LettersDataList.Items){
item.FindControl("ViewPanel").Visible = true;
item.FindControl("EditPanel").Visible = false;
}
EditButton.Visible = true;
CancelButton.Visible = false;
UpdatePanel1.Update();
}
}
}
Put your EditPanel in a EditItemTemplate and use Commands , you are not using this control the way it was designed to be used:
How to: Allow Users to Edit Items in DataList Web Server Controls
Markup:
<asp:SqlDataSource ID="AllLettersDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORMSTestConnectionString %>"
SelectCommand="SELECT * FROM [Letters] WHERE ([id] = #id)">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenLetterID" DefaultValue="1" Name="id" PropertyName="Value"
Type="Int32" />
</SelectParameters>
<!--change this -->
UpdateCommand="UPDATE [Categories] SET [CategoryName] = #CategoryName, [Description]
= #Description WHERE [CategoryID] = #original_CategoryID">
<UpdateParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="original_CategoryID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:DataList ID="LettersDataList" runat="server" DataSourceID="AllLettersDataSource"
OnEditCommand="LettersDataList_EditCommand" OnCancelCommand="LettersDataList_CancelCommand"
OnUpdateCommand="LettersDataList_UpdateCommand">
<ItemTemplate>
<asp:Panel ID="ViewPanel" runat="server">
<h2>
Data1:</h2>
<asp:Label ID="data1Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
Text='<%# Eval("data1") %>' Width="500px" />
<h2>
data2:</h2>
<asp:Label ID="data2Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
Text='<%# Eval("data2") %>' Width="500px" />
<h2>
data3:</h2>
<asp:Label ID="data3Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
Text='<%# Eval("data3") %>' Width="500px" />
</asp:Panel>
<asp:Button ID="EditButton" runat="server" CommandName="edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Panel ID="EditPanel" runat="server">
<h2>
data1:</h2>
<asp:TextBox ID="data1TextBox" runat="server" Height="100px" Text='<%# Eval("data1", "{0}") %>'
TextMode="MultiLine" Width="500px"></asp:TextBox>
<h2>
data2:</h2>
<asp:TextBox ID="data2TextBox" runat="server" Height="100px" Text='<%# Eval("data2", "{0}") %>'
TextMode="MultiLine" Width="500px"></asp:TextBox>
<h2>
data3:</h2>
<asp:TextBox ID="data3TextBox" runat="server" Height="100px" Text='<%# Eval("data3", "{0}") %>'
TextMode="MultiLine" Width="500px"></asp:TextBox>
</asp:Panel>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="update">
Save
</asp:LinkButton>
<asp:Button ID="CancelButton" runat="server" CommandName="cancel" Text="Cancel" Visible="False" />
</EditItemTemplate>
</asp:DataList>
Code-behind:
protected void LettersDataList_EditCommand(object source, DataListCommandEventArgs e)
{
LettersDataList.EditItemIndex = e.Item.ItemIndex;
LettersDataList.DataBind();
}
protected void LettersDataList_CancelCommand(object source,
DataListCommandEventArgs e)
{
LettersDataList.EditItemIndex = -1;
LettersDataList.DataBind();
}
protected void LettersDataList_UpdateCommand(object source,
DataListCommandEventArgs e)
{
//change this to your database needs
//String categoryID =
// LettersDataList.DataKeys[e.Item.ItemIndex].ToString();
//String categoryName =
// ((TextBox)e.Item.FindControl("textCategoryName")).Text;
//String description =
// ((TextBox)e.Item.FindControl("textDescription")).Text;
//SqlDataSource1.UpdateParameters["original_CategoryID"].DefaultValue
// = categoryID;
//SqlDataSource1.UpdateParameters["categoryName"].DefaultValue
// = categoryName;
//SqlDataSource1.UpdateParameters["Description"].DefaultValue
// = description;
//SqlDataSource1.Update();
LettersDataList.EditItemIndex = -1;
LettersDataList.DataBind();
}

Categories