c# - Get exception in web method when trying to assign value in page attributes -
when i'm trying assign value in textbox through webmethod on page got exception
my html code
<form id="form1" runat="server"><br /><br /> <asp:scriptmanager id="src" runat="server" enablepagemethods="true"></asp:scriptmanager> <asp:updatepanel id="up1" runat="server"> <contenttemplate> <div class="row"> <div class="col-md-6"> <asp:button id="btnchangemake" runat="server" text="change make" onclientclick="changemake()"/><br /> <asp:textbox id="txtmake" runat="server"></asp:textbox> </div> </div> </contenttemplate> </asp:updatepanel>
javascript code
function changemake() { pagemethods.changemaketext("devil", onsucess, onerror); function onsucess(result) { alert(result); } function onerror(result) { alert(result); }
}
c# code
[webmethod] public static void changemaketext(string datavalue) { if (httpcontext.current != null) { page page = (page)httpcontext.current.handler; textbox txtmake = (textbox)page.findcontrol("txtmake"); txtmake.text = datavalue; } }
exception @ 'txtmake.text = datavalue;'
an exception of type 'system.nullreferenceexception' occurred in app_web_0lr1r3rs.dll not handled in user code
additional information: object reference not set instance of object.
you not access page controls in web method rather can send value calling method in javascript , assign txtmake.
c# code
public static string changemaketext(string datavalue) { //your code return datavalue; }
javascript code
function changemake() { pagemethods.changemaketext("devil", onsucess, onerror); function onsucess(result) { document.getelementbyid("<%=txtmake.clientid%>").value = result; } }
you have used ajax updatepanel
bind click event button btnchangemake (by double click in desginer) , change value in server side click handler.
void btnchangemake_click(object sender, eventargs e) { txtmake.text = datavalue; }
Comments
Post a Comment