Results 1 to 7 of 7

Thread: How to handle Post back if using Ajax

  1. #1

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    How to handle Post back if using Ajax

    Hi all,

    Before I used updatepanel the code below is worked.

    asp Code:
    1. Protected Sub grdStatusMaster_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdStatusMaster.RowEditing
    2.  
    3.         With grdStatusMaster
    4.  
    5.             .EditIndex = e.NewEditIndex
    6.             .ShowFooter = False
    7.             .Width = 560
    8.             .Columns.Item(4).ItemStyle.Width = 125
    9.  
    10.             If Me.IsPostBack Then
    11.  
    12.                 Me.grdStatusMaster.DataSource = (CType(Session("myDatatable"), DataTable)).DefaultView
    13.                 Me.grdStatusMaster.DataBind()
    14.                 Dim descTB As TextBox
    15.                 Dim rowCheckbox As CheckBox
    16.                 Dim strScript As String
    17.                 descTB = .Rows(.EditIndex).Cells(2).FindControl("txtdesc")
    18.                 rowCheckbox = .Rows(.EditIndex).Cells(0).FindControl("ChkSelect")
    19.  
    20.                 strScript = "<script language=""JavaScript"">" & vbCrLf & _
    21.                              vbTab & "document.getElementById('" & descTB.ClientID & "')" & ".focus();" & _
    22.                               vbCrLf & vbTab & "document.getElementById('" & descTB.ClientID & "')" & ".select();" & _
    23.                               vbCrLf & vbTab & "document.getElementById('" & rowCheckbox.ClientID & "')" & ".checked = true;" & _
    24.                              vbCrLf & "<" & "/script>"
    25.                 Page.ClientScript.RegisterStartupScript(GetType(String), "HighLightText", strScript)
    26.  
    27.             End If
    28.         End With
    29.  
    30.     End Sub

    But after I used updatepanel the code above doesn't worked.

    Can any one tell me why it happened and how to solve my issue ?

    Thanks a lot.
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to handle Post back if using Ajax

    If in an updatepanel, you can't use Page.ClientScript, you need to use ScriptManager.

  3. #3

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: How to handle Post back if using Ajax

    Quote Originally Posted by mendhak View Post
    If in an updatepanel, you can't use Page.ClientScript, you need to use ScriptManager.
    I has changed the code as below but still cannot worked :-(

    asp Code:
    1. Protected Sub grdStatusMaster_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdStatusMaster.RowEditing
    2.  
    3.  
    4. With grdStatusMaster
    5.  
    6. .EditIndex = e.NewEditIndex
    7.  
    8. .ShowFooter = False
    9.  
    10. .Width = 560
    11.  
    12. .Columns.Item(4).ItemStyle.Width = 125
    13.  
    14. If Me.IsPostBack Then
    15. Me.grdStatusMaster.DataSource = (CType(Session("myDatatable"), DataTable)).DefaultView
    16.  
    17. Me.grdStatusMaster.DataBind()
    18. Dim descTB As TextBox
    19.  
    20. Dim rowCheckbox As CheckBox
    21. Dim strScript As String
    22.  
    23.  
    24. descTB = .Rows(.EditIndex).Cells(2).FindControl("txtdesc")rowCheckbox = .Rows(.EditIndex).Cells(0).FindControl("ChkSelect")
    25.  
    26.  
    27.  
    28. strScript = "<script language=""JavaScript"">" & vbCrLf & _
    29. vbTab & "document.getElementById('" & descTB.ClientID & "')" & ".focus();" & _
    30.  
    31. vbCrLf & vbTab & "document.getElementById('" & descTB.ClientID & "')" & ".select();" & _
    32. vbCrLf & vbTab & "document.getElementById('" & rowCheckbox.ClientID & "')" & ".checked = true;" & _
    33.  
    34. vbCrLf & "<" & "/script>"
    35. ScriptManager.RegisterStartupScript(Page, GetType(Page), "HighLightText", strScript, True)
    36.  
    37. End If
    38.  
    39.  
    40.  
    41. End With
    42.  
    43. End Sub
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  4. #4
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How to handle Post back if using Ajax

    You're injecting JS into a page that's already loaded. Think about that. The point of using an UpdatePanel is to prevent the page from reloading when an action takes place. No reload, no inline JS execution.

  5. #5

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: How to handle Post back if using Ajax

    hmm.. thanms timeshifter.

    then what is the purpose by using scriptmanager ?

    furthermore,got any others way to achieve my purpose ?
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How to handle Post back if using Ajax

    No, you can still send javascript to be run from an UpdatePanel, you just have to use ScriptManager. Show your markup.

  7. #7

    Thread Starter
    Hyperactive Member nUflAvOrS's Avatar
    Join Date
    Jul 2007
    Location
    Malaysia/ Currently at Singapore
    Posts
    372

    Re: How to handle Post back if using Ajax

    Thanks a lot Mendnak :-)

    asp Code:
    1. <%@ Control Language="VB" AutoEventWireup="false" CodeFile="MasterTable.ascx.vb" Inherits="MasterTable" %>
    2. <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    3.     Namespace="System.Web.UI" TagPrefix="asp" %>
    4. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="Aj" %>
    5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    6.  
    7.  
    8.  
    9.  
    10. <Aj:ToolkitScriptManager ID="ToolkitScriptManager2" runat="server">
    11. </Aj:ToolkitScriptManager>
    12. <div id = "Div1">
    13.     &nbsp; &nbsp;&nbsp;
    14.     <asp:Menu ID = "MST_Menu" runat = "server" Orientation="Horizontal" CssClass="toolbar1" Width="96px" BackColor="White">
    15.             <StaticMenuStyle CssClass="toolbar1" />
    16.             <StaticMenuItemStyle CssClass="toolbar1" />
    17.             <DynamicHoverStyle CssClass="toolbar1" />
    18.             <DynamicMenuStyle CssClass="toolbar1" />
    19.         <Items>
    20.             <asp:MenuItem ImageUrl="~/Images/Menu/application_edit.png"  Text="Add New"
    21.                 ToolTip="Add New Item" Value="Add New" PopOutImageUrl="~/Images/Menu/application_form_edit.png"></asp:MenuItem>
    22.            
    23.             <asp:MenuItem ImageUrl="~/Images/Menu/database_lightning.png" Text="Delete" ToolTip="Delete Selected Items"
    24.                 Value="Delete" PopOutImageUrl="~/Images/Menu/delete.png"></asp:MenuItem>
    25.         </Items>
    26.     </asp:Menu>
    27.     &nbsp;
    28.    
    29.     <table border = 0 align = "left" width = "100%" height = "75%">
    30.    
    31.     <tr>
    32.         <td style="width: 169px; height: 250px;" colspan = "2" valign = "top" bordercolor="gray" bordercolordark="#003333" bordercolorlight="#000066" >
    33.                 <asp:UpdatePanel ID="UpdatePanel1" runat = "server" >
    34.                 <ContentTemplate>
    35.                 <div>
    36.                     &nbsp; &nbsp;</div>
    37.                     <asp:GridView AllowPaging="True"
    38.                     AllowSorting="True"
    39.                     AutoGenerateColumns="False"
    40.                     BackColor="White"
    41.                     BorderColor="#CCCCCC"
    42.                     BorderStyle="None" BorderWidth="1px"
    43.                     CellPadding="2" ForeColor="Black"
    44.                     ID="grdStatusMaster" runat="server" Width="460px">
    45.                         <FooterStyle BackColor="Gray" ForeColor="Black" />
    46.                         <EmptyDataRowStyle BackColor="White" />
    47.                         <Columns >
    48.                             <asp:TemplateField HeaderImageUrl="~/Images/TreeviewICO/script_edit.jpg">
    49.                                 <HeaderTemplate>
    50.                                     <asp:CheckBox ID="chkHeader" runat="server"  />
    51.                                 </HeaderTemplate>
    52.                                 <ItemStyle Width="15px" />
    53.                                 <ItemTemplate>
    54.                                     <asp:CheckBox ID="ChkSelect" runat="server" />
    55.                                 </ItemTemplate>
    56.                             </asp:TemplateField>
    57.                             <asp:TemplateField HeaderText="ID">
    58.                                 <FooterTemplate>
    59.                                     <asp:TextBox ID="txtFTID" runat="server"></asp:TextBox>
    60.                                 </FooterTemplate>
    61.                                 <ItemTemplate>
    62.                                     <%# DataBinder.Eval(Container.DataItem,"ID") %>
    63.                                 </ItemTemplate>
    64.                                 <HeaderTemplate>
    65.                                     ID
    66.                                     <asp:TextBox ID="txtHID" runat="server" Width="104px"></asp:TextBox>
    67.                                 </HeaderTemplate>
    68.                             </asp:TemplateField>
    69.                             <asp:TemplateField HeaderText="Description">
    70.                                 <EditItemTemplate>
    71.                                     <asp:TextBox ID="txtDesc" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ID Description") %>'
    72.                                         Width="224px"></asp:TextBox>
    73.                                 </EditItemTemplate>
    74.                                 <FooterTemplate>
    75.                                     <asp:TextBox ID="txtFTDesc" runat="server" Width="224px"></asp:TextBox>
    76.                                 </FooterTemplate>
    77.                                 <ItemTemplate>
    78.                                     <%#DataBinder.Eval(Container.DataItem, "ID Description")%>
    79.                                 </ItemTemplate>
    80.                                 <HeaderTemplate>
    81.                                     Desc &nbsp;<asp:TextBox ID="txtHDesc" runat="server" Width="128px"></asp:TextBox>
    82.                                 </HeaderTemplate>
    83.                             </asp:TemplateField>
    84.                             <asp:CheckBoxField DataField="status" HeaderText="S">
    85.                                 <ItemStyle Width="15px" />
    86.                             </asp:CheckBoxField>
    87.                             <asp:TemplateField>
    88.                                 <EditItemTemplate>
    89.                                     <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update"
    90.                                         Text="Update" />&nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False"
    91.                                             CommandName="Cancel" Text="Cancel" />
    92.                                 </EditItemTemplate>
    93.                                 <FooterTemplate>
    94.                                     <asp:Button ID="cmdADD" runat="server" Height="24px" Text="Add" Width="64px" />
    95.                                 </FooterTemplate>
    96.                                 <ItemStyle HorizontalAlign="Center" Width="15px" Wrap="True" />
    97.                                 <ItemTemplate>
    98.                                     <asp:Button ID="btnEdit" runat="server" CausesValidation="False" CommandName="Edit"
    99.                                         Text="Edit" />
    100.                                 </ItemTemplate>
    101.                                 <HeaderTemplate>
    102.                                     <asp:Button ID="cmdHFind" runat="server" Text="Find" Width="48px" />
    103.                                 </HeaderTemplate>
    104.                             </asp:TemplateField>
    105.                         </Columns>
    106.                         <RowStyle VerticalAlign="Top"/>
    107.                         <EmptyDataTemplate >
    108.                             &nbsp;<asp:Label ID="Label1" runat="server" Height="20px" Text="No data found" Width="104px"></asp:Label>
    109.                         </EmptyDataTemplate>
    110.                         <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
    111.                         <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
    112.                         <HeaderStyle BackColor="DarkGray" Font-Bold="True" ForeColor="White" />
    113.                         <AlternatingRowStyle BackColor="WhiteSmoke" />
    114.                     </asp:GridView>
    115.              </ContentTemplate>
    116.                 <Triggers>
    117.                     <asp:AsyncPostBackTrigger ControlID="MST_Menu" EventName="MenuItemClick" />
    118.                 </Triggers>
    119.             </asp:UpdatePanel>
    120.             &nbsp;&nbsp;</td>
    121.     </tr>
    122.        
    123.        </table>
    124.             <asp:Literal ID="CheckBoxIDsArray" runat="server"></asp:Literal>&nbsp;      
    125.    
    126.     <br />
    127.  
    128.     <br />
    129.  
    130.       </div>
    Where there is no hope, there can be no endeavor.

    There are two ways of rising in the world, either by your own industry or by the folly of others.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width