Updatepanel trigger postback in gridview - asp.net

I have the next gridview:
<asp:UpdatePanel ID="upCustomer" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnCustomerSearch" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="gvPersons" EventName="PageIndexChanging" />
</Triggers>
<ContentTemplate>
<asp:TextBox ID="txtCode" runat="server" />
<asp:Button ID="btnCustomerSearch" runat="server" Text="Search" OnClick="btnCustomerSearch_Click" />
<asp:GridView ID="gvPersons" runat="server" AutoGenerateColumns="False" AllowPaging="True"
PageSize="10" GridLines="Vertical" EmptyDataText="No results"
ShowHeaderWhenEmpty="True" OnRowDataBound="gvPersons_RowDataBound"
OnPageIndexChanging="gvPersons_PageIndexChanging"
onselectedindexchanging="gvPersons_SelectedIndexChanging">
<Columns>
<asp:BoundField DataField="Personid" />
<asp:BoundField DataField="Name" HeaderText="Nombre" />
<asp:BoundField DataField="Phone" HeaderText="Telefono" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
</asp:GridView>
</ContentTemplate>
I need the next funcionality:
When I click btnCustomerSearch, I want an Ajax update (works)
When I click pagination, I want an Ajax update (works)
When I click a row (gvPersons_SelectedIndexChanging), I want a postback update, but this doesnt work.
Is posible to get an asyncpostback in some events in a gridview and postback in anothers?

Insert a Button inside your update panel and define a PostBackTrigger for it
<asp:UpdatePanel ID="upCustomer" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
...
<asp:PostBackTrigger ControlID="PostBackButton" />
</Triggers>
<ContentTemplate>
...
<asp:Button ID="PostBackButton" runat="server" Text="Button" OnClick="PostBackButton_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Insert a TemplateField on your grid like this
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="button" onclick="javascript:__doPostBack('<%= PostBackButton.ClientID %>',<%# ((GridViewRow)Container).RowIndex %>)" value="Select" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
When the user clicks on any button inside the grid, we will have to manually fire the Click event of the PostBackButton passing the RowIndex as argument.
On the server side
protected void PostBackButton_Click(object sender, EventArgs e)
{
string argument = Request.Form["__EVENTARGUMENT"];
gvPersons.SelectedIndex = Convert.ToInt32(argument);
}
Now we just have to hide the PostBackButton...
protected void Page_Load(object sender, EventArgs e)
{
// ...
PostBackButton.Style.Add("display", "none");
}
...and disable the event validation of the page, otherwise we will get an ArgumentException when the user selects a row
<%# Page Language="C#" ... EnableEventValidation="false" %>
Yes. It is possible.

As stated here, you can try this
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnCustomerSearch" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="gvPersons" EventName="PageIndexChanging" />
<asp:PostBackTrigger ControlID="gvPersons" EventName="SelectedIndexChanging" />
</Triggers>

Related

Textbox gridview in asp:asyncpostbacktrigger with updatepanel

I have a textbox in a gridview templatefield and I want to handle its TextChanged event. The problem is using the UpdatePanel trigger I get the following message?
A control with ID 'txtQtd' could not be found for the trigger in UpdatePanel 'UpdatePanel1'
How can I incorporate this control into updatepanel?
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField DataField="Origin" ItemStyle-Width="8%" />
<asp:BoundField DataField="Destiny" ItemStyle-Width="8%"/>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtQtd" AutoPostBack="true" OnTextChanged="txtQtd_TextChanged" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Factor" ItemStyle-Width="8%" />
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtQtd" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
what you could do is put an invisible button that calls the txtQtd_TextChanged method from javascript, that is, you put the onchange property inside the textbox and when this is executed you will call the button with document.getElementById (MainContent_btn).click();
It's just an idea

UpdatePanel doesn't allow to fire button click inside gridview

I have a gridview and inside that I have below code.
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!-- only your content that needs refreshing goes here -->
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonAdd" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</FooterTemplate>
</asp:TemplateField>
I have added update panel because when I click ButtonAdd button data in the gridview has been removed. But after I added below code button click event doesn't fire. Please suggest solution.
Please place your whole gridview inside update panel and remove update panel inside and try..
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="gridview1" runat="server" >
</asp:GridView>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>

Gridview inside updatepanel

How do i update gridView inside UpdatePanel.
My delete button i okay. But I have to refresh page to see changes.
btn Button and litTest is for checking updatePanel.
I hope anyone can help me whats wrong...
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" EnablePartialRendering="true" runat="server">
<ContentTemplate>
<asp:Literal ID="litTest" runat="server" />
<asp:GridView
ID="GridViewBruger"
CssClass="TableSort"
runat="server"
CellPadding="4"
GridLines="Horizontal"
AutoGenerateColumns="False"
width="500"
onrowcommand="GridViewCase_RowCommand">
<Columns>
<asp:BoundField DataField="FilePath" />
<asp:BoundField DataField="File" ItemStyle-HorizontalAlign="Center" HeaderText="File" ItemStyle-Width="200px" HeaderStyle-CssClass="header"/>
<asp:BoundField DataField="Date" ItemStyle-HorizontalAlign="Center" HeaderText="Date"/>
<asp:buttonfield buttontype="Image" ItemStyle-HorizontalAlign="Center" ImageUrl="~/img/trash.png" commandname="Del" text="Slet" HeaderText="Delete"/>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btn" Text="testUpdatePanel" runat="server" OnClick="btn_Click" />
Thanks!!
By calling the GridViewBruger.DataBind() on code behind the GridView will be updated.
Also if you add the update button, inside the update panel, and on code behind also just call the DataBind() also the grid view will be updated.
.....
</asp:GridView>
<asp:Button ID="btn" Text="testUpdatePanel" runat="server" OnClick="btn_Click" />
</ContentTemplate>
</asp:UpdatePanel>

