This is a migrated thread and some comments may be shown as answers.

RadAjaxManager UserControl Jquery Code are empty after a PostBack()

2 Answers 35 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Ashkan
Top achievements
Rank 1
Ashkan asked on 10 Nov 2014, 01:16 PM
Hi
when use RadAjaxManager my UserControl Jquery Code does not work after a PostBack();
my user Control Code is:
01.<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CtrlTextMessage.ascx.cs" Inherits="My.Sms.UserControl.CtrlTextMessage" %>
02.<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
03.<telerik:RadCodeBlock runat="server" >
04.    <script>
05.        $(document).ready(function() {
06.            var totalCharsFa = 350;
07.            var totalCharsEn = 800;
08.            var countTextBox = $('#<%= txtMessege.ClientID %>');
09.            var charsCountEl = $('#countchars');
10.            var countMessageEl = $('#countMessage');
11.            charsCountEl.text(0);
12.            countMessageEl.text(0);
13.            countTextBox.keyup(function() {
14.                var persian = /[^\x00-\x7E]/;
15.                var thisChars = this.value.replace(/{.*}/g, '').length;
16.                var charsToDel;
17.                if (persian.test(this.value.substring(0, thisChars))) {
18.                    if (thisChars > totalCharsFa) {
19.                        charsToDel = (thisChars - totalCharsFa);
20.                        this.value = this.value.substring(0, this.value.length - charsToDel);
21.                    } else {
22.                        countMessageEl.text(parseInt(this.value.length / 70) + 1);
23.                        charsCountEl.text(thisChars);
24.                    }
25. 
26.                } else {
27.                    if (thisChars > totalCharsEn) {
28.                        charsToDel = (thisChars - totalCharsFa);
29.                        this.value = this.value.substring(0, this.value.length - charsToDel);
30.                    } else {
31.                        charsCountEl.text(thisChars);
32.                        countMessageEl.text(parseInt(this.value.length / 160) + 1);
33.                    }
34. 
35.                }
36.            });
37.        });
38.    </script>
39.</telerik:RadCodeBlock>
40. 
41.<span style="padding-left: 62px;">متن پیام:</span>
42.<telerik:RadTextBox runat="server" ID="txtMessege" TextMode="MultiLine" Width="350px" Height="100px" Resize="None" ></telerik:RadTextBox>
43.<br /><br />
44.<span>تعداد کاراکتر پیام شما: <span id="countchars" ></span>  /  تعداد پیام: <span id="countMessage"></span></span>


