GridView and CheckBoxList with UpdatePanel in asp.net (C#) - c#

I have an CheckBoxList within UpdatePanel which has onselectedindex change event on this event i have to update my gridview by fetching data from database. Event perform well but GridView not showing updated data may be because of GridView not in updatepanel.
Actually my problem is data not updated in gridview after firing onselectedindexchanged of Checkboxlist, and i cannot put gridview inside the updatepanel(its mandatory for me).
onselectedindexchanged event working fine i am already checked it out. So how can i update my gridview data.
Here is my code
<asp:UpdatePanel ID="MonthUpdatePanel" runat="server">
<ContentTemplate>
<asp:CheckBoxList ID="MonthCheckBoxList" runat="server" AutoPostBack="True"
onselectedindexchanged="MonthCheckBoxList_SelectedIndexChanged"
Width="195px" onclick="updateviewmore()" >
<asp:ListItem Value="1">1 Month</asp:ListItem>
<asp:ListItem Value="2">2 Month</asp:ListItem>
<asp:ListItem Value="6">6 Month</asp:ListItem>
<asp:ListItem Value="12">1 Year</asp:ListItem>
</asp:CheckBoxList>
</ContentTemplate>
</asp:UpdatePanel>
<div>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" Width="100%"
GridLines="None" onrowdatabound="GridView2_RowDataBound"
ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="pad5">
<asp:Label ID="BookImageName" Text='<% #"../BookImages/"+ Eval("BookImage") %>' CssClass="BookImage" runat="server" Height="180px" Width="135px" />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>

Unfortunately, you can't do that. Not if the GridView is outside the UpdatePanel. Your options are:
Remove the UpdatePanel and use full postbacks
Add a PostbackTrigger to the UpdatePanel and specify the CheckboxList as triger element...this is similar to the above...it'll always cause a full postback
Put the GridView inside the UpdatePanel and everything will be done in an async postback
Move the CheckboxList outside of the UpdatePanel, then put the GridView inside the UpdatePanel and add an AsyncPostbackTrigger for the CheckboxList so the UpdatePanel gets update asynchronously when and item is checked
The bottom line is, if you want to re-bind the GridView asynchronously then you'll have to move it inside the UpdatePanel or else you'll have to go with full postback approach
A Hardcore Solution
You can still do everything manually with the help of the jQuery plugin, a UserControl and an http handler that you could create to serve the necessary html. That is, intercept the html requests from client-side, make an async request to the handler passing some parameters so that the handler knows how to tell the UserControl how to bind the GridView...of course, the GridView will be inside a UserControl
The handler can write the output of a UserControl to the response using Execute method of the HttpServerUtility class instance which is available in the current HttpContext. For example...
public class YourHandler: IHttpHandler {
public void ProcessRequest(HttpContext ctx){
StringWriter writer = new StringWriter();
ctx.Server.Execute("~/UserControl.ascx", output, false);
ctx.Response.Write(writer);
}
}
This method effective works as if the UserControl was passing through all the ASP.NET pipeline cycles during a normal request and its context will be exactly the same context of the http handler...so, if you're using built-in asp.net membership features make sure to use a generic handler (.ashx) which plugs into the ASP.NET pipeline by itself with no further effort from your side

Related

Linked Button inside grid view causing full post back when using update panel with grid view

There is a button outside update panel, the click event of the button binds grid view.
Grid view is inside the update panel and there is a link button inside the grid view.
Link button is causing full post back, i have looked online and tried different things but no luck so far.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<table>
<tr>
<td>
<asp:UpdatePanel ID="ContentUpdatePanel" runat="server">
<ContentTemplate>
<asp:GridView ID="gvMasterData" runat="server" AutoGenerateColumns="false" Width="200px" class="display">
<Columns>
<asp:TemplateField HeaderText="registration date" ItemStyle-Width="50">
<ItemTemplate>
<asp:Label ID="lblRegistrationDate" runat="server" Text='<%# Eval("registration_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="completed" ItemStyle-Width="50">
<ItemTemplate>
<asp:LinkButton Text='<%# Eval("completed") %>' ForeColor="Black" Font-Underline="false" runat="server" CommandName="Completed" CommandArgument="<%# Container.DataItemIndex %>" /> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="pending" ItemStyle-Width="50">
<ItemTemplate>
<asp:LinkButton ID="MarkAsCompleteButton" Text='<%# Eval("pending") %>' ForeColor="Black" Font-Underline="false" runat="server" CommandName="Pending" CommandArgument="<%# Container.DataItemIndex %>" /> </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</table>
Code Behind
private void gvMasterData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = e.Row.FindControl("MarkAsCompleteButton") as LinkButton;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lb);
}
}
Try to see this
Full postback triggered by LinkButton inside GridView inside UpdatePanel
When you use a update panel then the standard web page life cycle DOES occur. This is what we call a partial post back.
So the forms on-load event and near all of the standard web forms events WILL fire.
The only difference is that in these partial post backs, then the code stubs (code behind) can only modify controls inside of the panel - not controls outside. The code in the panel can certainly grab/use/look at and consume controls outside of the panel, but the values will be somewhat "static" or more clear such values will be the same when the full page was rendered since the last full page post back.
So are you confused or surprised that you seeing a post back? you will and you do!!
So perhaps you are seeing a post back (you will and do). However that post-back is not a full page post back. Any button or event that fires inside of the update panel will cause a post-back - but it is what we call a partial post back.
As such, then most of the web page standard events do fire - including the on-load event. As noted, the only difference here is that these partial post backs only sends up to the server the updated content inside of that up-date panel (not the whole page).
So you do of course in all cases will see events like on-load fire. And when the code behind stubs inside of that panel run, then as a normal course of action you can only deal with and modify controls inside of that panel with the code behind. If that code modifies controls outside of the panel, then in the classic standard "round trip" that occurs here DOES NOT include those controls outside of the update panel. So you can't and will not see the changes reflected in controls outside of the panel in that code behind that runs.
But a good old fashioned round trip DOES occur when using a up-date panel, it just that only controls and things inside of the panel make this classic round trip - not the whole page.
So update panels do and will cause post-backs, but they are considered partial ones.
So of course a update panel DOES cause a post-back - but it is only a partial post back. All of the classic round trip operations occur here - same as a full page post back, but it is limited to the update panel - not the whole page.
So it not clear if you "wondering" why you see a post back (you will and you do).
Or if you are actually seeing a whole page post back. So ZERO surprise that a post-back is being triggered here (that does occur). The only question then is the post back you are seeing is a full page (should not occur), or you just seeing a page post back that is the partial one?
As noted, you will see a post back for events run inside of the panel. And thus as a normal operation things like page on-load does and will fire when using a update panel.
In other words, the whole standard page round trip for a update panel code and trigger DOES occur. This is a standard classic round trip that occurs here. As noted, the only difference is that such round trips only include the content inside of the panel, and thus other controls on the page should not be touched nor re-freshed.

