【问题标题】:Databind() from code behind never reaches the database来自后面代码的 Databind() 永远不会到达数据库
【发布时间】:2017-01-20 05:18:00
【问题描述】:

我的 FormView 不会 DataBind。我没有收到任何错误,所有元素都正确找到,当我单步执行代码时,一切看起来都像预期的那样工作。设置了select参数,FormView为DataBound。

但是没有返回任何数据,并且我的数据库日志显示,本应成为 DataBound 的过程从未被触及。

更新面板

 <asp:updatepanel ID="upnlMixingTankInfo" runat="server">
<ContentTemplate>
 <asp:formview id="fvMixingTankInfo" runat="server" datasourceid="SqlDataSourceMixingTankInfo">
   <ItemTemplate>
         <asp:label runat="server">Vessel Capacity:</asp:label>
         <asp:TextBox ID="vesselCapacity" runat="server" class="form-control" Text='<%# Bind("fldVesselCapacity")%>'></asp:TextBox>
   </ItemTemplate>
  </asp:formview>
</ContentTemplate>

代码背后:

SourceDropDownList = sender
  upnlMixingTankInfo = CType(SourceDropDownList.Parent.FindControl("upnlMixingTankInfo"), UpdatePanel)
        fvTankInfo = CTYPE(upnlMixingTankInfo.FindControl("fvMixingTankInfo"), FormView)
        If Not IsNothing(SourceDropDownList.SelectedValue) Then
           SqlDataSourceMixingTankInfo.SelectParameters.Add("TankName", DropDownListEquipmentList.SelectedValue)
        End If
        fvTankInfo.Databind()

SQLDATSOURCE:

         <asp:SqlDataSource ID="SqlDataSourceMixingTankInfo" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ZMConnectionString %>"   
    SelectCommand="EXEC stpWebGetMixTankCapacity @TankName" >
    <SelectParameters>
      <asp:Parameter Name="TankName" defaultvalue=""/>
    </SelectParameters>

【问题讨论】:

  • 您是否在SqlDataSource 中设置SelectCommandType="StoredProcedure" 而不是使用文本命令类型?您也可以使用&lt;asp:ControlParameter ControlID="DropDownListEquipmentList" PropertyName="SelectedValue" Name="TankName" Type="String" /&gt; 将下拉列表值直接绑定到程序
  • 感谢 Tetsuya 的出色建议!

标签: c# asp.net .net vb.net sqldatasource


【解决方案1】:

SqlDataSource 改动小,大功告成,我用自己的sp测试过,根据你的使用情况修改。

更新面板和表单视图

<asp:UpdatePanel ID="upnlMixingTankInfo" runat="server">
    <ContentTemplate>
        <asp:FormView ID="fvMixingTankInfo" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server">Vessel Capacity:</asp:Label>
                <asp:TextBox ID="vesselCapacity" runat="server" class="form-control" Text='<%# Bind("Name")%>'></asp:TextBox>
            </ItemTemplate>
        </asp:FormView>
    </ContentTemplate>
</asp:UpdatePanel>

代码隐藏,我已经在加载时进行了测试,您可以在任何地方使用相同的代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataSource1.SelectParameters.Add("Id", "10");
    }

SqlDataSource 有变化:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="GetImages" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

效果很好,在这里提交之前我已经测试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2020-04-12
    • 2020-11-14
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多