i have a page which is inside update panel.it contains two gridview which are also inside update panels.i m binding them in page load inside !ispostback as well as other parts of code.when i bind only first grid it works fine but when i bind the second grid it refreshes the first one and it shows no data.is this the problem of improper usage of update panel usage or any other issue.. how can i use the triggers here.
my code is as follows..
<asp:UpdatePanel ID="updgrd1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" Width="85%"ShowHeaderWhenEmpty="true"EmptyDataText="No Records Found" AutoGenerateColumns="false"OnRowCancelingEdit="GridView1_RowCancelingEdit"OnRowEditing="G dView1_RowEditing">
<Columns>
//my code here
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpdate" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="updgrd2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" Width="85%" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false" EmptyDataText="No Records Found">
<Columns>
//my code here
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpdate" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
LoadGrid1();
LoadGrid2();
}
}
Here I'm doing exactly what you are. Here I have a textbox and a gridview, when clicking add whatever is in the textbox gets added to the gridview.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger controlid="LnkAddTrack" eventname="Click" />
</Triggers>
<ContentTemplate>
<div id="EventTrack" >
<asp:Label ID="lblEventTracks" runat="server" Text="Event Tracks"></asp:Label>
<asp:TextBox ID="txtEventTracks" CssClass="EventTextbox" runat="server"></asp:TextBox>
<asp:LinkButton ID="LnkAddTrack" ClientIDMode="Static" runat="server" OnClick="LnkAddTrack_Click">Add Track</asp:LinkButton>
</div>
<asp:GridView ID="dgTracks" runat="server" >
<Columns>
<asp:TemplateField HeaderText="TrackName">
<ItemTemplate>
<asp:Label ID="Control" runat="server" Text='<%# Eval("TrackName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Then make sure you on your button event to add new data you are adding the data correctly
and remember to rebind the gridview.
protected void LnkAddTrack_Click(object sender, EventArgs e)
{
InsertTrack();
DgPopTracks();
}
Ask any questions you may have, if you want to see my Insert Track and DgPopTracks events
i can post them as well for you
Related
EDITED...
I got an ASP.NET Button inside an UpdatePanel, inside a UserControl.
I want to make the IMAGE visible by CLICKing on the UserControl’s Button (btnSubmit).
Using the following js function I’m able to do it in normal postback mode, but it’s not working in Async mode.
The question is, how can I do this in an Async mode or what’s the best way to do this?
User Control (Categories):
<asp:UpdatePanel ID="upItems" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="btnSubmitPostBack();"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
ASP.NET Page
<script>
function btnSubmitPostBack() {
__doPostBack('<%= btnDoSomething.ClientID %>', '');
}
</script>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc1:Categories ID="Categories1" runat="server" CatRootName="Products" />
<asp:Button ID="btnDoSomething" CssClass="hidden" runat="server" Text=" Do Something" OnClick=" btnDoSomething_Click" />
<asp:Image ID="Image1" runat="server" Visible="false" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnDoSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
protected void btnDoSomething_Click(object sender, EventArgs e)
{
Image1.Visible = true;
up.Update();
}
Thanks for your attention and help in advance!
OK here we go :( Just compiled and ran this successfully) and I think that's the right way to do it: ( the key here is to find the other user control and drill inside it for your control to update:
You user control code :
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Categories.ascx.cs" Inherits="WebApplication1.Categories" %>
<asp:UpdatePanel ID="upItems" runat=*emphasized text*"server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
User control code behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
UpdatePanel ImageUpdatePanel = (UpdatePanel)this.Parent.FindControl("up");
Image _img = (Image)ImageUpdatePanel.FindControl("Image1");
_img.Visible = true;
//Updating UpdatePanel
ImageUpdatePanel.Update();
}
and then your page code
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc1:Categories ID="Categories1" runat="server" CatRootName="Products" />
<asp:Button ID="btnDoSomething" CssClass="hidden" runat="server" Text=" Do Something"/>
<asp:Image ID="Image1" runat="server" Visible="false" ImageUrl="~/Images/heroAccent.png" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnDoSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
this is 100% working solution, if u need I can send u the code too :)
I have two nested Gridviews, the parent is for Posts and child is for Comments. I want to update Post and Comments with a Time Interval so Im using an asp.net Timer. My problem is that a TextBox that is in the first Gridview loses focus when timer Ticks. I searched the web a lot, one possible solution was to take the textbox out of the UpdatePanel but in this situation I can't take out the textBox. Please help me, here is my code.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" Interval="10000" OnTick="Timer1_Tick" runat="server">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<%--post GridView--%>
<asp:GridView ID="posts" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%--Comments Gridview--%>
<asp:GridView ID="comments" runat="server"></asp:GridView>
<%--a Textbox and bUtton For sending new Comment--%>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
protected void Timer1_Tick(object sender, EventArgs e)
{
posts.DataSource = GetData();
posts.DataBind();
}
I was going to type a similar answer to this, but it appears it has already been explained here - https://stackoverflow.com/a/22014420/2248290
The key point being: "An update panel takes focus away as it posts back"
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>
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.
<asp:UpdatePanel ID="CartUpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID='<%= rdoSelect.ClientID %>'/>
</Triggers>
<ContentTemplate>
<asp:Timer ID="cartTimer" Interval="5000" runat="server" OnTick="cartTimer_Tick">
</asp:Timer>
<asp:GridView ID="gridCartSearch" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:HiddenField ID="Serial" runat="server" Value='<%# Eval("serialnum") %>' />
<asp:RadioButton ID="rdoSelect" runat="server" AutoPostBack="true" GroupName="radioBtns"
OnCheckedChanged="rdoSelect_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
I'm getting the following error
A control with ID '<%=
rdoSelect.ClientID %>' could not be
found for the trigger in UpdatePanel
'CartUpdatePanel'.
Change the following line:
<asp:AsyncPostBackTrigger ControlID='<%= rdoSelect.ClientID %>'/>
to:
<asp:AsyncPostBackTrigger ControlID='rdoSelect'/>
You cannot use server side tags inside of a server control (like AsyncPostBackTrigger). Instead you should use the ID of the control and the asp.net runtime will dynamical replace it with the proper client side id.
I tried <asp:AsyncPostBackTrigger ControlID='rdoSelect'/> but it’s not working.
I found a solution here A control with ID 'ImageButton5' could not be found for.
The AsyncPostBack should be registered whenever a row is created.
protected void gridCartSearch_RowCreated(object sender, GridViewRowEventArgs e)
{
Control radioControl = e.Row.Cells[0].FindControl("rdoSelect");
if (radioControl != null)
{
cartScriptMgr.RegisterAsyncPostBackControl(radioControl);
}
}
Update the Update Panel On rdoSelect_CheckedChanged
protected void rdoSelect_CheckedChanged(object sender, EventArgs e)
{
....
CartUpdatePanel.Update();
}
using <asp:AsyncPostBackTrigger ControlID='rdoSelect'/>
Thanks All
You should not have the client id. Just put rdoSelect as the ID. You should also put a tag EventName to be CheckedChanged
The ControlID is the asp.net control id defined in the aspx markup and not the client id, change it as follows:
<asp:AsyncPostBackTrigger ControlID="rdoSelect"/>
Should work