How to use the .show() of Ajax ModalPopUpExtender when ModalPopupExtender is within a gridview?

So I am using the ModalPopupExtender control from the Ajax control toolkit. It's my understanding that when I want to set the targetControlID to a button that's within a Gridview, that I need to put my ModalPopupExtender within the template that holds the button. In my case:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteWidget" runat="server" Text="Delete" CommandName="DeleteWidget" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="btnAddNewWidget" runat="server" CssClass="buttonStyle" Text="Add New Widget" onclick="btnAddNewWidget_Click"/>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
BackgroundCssClass="modalBackground"
DropShadow="true"
OkControlID="btnSaveWidget"
CancelControlID="aCloseX"
PopupControlID="Panel1"
TargetControlID="btnAddNewWidget" />
</FooterTemplate>
</asp:TemplateField>
Now, this is working well, the modal popup appears when the button is pressed and all is good. However, ModalPopUpExtender has the nasty habbit of closing the modal on postback. My pop up contains some DropDownLists that will have to do postbacks (it needs to execute some relevant code). Whenever this happens the modal closes.
A(n ugly) work around to this is using the show() function. For example, whenever a dropdownlist does a postback, the function ends with ModalPopupExtender1.Show();.
The problem:
ModalPopupExtender1 does not exist in my current context, because I defined it within the template field, so that it would work. How do I fix this? I wish to refer to my the ModalPopupExtender1 and use the .show() function to handle the postback issue.
Search for the nested modal popup extender like this.
VB.NET
Dim ModalPopupExtender1 = CType(YourGridView.FooterRow.Findcontrol("ModalPopupExtender1"), AjaxControlToolkit.ModalPopupExtender)
ModalPopupExtender1.Show()
C#
AjaxControlToolkit.ModalPopupExtender ModalPopupExtender1 = (AjaxControlToolkit.ModalPopupExtender)YourGridView.FooterRow.Findcontrol("ModalPopupExtender1");
ModalPopupExtender1.Show();
This will expose the buried control which is hidden in the item template.Let me know if this works.
You can also put modalpopup out side the grid control.
it works fine if its out side the grid.
and also you don't need to use findcontrol you can directly write Modalpopup.show() method to show modalpopup again.
try this this will definitely works.

Is it normal for Page_Load to trigger upon clicking a page of a GridView inside an UpdatePanel?