button gridview in UpdatePanel

i use gridview and UpdatePanel in asp.net.
<asp:UpdatePanel ID="upSubject" runat="server">
<ContentTemplate>
<div class="c_box" style="height: auto; text-align: center; margin-top: 10px; width:565px; padding-right:140px;">
<asp:GridView ID="gvSubject" runat="server" AutoGenerateColumns="False"
SkinID="gvSubject" ShowHeader="true" ShowHeaderWhenEmpty="true"
onrowcommand="gvSubject_RowCommand">
<Columns>
<asp:BoundField DataField="lblCounter" HeaderText="Number" ReadOnly="True" HeaderStyle-Font-Bold="false" ItemStyle-Width="60px"></asp:BoundField>
<asp:BoundField DataField="lblSubject" HeaderText="Tiltle" ReadOnly="True" HeaderStyle-Font-Bold="false" ItemStyle-Width="300px" ItemStyle-HorizontalAlign="Right"></asp:BoundField>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-Width="60px">
<ItemTemplate>
<asp:ImageButton ID="dltSubject" runat="server" CommandName="remove" CommandArgument="lblCounter" ValidationGroup="Subject" OnClientClick="return confirm('Delete?')" ImageUrl="~/Style/Pic/pic_delete_small.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#000000" />
<EmptyDataRowStyle BackColor="#00ff00" />
<EmptyDataTemplate>
Not item...
</EmptyDataTemplate>
</asp:GridView>
</div>
</ContentTemplate>
<Triggers>
<asp:asyncpostbacktrigger controlid="gvSubject" eventname="RowCommand" />
</Triggers>
</asp:UpdatePanel>
or
<asp:asyncpostbacktrigger controlid="dltSubject" eventname="Click" />
.
protected void gvSubject_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "remove")
{
DataTable _dt = (DataTable)Session["Subject"];
_dt.Rows[int.Parse(e.CommandArgument.ToString())].Delete();
gvSubject.DataSource = _dt;
gvSubject.DataBind();
Session["Subject"] = _dt;
}
}
but not work UpdatePanel. please help me for work button gridview in UpdatePanel.
i want work ImageButton in gridview and UpdatePanel.
You dont have to use Triggers for the RowCommand. Also remove rowcommand if you dont want to use besides this button click.
Please specify OnCommand=dtlSubject_Click
I have used here "Number" as commandargument thinking that it is your unique id needed to delete the record.
You need to specify unique column for deletion in commandargument.
<asp:ImageButton ID="dltSubject" runat="server"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "Number")%>'
ValidationGroup="Subject" OnCommand="dtlSubject_Click"
OnClientClick="return confirm('Delete?')" ImageUrl="~/Style/Pic/pic_delete_small.png" />
And on server side write down following code :
protected void dtlSubject_Click(object sender, CommandEventArgs e)
{
string uniqueid= e.CommandArgument.ToString();
//call your procedure here to delete your item.
}

ASP.NET - Adding an UpdatePanel trigger to a LinkButton inside a gridview

I was trying to update the content of a modal dialog, and this code works for me:
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
<asp:UpdatePanel ID="upNewUpdatePanel" runat="server">
<ContentTemplate>
<asp:Label ID="updateLabel" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="updateSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
However, when I try to place the LinkButton inside a gridview, like so:
<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
<asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
<asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
</asp:TemplateField>
</Columns>
</asp:GridView>
This does not work, I get an error saying: A control with ID 'updateSomething' could not be found for the trigger in UpdatePanel 'upNewUpdatePanel'.
How can I use the ImageButton inside the gridview?
Try and add the asp:AsyncPostBackTrigger to the asp:GridView's OnRowCommand event and handle the link button click in that event
<asp:GridView ID="grdListUsers" runat="server" onRowCommand="grdListUsers_RowCommand">
<asp:TemplateField>
<asp:LinkButton ID="updateSomething" CommandName="update-something" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</asp:TemplateField>
</asp:GridView>
and in the cs create the event like this
protected void grdListUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "update-something")
{
grdListUsers.SelectedIndex = Convert.ToInt32(e.CommandArgument);
}
}
You could add a trigger of the gridview
<Triggers>
<asp:PostBackTrigger ControlID="gridview1" />
</Triggers>
Add another Update Panel surrounding your link button just like below.
<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
<asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
<asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:UpdatePanel ID="aa" runat="server">
<ContentTemplate>
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="updateSomething"/>
</Triggers>
</asp:UpdatePanel>
</asp:TemplateField>
</Columns>
</asp:GridView>
You could set the UpdatePanel's UpdateMode to Conditional and update it manually from the UpdateButton_Click-Handler:
<asp:UpdatePanel ID="UdpFormPanel" runat="server" UpdateMode="conditional" ChildrenAsTriggers="false" >
LinkButton's Click-Event handler:
Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As EventArgs)
'blah....
upNewUpdatePanel.Update()
End Sub

Resources