c# - DetailsView paging is enabled/causing postback when the textbox used for searching has invalid content and client-side shows the error -
very new asp.net development hope not of silly question.
i have web page (framework 3.5) detailsview control bound sql , reads records via stored procedure. allowpaging = true on control because more 1 record may found.
it uses text box control - value entered user - pass sp.
the text box has regular expression validation control highlights when input invalid.
i doing follows:
- enter valid data in text box , hit 'enter' - records found detailsview shows me first record plus numbered paging buttons (correct)
- change value in textbox invalid, , tab out of textbox - validation control highlights error (correct)
- press 'enter' - nothing happens, invalid (correct)
- click on 1 of numbered paging buttons in detailsview - postback occurs. validation control has not prevented paging taking place. prevent postback/response other forcing user correct invalid data in text box.
am sure there should easy way handle , have tried various options not getting there. in advance.
basically here validation doesn't happens when click on numbered paging buttons of detailsview.
preventing complete postback requires page checks client side validation. make validation occur on server side,call page.validate() in pageindexchanging event of detailsview. if page not valid prevent paging happening.
note i: can call page.validate() inside page_load event also. it's not necessary should call event in detailsview.pageindexchanging event only.
note ii: in case want prevent paging occur , don't want use pagertemplates, use server side validation. postback happen, although if page not valid, paging not occur.
start using <pagertemplates> paging in detailsview , set causesvalidation attribute true pager buttons. below settings of commandname & commandargument automatically take care of paging. see msdn.
<pagertemplate> <asp:linkbutton id="previousbutton" text=" previous_" commandname="page" commandargument="prev" causesvalidation="true" runat="server"/> <asp:linkbutton id="nextbutton" text="next_" commandname="page" commandargument="next" causesvalidation="true" runat="server"/> </pagertemplate> markup of detailsview:
<asp:detailsview runat="server" id="empdetails" onpageindexchanging="empdetails_pageindexchanging" ... /> event handler
protected void empdetails_pageindexchanging(object sender, detailsviewpageeventargs e) { page.validate(); if (!page.isvalid) e.cancel = true;// prevent paging }
Comments
Post a Comment