I have this code in my aspx page:
<form id="form2" runat="server">
<asp:ScriptManager ID="ItemsScriptManager" runat="server" EnablePartialRendering="true" />
<asp:Button runat="server" ID="SearchButton" OnClick="ItemsSearch" Text="Search" />
<asp:UpdatePanel runat="server" ID="ItemsUpdatePanel">
<ContentTemplate>
<asp:ObjectDataSource runat="server" ID="ItemsDS"
TypeName="TemplateGridViewODSPagingSorting.ItemDAO" SelectMethod="GetItems" />
<asp:GridView runat="server" ID="ItemsGridView" DataSourceID="ItemsDS"
AllowPaging="true" AllowSorting="true" PageSize="4">
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
By pressing on another page of the GridView the Page_Load is triggered, is this normal behavior for a partial postback?
Partial rendering using UpdatePanel does not change or affect the whole Page life cycle in ASP.NET.
it's a small trick used to re-render only a certain region of the page in the browser (the UpdatePanel) but nothing else change, so yes, it's normal to see Page_Load and all other events to be triggered as usual; it has to be like that or it would not work :)
Yes, the during update panel update, the page_load will be called with every asynchronous postback to the server, to overcome this, you can use jquery ajax.

AJAX UpdatePanel help needed

I have the following ASPX code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" ID="UpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button runat="server" ID="UpdateButton1" OnClick="NextImg_Click" Text="Update" />
<asp:Repeater runat="server" ID="urlsUpdateRepeater">
<ItemTemplate>
<!-- THIS WOULD BE A LOOP FROM HERE -->
<!-- OPENS RESULT ITEM DIV CONTAINER -->
<div id="result_item">
<a href="<%# Eval("myUrl") %>" target="_blank">
<%# Eval("urlPageTitle")%></a>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
I have a NextImg_Click() event which works fine.
I use this code for DataBind... what is the Update method?
urlsUpdateRepeater.DataSource = resultsCollection;
urlsUpdateRepeater.DataBind();
Everything would appear to be in order. But every time the Update button is clicked it re-renders the whole page instead of the partial-postback UpdatePanel only.
It is driving me completely mad as I can't see anything wrong with the code. Is there some simple thing I'm missing?! Please help!
The search and data is displayed correctly (inside the panel) it just will not do a partial postback.
Appreciate your help with my noob problems!
Because the button is inside your UpdatePanel's ContentTemplate, it is unnecessary to take any extra action to get a partial page postback.
Try removing the line from your Page_Load() method.
Taken from MSDN:
Use the RegisterAsyncPostBackControl
method to register controls outside an
UpdatePanel control as triggers for
asynchronous postbacks, and to
potentially update the content of an
update panel. To update an UpdatePanel
control programmatically, call the
Update method.
So, you're control (UpdateButton1) is inside the UpdatePanel, no need for the ScriptManager1.RegisterAsyncPostBackControl call - ditch it and your problem is solved.
The problem was that my <form> tag was nested further into the document than it's corresponding end tag (with Warning!)...
Upon moving my form tag - it worked!
Entirely my fault, thanks guys.

ScriptManager.RegisterClientScript in a UserControl within a FormView inside an Async Panel

I'm having an annoying problem registering a javascript event from inside a user control within a formview in an Async panel. I go to my formview, and press a button to switch into insert mode. This doesn't do a full page postback. Within insert mode, my user control's page_load event should then register a javascript event using ScriptManager.RegisterStartupScript:
ScriptManager.RegisterStartupScript(base.Page, this.GetType(), ("dialogJavascript" + this.ID), "alert(\"Registered\");", true);
However when I look at my HTML source, the event isn't there. Hence the alert box is never shown. This is the setup of my actual aspx file:
<igmisc:WebAsyncRefreshPanel ID="WebAsyncRefreshPanel1" runat="server">
<asp:FormView ID="FormView1" runat="server" DataSourceID="odsCurrentIncident">
<EditItemTemplate>
<uc1:SearchSEDUsers ID="SearchSEDUsers1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
Hello
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Button" />
</ItemTemplate>
</asp:FormView>
</igmisc:WebAsyncRefreshPanel>
Does anyone have any idea what I might be missing here?
Have you tried using RegisterClientSideScript? You can always check the key for the script with IsClientSideScriptRegistered to ensure you don't register it multiple times.
I'm assuming the async panel is doing a partial page past back which doesn't trigger the mechansim to regenerate the startup scripts. Perhaps someone with a better understanding of the ASP.Net Page Life Cycle and the CLR can fill in those blanks.
For me that works fine. resizeChartMid() is a function name.
ScriptManager.RegisterStartupScript(this, typeof(string), "getchart48", "resizeChartMid();", true);
Try this, i got the same issue
ScriptManager.RegisterClientScriptBlock(MyBase.Page, Me.[GetType](),
("dialogJavascript" + this.ID), "alert(\"Registered\");", True)
This worked for me!

Categories