Updatepanel inside usercontrol ascx file not updating gridview - c#

I'm trying to be slick and I'm putting this user control in a div that covers the whole page.
However when I click on the search button I get a full postback.
Any ideas on how to get it to not postback and just update the gridview?
And just to be clear. The following control is in a ascx file. Which is called in a aspx page.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="PropertyViewAddEntity.ascx.cs"
Inherits="UserControls_PropertyViewAddEntity" %>
<p>Search for State City County zip</p>
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button2ae">
<p>
<asp:TextBox ID="TextBox1ae" runat="server" Width="500px"></asp:TextBox>
<asp:Button ID="Button2ae" runat="server" Text="Search" CausesValidation="false" />
</p>
</asp:Panel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView2ae" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3ae"
OnSelectedIndexChanged="GridView2ae_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="zipcodeid" HeaderText="zipcodeid" SortExpression="zipcodeid" />
<asp:BoundField DataField="zip_code" HeaderText="zip_code" SortExpression="zip_code" />
<asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
<asp:BoundField DataField="city_alias_name" HeaderText="city_alias_name" SortExpression="city_alias_name" />
<asp:BoundField DataField="county_name" HeaderText="county_name" SortExpression="county_name" />
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2ae" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
btw the control is in a placeholder
<asp:PlaceHolder ID="ControlContainer" runat="server"/>
and called from the code behind.
Control mycontrol = LoadControl("~/UserControls/PropertyViewAddEntity.ascx");
ControlContainer.Controls.Add(mycontrol);

I have finally figured out the issue.
In order for things to work properly adding the user control via codebehind will not work!
To get things working properly you must declare the usercontrol on the page so that you can access the UpdateMode="Conditional" property of the usercontrol.
<%# Register TagPrefix="my" TagName="AddEntity" Src="~/UserControls/PropertyViewAddEntity.ascx" %>
<my:AddEntity Visible="false" ID="test1" runat="server" ClientIDMode="Inherit" UpdateMode="Conditional" />
I set the Visible property of the control to false so that I can show it via button click.
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
And in the code behind I did this.
protected void Button1_Click(object sender, EventArgs e)
{
test1.Visible = true;
}
Once the updatemode is set. Everything works as it should.

Related

Why LinkButton inside GridView which is present in Updatepanel not firing OnClientClick event?

I have placed my GridView inside the Updatepanel and there I have defined few columns with one LinkButton. But for that LinkButton OnClientClick event is not firing. Instead it's doing a postback.
Following is the Code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvUnmappedICD" runat="server" EmptyDataText="No Records are added yet."
OnRowCommand="gvUnmappedICD_RowCommand" OnRowDataBound="gvUnmappedICD_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%#Eval("KEYWORD") %>' CommandName="remove" ClientIDMode="AutoID"
OnDataBinding="lnkRemove_DataBinding" OnClientClick='return confirm("Are you sure you want to Delete this?");' ToolTip="Click to Remove this record." Text="Remove" />
</ItemTemplate>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
This LinkButton with the ID lnkRemove should display a confirm message box when user clicks on it. But it's not showing it.I have tried registering the Asynchronous PostBack event to this from code behind as follows:
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbRemove);
Kindly anyone help me to resolve this.
Use a PostBackTrigger
<asp:ScriptManager ID="scriptManager" runat="server">
<asp:UpdatePanel ID="updatePanel" runat="server">
<asp:GridView ID="gvUnmappedICD" runat="server" EmptyDataText="No Records are added yet."
OnRowCommand="gvUnmappedICD_RowCommand" OnRowDataBound="gvUnmappedICD_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%#Eval("KEYWORD") %>' CommandName="remove" ClientIDMode="AutoID"
OnDataBinding="lnkRemove_DataBinding" OnClientClick='return confirm("Are you sure you want to Delete this?");' ToolTip="Click to Remove this record." Text="Remove" />
</ItemTemplate>
</Columns>
</asp:GridView>
<Triggers>
<asp:PostBackTrigger ControlID="lnkRemove" />
</Triggers>
</asp:UpdatePanel>
Please use this on link button's OnClientClick
OnClientClick='return confirm("Are you sure you want to Delete this?");return false;'

reload on button click in

