c# - Why do ASP elements in templates not exist in current context? -
in aspx file, have insertitemtemplate inside of asp:formview. template consists of form made asp:textbox, droplists, etc. form going nothing take user input, pass stored procedure on sql server , insert data.
i've tested sp separately, , works fine. problem asp formviews , templates make no sense me. it's data input in fields doesn't exist it's not passed sp nothing inserted. when add parameters codebehind, told doesn't exist in current context.
if copy 1 of textbox, unchanged, outside formview, magically exists in current context. forgive ignorance, what's point of insert template if can't pass info inserted?
code (abbreviated brevity):
<asp:formview id="patientformview" datasourceid="sqldatasource1" gridlines="both" runat="server" defaultmode="insert" horizontalalign="left" width="800"> ... provider name: <asp:textbox id="npi" runat="server" width="257px" text='<%#bind("npi")%>' font-bold="true"></asp:textbox> patient mrn: <asp:textbox id="acctno" runat="server" readonly="true" width="257px" text='<%#bind("acctno")%>'></asp:textbox> </insertitemtemplate>
and codebehind:
protected void sqldatasource1_inserting(object sender, sqldatasourcecommandeventargs e) { e.command.parameters["@npi"].value = npi.text.tostring(); e.command.parameters["@acctno"].value = acctno.text.tostring(); }
that's npi , acctno not exist in current context.
however, if copy either of asp texboxes out of formview, exist. suppose eliminate entire template, want understand context issue.
thanks in advance. i've tried find explanation this, , of find doesn't apply or refers using .design file, don't have on pages in project.
you're using <%# bind("...") %>
within template, don't need set parameters manually. if debug sqldatasource1_inserting
method, you'll notice parameters populated values bound controls.
the controls within templates not directly accessible code-behind because might not unique. example, might have textbox
id of "npi" in both insertitemtemplate
, edititemtemplate
.
Comments
Post a Comment