How do I Prevent Postback after a PageMethod (as webmethod) exception?-URGENT!

Split off from

http://forums.asp.net/t/1417300.aspx/1?How+do+I+Prevent+Postback+after+a+PageMethod+as+webmethod+exception+URGENT+

hi there mrmercury..

i’ve the same problem except that i want the page to postback  if the onSuccess callback function gets hit

i’ve listbox (NewModelLstBx) bound to database and a button (RemoveModelLnk) to delete selected item in that listbox .. i want that when i click the delete button a confirm messege popup with selected item text when the user click ok the
button click event triggres and delete the selected item and if the user click  cancle nothing happend.

i could’nt do that in javascript and i thought it’ll be easier with page method but it does’nt work too

this’s my html code

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/ConfirmScripts.js" />
</Scripts>
</asp:ScriptManager>

<td valign="top">
<asp:Button ID="AddModelLnk" runat="server" OnClick="AddModelLnk_Click" ValidationGroup="5" Text=">>" CommandName="Insert"/><br />
<asp:Button ID="RemoveModelLnk" runat="server" OnClick="RemoveModelLnk_Click" Text="<<" CommandName="Delete" OnClientClick="ShowConfirm(‘NewModelLstBx’)"/></td>
<td>
<asp:ListBox runat="server" ID="NewModelLstBx" DataSourceID="NewModelDataSource" DataTextField="Model" DataValueField="ModelID" Width="200px" Height="150px"></asp:ListBox>
<asp:ObjectDataSource ID="NewModelDataSource" runat="server" TypeName="DataBaseComponent.ModelFunctions" SelectMethod="GetBrandModels">
<SelectParameters>
<asp:ControlParameter ControlID="NewBrandDL" PropertyName="SelectedValue" Name="BrandID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

and this’s my code behind class with the page method and the button click event

public partial class _Default : System.Web.UI.Page
{
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static string ShowConfirmDelete (string ListBoxId)
{
Page web = new _Default();
ListBox ListBx=(ListBox) web.FindControl(ListBoxId);
return ListBx.SelectedItem.Text;
}

protected void RemoveModelLnk_Click(object sender, EventArgs e)
{
if (NewModelLstBx.SelectedItem == null) NewBrandModelInfoLbl.Text = "select the model first";
else
{
if (ModelFunc.HasMatchesDevices(Convert.ToInt32(NewModelLstBx.SelectedValue)))
NewBrandModelInfoLbl.Text = "u cant delete this model";
else
{
int result = ModelFunc.DeleteModel(Convert.ToInt32(NewModelLstBx.SelectedValue));
if (result > 0)
{
NewModelLstBx.DataBind();
ModelDL.DataBind();
DropDownList InsertModelDL = (DropDownList)InsertNewDeviceFV.FindControl("InsertModelDL");
InsertModelDL.DataBind();
AddModelTxt.Text = "";
NewBrandModelInfoLbl.ForeColor = System.Drawing.Color.Green;
NewBrandModelInfoLbl.Text = "model has deleted succssesfuly";
}
}
}
}

and this’s my javascript code

function ShowConfirm(LisBoxId) {
PageMethods.ShowConfirmDelete(ListBoxId, OnSucceeded);
}
function OnSucceeded(result) {
confirm("are you sure that you want to delete "+result+" model");
}

i really dont know where exactly my mistake is ..is’t in the client code or i did mistake in the page method,, or where exactly

can u help me plzzzzz

Leave a Reply