Here is my aspx:
<asp:Panel ID="pnl_updateClinicVisit" runat="server" CssClass="modalPopupClinicVisitEntry2" DefaultButton="bt_editClinicVisit_submit" Style="display:none">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button AutoPostBack="false" UseSubmitBehavior="false" ID="AddMedicationChange" ClientIdMode="Static" runat="server" Text="Add Med Change" OnClick="AddMedicationChange_Click" />
<asp:Panel ID="AddNewMedicationPanel" runat="server">
<asp:TextBox ID="NewDrugName" OnTextChanged="NewDrugName_TextChanged" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="NewDrugNameAutoCompleteExtender"
runat="server"
TargetControlID="NewDrugName"
MinimumPrefixLength="1"
EnableCaching="false"
CompletionSetCount="1"
CompletionInterval="500"
ServiceMethod="GetDrugs">
</asp:AutoCompleteExtender>
<asp:DropDownList OnSelectedIndexChanged="NewDrugChange_SelectedIndexChanged" ID="NewDrugChange" runat="server">
<asp:ListItem>Drug +</asp:ListItem>
<asp:ListItem>Drug -</asp:ListItem>
<asp:ListItem>Dose ↑</asp:ListItem>
<asp:ListItem>Dose ↓</asp:ListItem>
</asp:DropDownList>
<asp:Button AutoPostBack="false" UseSubmitBehavior="false" ID="SubmitMedChange" runat="server" Text="Add to Visit" OnClick="SubmitMedicationChange_Click" />
</asp:Panel>
<asp:ModalPopupExtender ID="updateClinicModalPopupExtender" runat="server" TargetControlID="bt_editClinicVisit_dummy"
PopupControlID="pnl_updateClinicVisit" CancelControlID="bt_editClinicVisit_cancel"
DropShadow="true" BackgroundCssClass="modalBackground" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
For some reason my page is reloading when I click the "AddnewMdication" and "SubmitMedChange" buttons. When I have the AutoPostBack=false UseSubmitBehavior=false, the events fire and then the page reloads. If I don't have these attributes then the page reloads before the events even fire. How do I get AJAX functionality within this modal?
Try setting your Buttons as an AsyncPostBackTrigger. You can do this by adding in the Triggers section and declaring the UpdatePanel as a conditional update.
For example
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="AddMedicationChange" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="SubmitMedChange" EventName="Click" />
</Triggers>
<ContentTemplate>
<%-- Your code here --%>
</ContentTemplate>
</asp:UpdatePanel>
What this should do is to explicitly declare that those two buttons as being Async.
If this doesn't work, will you please post the relevant code to your two button OnClick events? AddMedicationChange_Click and SubmitMedicationChange_Click
If you try to interact with controls outside of the UpdatePanel in your code-behind during an asyncpostback, odd things can start to happen.

How can I add UpdatePanels to my gridview in asp.net

I have a gridview with some textboxes and buttons inside the gridview. I want to use updatepanels when the buttons are clicked so that only one textbox is refreshed instead of the whole page. I tried to add my ScriptManager and UpdatePanel inside my gridview but I get errors saying the tags aren't recognized.
I've looked elsewhere on SO for solutions and others have said to place the whole gridview in an UpdatePanel. However, I don't want the whole grid to be updated (it's pretty lengthy). Is it possible to have individual UpdatePanels in my gridview?
EDIT:
Here is some code to go along with my question:
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="PCalendar" EventName="SelectedChange" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="QS">
<ItemTemplate>
<asp:TextBox ID="TextBox" runat="server" Text=' <%#Bind("PDate") %>' OnTextChanged="SetTextBox" AutoPostBack="True"></asp:TextBox>
<asp:ImageButton ID="PButton" runat="server" OnClick="ShowCalendar" ImageUrl="../images/calendarIcon.jpg" />
<asp:Calendar ID="PCalendar" runat="server" Visible="false" OnSelectionChanged="SetDate" ></asp:Calendar>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Here is what it's going to sorta look like. Once you post your code, it will be easier to see how close you are and how to help you properly. But this is the general idea.
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="buttonOneID" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="buttonTwoID" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
<Columns>
//columns code here
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

ASP.NET PostBack is not hitting the correct method

Consider the following ASP.NET code:
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:MultiView runat="server" ID="MultiView" ActiveViewIndex="0">
<asp:View runat="server">
</asp:View>
<asp:View runat="server">
<p><img alt="Loading..." src="/global/images/ajax-mini-loader.gif" style="vertical-align: middle;" /> Loading...</p>
</asp:View>
<asp:View runat="server">
<asp:GridView runat="server" ID="WarrantyView" OnDataBound="WarrantyView_DataBound" AutoGenerateColumns="false" ItemType="WarrantySystem.Data.ServiceCompany">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Telephone" DataField="Telephone" />
<asp:BoundField HeaderText="Email" DataField="Email" />
<asp:BoundField HeaderText="Telephone 24/7" DataField="Telephone247" />
<asp:BoundField HeaderText="Email 24/7" DataField="Email247" />
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Button runat="server" ID="btnEdit" CommandName="Edit" CommandArgument="<%# Item.ID %>" Text="Edit" />
<asp:Button runat="server" ID="btnDelete" CommandName="Delete" CommandArgument="<%# Item.ID %>" Text="Delete" OnCommand="btnDelete_Command"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:View>
<asp:View runat="server">
<p>Your data could no be loaded at this time.</p>
</asp:View>
</asp:MultiView>
<asp:Timer runat="server" ID="tmrLoadData" Enabled="true" Interval="1" OnTick="tmrLoadData_Tick" />
</ContentTemplate>
</asp:UpdatePanel>
When btnDelete is clicked, a postback event occurs to Page_Load, and never hits btnDelete_Command, as it should. I'm lost as to why!
EDIT: The same problem occurs with the edit button...Okay so in the example, there is no OnClick or OnCommand event, but I have just tested this...it does the same thing.
How it works:
The page loads and shows an empty view.
The timer ticks and begins loading data, and shows the data loading view.
Once the data loads, the data view shows.
When clicking edit, the page should redorect to the edit page.
When clicking delete, the page should just delete the entry.
EDIT: As per user comments (server-side code command code):
protected void btnDelete_Command(object sender, CommandEventArgs e)
{
bool result = this.mgr.DeleteServiceCompany(Int32.Parse(e.CommandArgument.ToString()));
}
protected void btnEdit_Command(object sender, CommandEventArgs e)
{
Response.Redirect("ServiceCompany.aspx?id=" + e.CommandArgument.ToString());
}
Use the RowCommand event of GridView to fire your Edit and Delete command.
Add this inside your GridView markup.
OnRowCommand="WarrantyView_RowCommand"
Also change the CommandName text
CommandName="Modify" // change the name here
CommandName="Remove" // change the name here
And inside the event
protected void WarrantyView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Modify")
{
// your edit logic
}
if (e.CommandName == "Remove")
{
// your delete logic
}
}
I did recommend you to change the name of the CommandName property.
Change Edit to perhaps Modify
Change Delete to perhaps Remove
Reason being they are inbuilt gridview commands
NOTE: Ensure that your ViewState is enabled in all relevant places too. This will not work with ViewState disabled.