My Aspx Web Form Code is:
001.<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Moderator/Master.Master" CodeBehind="Dashboard.aspx.cs" Inherits="Moderator.Dashboard" %>
002.<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
003.<%@ Register Src="~/Sms/UserControl/CtrlTextMessage.ascx" TagPrefix="uc1" TagName="CtrlTextMessage" %>
004.<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1">
005.    <telerik:RadCodeBlock runat="server">
006.        <script>
007. 
008.        </script>
009.    </telerik:RadCodeBlock>
010.    <telerik:RadSkinManager ID="QsfSkinManager" runat="server" ShowChooser="False" Skin="MetroTouch"  />
011.    <telerik:RadFormDecorator ID="QsfFromDecorator" runat="server" DecoratedControls="All" EnableRoundedCorners="false" Font-Names="Yekan"/>
012.    <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
013.    </telerik:RadScriptManager>
014.    <telerik:RadProgressManager ID="RadProgressManager1" Runat="server" />
015.    <telerik:RadAjaxManager ID="RadAjaxManagerOperation" runat="server" EnablePageHeadUpdate="True" >
016.        <AjaxSettings>
017.            <telerik:AjaxSetting AjaxControlID="cmbTextMessage" >
018.                <UpdatedControls>
019.                    <telerik:AjaxUpdatedControl ControlID="cmbTextMessage" LoadingPanelID="RadAjaxLoadingPanel1"/>
020.                    <telerik:AjaxUpdatedControl ControlID="CtrlTextMessage" LoadingPanelID="RadAjaxLoadingPanel1" />
021.                </UpdatedControls>
022.            </telerik:AjaxSetting>
023.        </AjaxSettings>
024.    </telerik:RadAjaxManager>
025.    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="MetroTouch"/>
026.            <telerik:RadGrid ID="gridShowSms" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellSpacing="0" GridLines="Horizontal" Width="890px" AllowPaging="True" Skin="Metro"
027.                             AllowAutomaticUpdates="True" AllowAutomaticInserts="False" ShowStatusBar="True"
028.                             AllowAutomaticDeletes="True" PageSize="100" EnableLinqExpressions="False" OnDetailTableDataBind="gridShowSms_DetailTableDataBind" OnNeedDataSource="gridShowSms_NeedDataSource" OnPreRender="gridShowSms_PreRender"  >
029.                <PagerStyle Mode="NumericPages" ></PagerStyle>
030.                <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="False" EnableRowHoverStyle="True" >
031.                    <Selecting AllowRowSelect="True" />
032.                </ClientSettings>
033.                <MasterTableView DataKeyNames="ReciveId" AllowMultiColumnSorting="True" Width="100%">
034.                    <%--<ItemStyle BackColor="#3399FF"/>
035.            <AlternatingItemStyle BackColor="#CCFF66"/>--%>
036.                    <ParentTableRelation>
037.                        <telerik:GridRelationFields DetailKeyField="ReciveId" MasterKeyField="ReciveId">
038.                        </telerik:GridRelationFields>
039.                    </ParentTableRelation>
040.                    <DetailTables>
041.                        <telerik:GridTableView DataKeyNames="SenderId" Name="Sender" >
042.                            <Columns>
043.                                <telerik:GridBoundColumn SortExpression="RowIndexSender" HeaderText="ردیف" HeaderButtonType="TextButton" HeaderStyle-Width="1"
044.                                                         DataField="RowIndexSender">
045.                                </telerik:GridBoundColumn>
046.                                <telerik:GridBoundColumn SortExpression="Body" HeaderText="متن پیام" HeaderButtonType="TextButton"
047.                                                         DataField="Body" UniqueName="Body">
048.                                </telerik:GridBoundColumn>
049.                                <telerik:GridBoundColumn SortExpression="ReciverInSender" HeaderText="گیرنده پیام" HeaderButtonType="TextButton"
050.                                                         DataField="ReciverInSender" UniqueName="ReciverInSender">
051.                                </telerik:GridBoundColumn>
052.                                <telerik:GridBoundColumn SortExpression="ResendCount" HeaderText="تعداد تلاش مجدد" HeaderButtonType="TextButton"
053.                                                         DataField="ResendCount" UniqueName="ResendCount">
054.                                </telerik:GridBoundColumn>
055.                                <telerik:GridBoundColumn SortExpression="ReciverInSenderStatus" HeaderText="وضعیت" HeaderButtonType="TextButton"
056.                                                         DataField="ReciverInSenderStatus" UniqueName="ReciverInSenderStatus">
057.                                </telerik:GridBoundColumn>
058.                            </Columns>
059.                        </telerik:GridTableView>
060.                    </DetailTables>
061.                    <Columns>
062.                        <%--                        <telerik:GridClientSelectColumn FilterControlAltText="Filter column4 column" UniqueName="column4" HeaderText="انتخاب" HeaderStyle-Height="1">
063.                            <HeaderStyle Height="1px" />
064.                        </telerik:GridClientSelectColumn>--%>
065.                        <telerik:GridBoundColumn SortExpression="RowIndex" HeaderText="ردیف" HeaderButtonType="TextButton"
066.                                                 DataField="RowIndex">
067.                        </telerik:GridBoundColumn>
068.                        <telerik:GridBoundColumn DataField="Id" FilterControlAltText="Filter column1 column" HeaderText="Id" UniqueName="Id" Visible="True" Display="False" >
069.                        </telerik:GridBoundColumn>
070.                        <telerik:GridBoundColumn SortExpression="Body" HeaderText="متن پیام" HeaderButtonType="TextButton"
071.                                                 DataField="Body" UniqueName="Body">
072.                        </telerik:GridBoundColumn>
073.                        <telerik:GridBoundColumn SortExpression="Sender" HeaderText="فرستنده" HeaderButtonType="TextButton"
074.                                                 DataField="Sender" UniqueName="Sender">
075.                        </telerik:GridBoundColumn>
076.                        <telerik:GridBoundColumn SortExpression="Reciver" HeaderText="گیرنده پیام" HeaderButtonType="TextButton"
077.                                                 DataField="Reciver" UniqueName="Reciver">
078.                        </telerik:GridBoundColumn>
079.                        <telerik:GridBoundColumn SortExpression="ProprtyCode" HeaderText="کد مالکیت" HeaderButtonType="TextButton"
080.                                                 DataField="ProprtyCode" UniqueName="ProprtyCode">
081.                        </telerik:GridBoundColumn>
082.                        <telerik:GridBoundColumn SortExpression="SendDate" HeaderText="زمان پیامک" HeaderButtonType="TextButton"
083.                                                 DataField="SendDate" UniqueName="SendDate">
084.                        </telerik:GridBoundColumn>
085.                        <telerik:GridBoundColumn SortExpression="TypeOfRequest" HeaderText="نوع درخواست" HeaderButtonType="TextButton"
086.                                                 DataField="TypeOfRequest" UniqueName="TypeOfRequest">
087.                        </telerik:GridBoundColumn>
088.                        <telerik:GridBoundColumn SortExpression="Username" HeaderText="کاربر" HeaderButtonType="TextButton"
089.                                                 DataField="Username" UniqueName="Username">
090.                        </telerik:GridBoundColumn>
091.                        <telerik:GridTemplateColumn UniqueName="Operation" HeaderText="عملیات" 
092.                                                    ItemStyle-HorizontalAlign="Center"  >
093.                            <ItemTemplate>
094.                                <telerik:RadButton ID="btnOpertion" runat="server" Width="28px" Height="28px" ToolTip="انجام عملیات" CommandArgument='<%# Eval("Id") %>' OnCommand="btnOperation_Click" AutoPostBack="True">
095.                                    <Image EnableImageButton="True" ImageUrl="../Images/Sms/edit.png" IsBackgroundImage="True"></Image>
096.                                </telerik:RadButton>
097.                            </ItemTemplate>
098.                        </telerik:GridTemplateColumn>
099.                        <telerik:GridBoundColumn SortExpression="ReciverStatus" HeaderText="وضعیت" HeaderButtonType="TextButton"
100.                                                 DataField="ReciverStatus" UniqueName="ReciverStatus">
101.                        </telerik:GridBoundColumn>
102.                    </Columns>
103.                </MasterTableView>
104.            </telerik:RadGrid>
105.            <telerik:RadWindow ID="winOperation" runat="server" Height="600px" Width="960px"
106.                               EnableShadow="true" Behaviors="Close" Visible="True" Modal="true"
107.                               ShowContentDuringLoad="false" Title="انجام عملیات" >
108.                <ContentTemplate>
109.                        <table style="text-align: right;">
110.                            <tr>
111.                                <td>
112.                                <th style="padding-bottom: 10px;">
113.                                    <span class="blue">نام : </span><br/>
114.                                    <telerik:RadTextBox ID="txtName" runat="server" Width="250px" MaxLength="20" >
115.                                    </telerik:RadTextBox>
116.                                    <br />
117.                                    <%--                                    <asp:RequiredFieldValidator ID="rfvtxtName" runat="server" ControlToValidate="txtName" ErrorMessage="لطفا نام خود را وارد کنید" SetFocusOnError="True"></asp:RequiredFieldValidator>--%>
118.                                    <span class="blue">نام خانوادگی : </span><br/>
119.                                    <telerik:RadTextBox ID="txtFamily" runat="server" Width="250px" MaxLength="30" >
120.                                    </telerik:RadTextBox>
121.                                    <br />
122.                                    <%--                                    <asp:RequiredFieldValidator ID="rfvtxtFamily" runat="server" ControlToValidate="txtFamily" ErrorMessage="لطفا نام خانوادگی خود را وارد کنید" SetFocusOnError="True"></asp:RequiredFieldValidator>--%>
123.                                    <span class="blue">شماره تلفن همراه : </span>
124.                                    <br/>
125.                                    <telerik:RadTextBox ID="txtMobileNumber" runat="server" CssClass="fontLeft" EmptyMessage=" 09123456789" InputType="Tel" MaxLength="11" Width="250px">
126.                                    </telerik:RadTextBox>
127.                                    <br/>
128.                                    <%--                                    <asp:RequiredFieldValidator ID="rfvtxtMobileNumber" runat="server" ControlToValidate="txtMobileNumber" Display="Dynamic" ErrorMessage="شماره همراه را وارد کنید" SetFocusOnError="True"></asp:RequiredFieldValidator>
129.                                    <asp:RegularExpressionValidator ID="revtxtMobileNumber" runat="server" ControlToValidate="txtMobileNumber" Display="Dynamic" ErrorMessage="شماره همراه وارد شده صحیح نمیباشد" SetFocusOnError="True" ValidationExpression="(0|\+98)?([ ]|-|[()]){0,2}9[1|2|3|4]([ ]|-|[()]){0,2}(?:[0-9]([ ]|-|[()]){0,2}){8}"></asp:RegularExpressionValidator>--%>
130.                                    <span>کد مالکیت : </span>
131.                                    <telerik:RadComboBox runat="server" ID="cmbPropertyCode" Width="230" OnInit="cmbPropertyCode_Init" >
132.                                    </telerik:RadComboBox>
133.                                    <br/>
134.                                    <span class="blue">مبلغ (به ریال) : </span><br/>
135.                                    <telerik:RadTextBox runat="server" ID="txtPrice" Width="250px"></telerik:RadTextBox>
136. 
137.                                </th>
138.                                <th style="padding-bottom: 10px; padding-right: 200px;">
139.                                    
140.                                    <span class="blue">قالب پیام : </span><br/>
141.                                    <telerik:RadComboBox runat="server" ID="cmbTextMessage" Width="330" OnInit="cmbTextMessage_Init" OnSelectedIndexChanged="cmbTextMessage_SelectedIndexChange" AutoPostBack="True">
142.                                    </telerik:RadComboBox>
143.                                    <br/>
144.                                    <uc1:CtrlTextMessage runat="server" id="CtrlTextMessage" />
145.                                    <span>شارژ مانده : </span><asp:Label runat="server" ID="lblCharge"></asp:Label>
146.                                    <br/>
147.                                    <span>زمان پيامک : </span><asp:Label runat="server" ID="lblDateTime" Text="1392"></asp:Label>
148.                                </th>
149.                            </td>
150.                            </tr>
151.                        </table>
152. 
153.                </ContentTemplate>
154.            </telerik:RadWindow>
155.        </div>
156.    </div><%--HeaderStyle-HorizontalAlign="Center"--%>
157.</asp:Content>