Refresh an updatepanel on buttonclick from code-behind

I'm using a Gridview that is using datasource & databinding. When i reload the page the gridview is updated but I want it to be on the buttonclick, but it's not working for me.
The gridview inside the updatepanel:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upWall" runat="server" ChildrenAsTriggers="true" UpdateMode="conditional">
<ContentTemplate>
<asp:GridView ID="gvWallPosts" runat="server" AutoGenerateColumns = "false"
CaptionAlign="NotSet" CellPadding="5">
<Columns>
<asp:TemplateField HeaderText="Avsändare">
<ItemTemplate>
<%# GetSender((int)Eval("WallSender"))%>
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Inlägg">
<ItemTemplate>
<%# Eval("Post")%>
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="txtWall" runat="server" Height="105px" TextMode="MultiLine" Width="227px"></asp:TextBox>
<br />
<asp:Button ID="btnWall" runat="server" Text="Posta" onclick="btnWall_Click" />
Code-behind:
protected void btnWall_Click(object sender, EventArgs e)
{
con.SendWallPost(con.GetId(Membership.GetUser().UserName), Convert.ToInt32(Request.QueryString["ID"]), txtWall.Text); //This method is sending the post
upWall.Update();
}
So, I want the updatepanel to be updated on the ButtonClick, I don't want to reload the whole page to see the result
Since i don't see the button btnWall, i assume that it's outside of the UpdatePanel.
But you need to define an explicit trigger if you want to allow a control outside of an UpdatePanel to trigger a postback.
Therefore you can use an AsyncPostBackTrigger:
<asp:UpdatePanel ID="upWall" runat="server" ChildrenAsTriggers="true" UpdateMode="conditional">
<ContentTemplate>
....
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnWall" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
By default, partial-page updates are enabled in an update panel because the default value of the EnablePartialRendering property of the ScriptManager control is true.Putting the button in the update panel is suffice to give you what you need since the button acts as an asynchronus postback control inside the panel.Then just add this line( gvWallospts.Databind()) after your update.Let me know how it goes.
protected void btnWall_Click(object sender, EventArgs e)
{
con.SendWallPost(con.GetId(Membership.GetUser().UserName), Convert.ToInt32(Request.QueryString["ID"]), txtWall.Text); //This method is sending the post
//upWall.Update();
gvWallPosts.DataBind();
}
Try setting up you markup like this
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upWall" runat="server" ChildrenAsTriggers="true" UpdateMode="conditional">
<ContentTemplate>
<asp:GridView ID="gvWallPosts" runat="server" AutoGenerateColumns = "false"
CaptionAlign="NotSet" CellPadding="5">
<Columns>
<asp:Templatefield>
<asp:Button ID="btnWall" runat="server" Text="Posta" command="Edit" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Avsändare">
<ItemTemplate>
<%# GetSender((int)Eval("WallSender"))%>
<br />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text='<%# Bind("WallSender")%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Inlägg">
<ItemTemplate>
<%# Eval("Post")%>
<br />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text='<%# Bind("Post")%>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
In your grid Row updating event
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
con.SendWallPost(con.GetId(Membership.GetUser().UserName), Convert.ToInt32(Request.QueryString["ID"]), txtWall.Text);
gvWallPosts.DataBind();
}
Make sure that also you Binding code in page load is sandwiched by this
If(!IsPostBack)
{
}
You should either put the button inside the update panel or define an explicit trigger to update the update panel on button click event as suggested by Tim Schmelter.

Categories