My Aspx.cs Web Form Code is:
001.protected void gridShowSms_PreRender(object sender, EventArgs e)
002.{
003.    if (!Page.IsPostBack)
004.    {
005.        gridShowSms.MasterTableView.Items[0].Expanded = true;
006.        gridShowSms.MasterTableView.Items[0].ChildItem.NestedTableViews[0].Items[0].Expanded = true;
007.    }
008.}
009. 
010.protected void gridShowSms_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
011.{
012.    if (!e.IsFromDetailTable)
013.    {
014. 
015.        using (var db = new MyDataContext())
016.        {
017.            var reciveMsg = db.TBL_ReceiverMsgs.ToList().Select((c, index) => new
018.            {
019.                RowIndex = index + 1,
020.                c.Id,
021.                c.Body,
022.                c.Sender,
023.                SendDate = ClsHelper.GetPersianDate(c.RecieveDate.Date) + "-" + c.RecieveDate.Hour + ":" + c.RecieveDate.Minute,
024.                ReciveId = c.Id,
025.                Reciver = c.Receiver,
026.                ProprtyCode = c.PropertyCode ,
027.                c.TypeOfRequest,
028.                c.Username,
029.                ReciverStatus = c.Status ? "رسیده به گوشی" : "نرسیده به گوشی"
030. 
031.            });
032.            gridShowSms.DataSource = reciveMsg;
033.        }
034. 
035.         
036.    }
037.}
038. 
039.protected void gridShowSms_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
040.{
041.    var dataItem = (GridDataItem)e.DetailTableView.ParentItem;
042.    switch (e.DetailTableView.Name)
043.    {
044.        case "Sender":
045.            {
046.                int reciverId = Convert.ToInt32(dataItem.GetDataKeyValue("ReciveId").ToString());
047.                using (var db=new MyDataContext())
048.                {
049.                    var detail = db.TBL_SenderMsgs.Where(c => c.ReceiverMsgId.Equals(reciverId)).ToList();
050.                         
051.                        var sendMsg = detail.Select((c, index) => new
052.                                {
053.                                    RowIndexSender = index + 1,
054.                                    SenderId = c.Id,
055.                                    c.Body,
056.                                    ReciverInSender = c.Receiver,
057.                                    c.ResendCount,
058.                                    ReciverInSenderStatus = c.Status==0 ? "رسیده به گوشی" : "نرسیده به گوشی"
059.                                }).ToList();
060.                    e.DetailTableView.DataSource = sendMsg;
061.                }
062.                 
063.                break;
064.            }
065.    }
066.}
067.protected void btnOperation_Click(object sender, CommandEventArgs e)
068.{
069.    Id = Convert.ToInt32(e.CommandArgument);
070.    winOperation.VisibleOnPageLoad = true;
071.    using (var db = new MyDataContext())
072.    {
073.        var selectMsg = db.TBL_ReceiverMsgs.FirstOrDefault(c => c.Id.Equals(Id));
074.        if (selectMsg != null)
075.        {
076.            var userInfo =
077.                db.TBL_Users.FirstOrDefault(c => c.TBL_PropertyCode.PropertyCode.Equals(selectMsg.PropertyCode));
078.             
079.            if (userInfo != null)
080.            {
081.                txtName.Text = userInfo.Name;
082.                txtFamily.Text = userInfo.Family;
083.                lblCharge.Text = userInfo.Charge.ToString("N0");
084.            }
085.            else
086.            {
087.                txtName.Text = txtFamily.Text = "";
088.                lblCharge.Text = "0";
089.            }
090.            lblDateTime.Text = ClsHelper.GetPersianDate(selectMsg.RecieveDate);
091.            txtMobileNumber.Text = "0" + selectMsg.Sender;
092.            txtPrice.Text = "5000";
093.            FillCmbPropertyCode(Id);
094.            ((RadTextBox)CtrlTextMessage.FindControl("txtMessege")).Text = selectMsg.Body;
095.        }
096.    }
097.    string script = "function f(){$find(\"" + winOperation.ClientID + "\").show(); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
098.    ScriptManager.RegisterStartupScript(this, this.GetType(), "key", script, true);
099.}
100.private void FillCmbPropertyCode(int id)
101.{
102.    using (var db = new MyDataContext())
103.    {
104.        var lst = db.TBL_Users.Where(c => c.PropertyCodeId == id).ToList();
105.        var result = lst.Select(c => new
106.        {
107.            name = c.TBL_PropertyCode.PropertyCode,
108.            id = c.Id
109.        }).ToList();
110.        cmbPropertyCode.DataSource = result;
111.        cmbPropertyCode.DataValueField = "id";
112.        cmbPropertyCode.DataTextField = "name";
113.        cmbPropertyCode.DataBind();
114.        cmbPropertyCode.SelectedIndex = 0;
115.    }
116.}
117.private void FillCmbTextMessage()
118.{
119.    using (var db = new MyDataContext())
120.    {
121.        var lst = db.TBL_TextMessages.ToList();
122.        var result = lst.Select((c,index) => new
123.        {
124.            name = index + 1 + "-" + c.UniqName,
125.            id = c.Id
126.        }).ToList();
127.        cmbTextMessage.DataSource = result;
128.        cmbTextMessage.DataValueField = "id";
129.        cmbTextMessage.DataTextField = "name";
130.        cmbTextMessage.DataBind();
131.        cmbTextMessage.SelectedIndex = 0;
132.    }
133.}
134.protected void cmbTextMessage_SelectedIndexChange(object sender, EventArgs e)
135.{
136.    using (var db=new MyDataContext())
137.    {
138.        var selectText = db.TBL_TextMessages.FirstOrDefault(c=> c.Id.Equals(Convert.ToInt32(cmbTextMessage.SelectedValue)));
139.        if (selectText != null)
140.        {
141.            ((RadTextBox) CtrlTextMessage.FindControl("txtMessege")).Text = selectText.TextMessage;
142.        }
143.         
144.             
145.    }
146.}
147.protected void cmbPropertyCode_Init(object sender, EventArgs e)
148.{
149.    
150.}
151. 
152.protected void cmbTextMessage_Init(object sender, EventArgs e)
153.{
154.    FillCmbTextMessage();
155.}

After Run Method:"cmbTextMessage_SelectedIndexChange"
UserControl Does Not Word!
Jquery Code Disabled!
How To Resolve This Problem?????
And:
 if gridShowSms_DetailTableDataBind is null , Crash Application and error this line in gridShowSms_PreRender method:
gridShowSms.MasterTableView.Items[0].ChildItem.NestedTableViews[0].Items[0].Expanded = true;

I'm desperately need help!
TIA!

2 Answers, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 12 Nov 2014, 11:44 AM
Hello Ashkan,


Note that   $(document).ready does not execute after Ajax calls.

Therefore in your case I would suggest you to implement the client logic int he client pageLoad event like this:
<telerik:RadScriptBlock runat="server" >
04.    <script>
05.        function pageLoad() {
06.            var totalCharsFa = 350;
07.            var totalCharsEn = 800;
08.            var countTextBox = $('#<%= txtMessege.ClientID %>');
09.            var charsCountEl = $('#countchars');
10.            var countMessageEl = $('#countMessage');
11.            charsCountEl.text(0);
12.            countMessageEl.text(0);
13.            countTextBox.keyup(function() {
14.                var persian = /[^\x00-\x7E]/;
15.                var thisChars = this.value.replace(/{.*}/g, '').length;
16.                var charsToDel;
17.                if (persian.test(this.value.substring(0, thisChars))) {
18.                    if (thisChars > totalCharsFa) {
19.                        charsToDel = (thisChars - totalCharsFa);
20.                        this.value = this.value.substring(0, this.value.length - charsToDel);
21.                    } else {
22.                        countMessageEl.text(parseInt(this.value.length / 70) + 1);
23.                        charsCountEl.text(thisChars);
24.                    }
25. 
26.                } else {
27.                    if (thisChars > totalCharsEn) {
28.                        charsToDel = (thisChars - totalCharsFa);
29.                        this.value = this.value.substring(0, this.value.length - charsToDel);
30.                    } else {
31.                        charsCountEl.text(thisChars);
32.                        countMessageEl.text(parseInt(this.value.length / 160) + 1);
33.                    }
34. 
35.                }
36.            });
37.        };
38.    </script>
39.</telerik:RadScriptBlock>
 and see how it goes.

Also try replacing the RadCodeBlock with RadScriptBlock and verify if this makes any difference.

Regards,
Maria Ilieva
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ashkan
Top achievements
Rank 1
answered on 12 Nov 2014, 06:18 PM
Thank you Maria...
Excellent performance...
Tags
Ajax
Asked by
Ashkan
Top achievements
Rank 1
Answers by
Maria Ilieva
Telerik team
Ashkan
Top achievements
Rank 1
Share this question
or