FormView的EditItemTemplate模板中有3个TextBox,并且已经绑定到相对应的字段,为什么在插入时,我在TextBox输入值后,返回ItemTemplate时却显示为空呢?

解决方案 »

  1.   

    ItemTemplate绑定了吗?有很多需要额外做的步骤那,你一定少做了某一步
      

  2.   

    给你一个例子<%@ Page language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html  >
      <head runat="server">
        <title>FormView Example</title>
    </head>
    <body>
        <form id="form1" runat="server">      <h3>FormView Example</h3>      <asp:formview id="EmployeeFormView"
            datasourceid="EmployeeSource"
            allowpaging="true"
            datakeynames="EmployeeID"
            emptydatatext="No employees found."  
            runat="server">        <rowstyle backcolor="LightGreen"
              wrap="false"/>
            <editrowstyle backcolor="LightBlue"
              wrap="false"/>        <itemtemplate>
              <table>
                <tr>
                  <td rowspan="4">
                    <asp:image id="EmployeeImage"
                      imageurl='<%# Eval("PhotoPath") %>'
                      alternatetext='<%# Eval("LastName") %>' 
                      runat="server"/>
                  </td>
                  <td colspan="2">
                      &nbsp; 
                  </td>
                </tr>
                <tr>
                  <td>
                    <b>Name:</b>
                  </td>
                  <td>
                    <%# Eval("FirstName") %> <%# Eval("LastName") %>
                  </td>
                </tr>
                <tr>
                  <td>
                    <b>Title:</b>
                  </td>
                  <td>
                    <%# Eval("Title") %>
                  </td>
                </tr>
                <tr>
                  <td colspan="2">
                    <asp:linkbutton id="Edit"
                      text="Edit"
                      commandname="Edit"
                      runat="server"/> 
                  </td>
                </tr>
              </table>       
            </itemtemplate>
            <edititemtemplate>
              <table>
                <tr>
                  <td rowspan="4">
                    <asp:image id="EmployeeEditImage"
                      imageurl='<%# Eval("PhotoPath") %>'
                      alternatetext='<%# Eval("LastName") %>' 
                      runat="server"/>
                  </td>
                  <td colspan="2">
                      &nbsp; 
                  </td>
                </tr>
                <tr>
                  <td>
                    <b>Name:</b>
                  </td>
                  <td>
                    <asp:textbox id="FirstNameUpdateTextBox"
                      text='<%# Bind("FirstName") %>'
                      runat="server"/>
                    <asp:textbox id="LastNameUpdateTextBox"
                      text='<%# Bind("LastName") %>'
                      runat="server"/>
                  </td>
                </tr>
                <tr>
                  <td>
                    <b>Title:</b>
                  </td>
                  <td>
                    <asp:textbox id="TitleUpdateTextBox"
                      text='<%# Bind("Title") %>'
                      runat="server"/> 
                  </td>
                </tr>
                <tr>
                  <td colspan="2">
                    <asp:linkbutton id="UpdateButton"
                      text="Update"
                      commandname="Update"
                      runat="server"/>
                    <asp:linkbutton id="CancelButton"
                      text="Cancel"
                      commandname="Cancel"
                      runat="server"/> 
                  </td>
                </tr>
              </table>       
            </edititemtemplate>       </asp:formview>      <!-- This example uses Microsoft SQL Server and connects  -->
          <!-- to the Northwind sample database. Use an ASP.NET     -->
          <!-- expression to retrieve the connection string value   -->
          <!-- from the Web.config file.                            -->
          <asp:sqldatasource id="EmployeeSource"
            selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
            updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title Where [EmployeeID]=@EmployeeID"
            connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
            runat="server"/>    </form>
      </body>
    </html>
      

  3.   

    Update 更新方法中需要一个@EmployeeID, 你好像在EditItemTemplate中没有绑定[EmployeeID]
      

  4.   

    我的代码如下: 
    <asp:FormView ID="Form_OpRsk_TB_EventDetail" runat="server" DataKeyNames="Id" DataSourceID="UserAddEventDetail" >
                    <EditItemTemplate>
                        <table>
                            <tr>
                                <td class="TitleCell">
                                    事件后续进展
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:TextBox ID="EventDescriptionTextBox" runat="server" Text='<%# Bind("EventDescription") %>'
                                        Columns="60" MaxLength="300" TextMode="MultiLine">
                                    </asp:TextBox></td>
                            </tr>
                            <tr>
                                <td class="TitleCell">
                                    采取相关措施
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:TextBox ID="EventActionTextBox" runat="server" Text='<%# Bind("EventAction") %>'
                                        Columns="60" MaxLength="300" TextMode="MultiLine">
                                    </asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td class="TitleCell">
                                    实际损失金额更新
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:TextBox ID="AmountRealChangeTextBox" runat="server" Text='<%# Bind("AmountRealChange","{0:N2}") %>'
                                        ToolTip="必须输入角分"></asp:TextBox>
                                    </td>
                            </tr>
                            <tr>
                                <td colspan="4">
                                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                                        Text="更新事件追踪">
                                    </asp:LinkButton>
                                    <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                        Text="取消追踪更新">
                                    </asp:LinkButton>
                                </td>
                            </tr>
                        </table>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <table>
                            <tr>
                                <td class="TitleCell">
                                    事件后续进展
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:TextBox ID="EventDescriptionTextBox" runat="server" Text='<%# Bind("EventDescription") %>'
                                        Columns="60" MaxLength="300" TextMode="MultiLine">
                                    </asp:TextBox></td>
                            </tr>
                            <tr>
                                <td class="TitleCell">
                                    采取相关措施
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:TextBox ID="EventActionTextBox" runat="server" Text='<%# Bind("EventAction") %>'
                                        Columns="60" MaxLength="300" TextMode="MultiLine">
                                    </asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td class="TitleCell">
                                    实际损失金额更新
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:TextBox ID="AmountRealChangeTextBox" runat="server" Text='<%# Bind("AmountRealChange","{0:N2}") %>'
                                        ToolTip="必须输入角分">
                                    </asp:TextBox>
                                    </td>
                            </tr>
                            <tr>
                                <td colspan="4">
                                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                        Text="保存事件追踪" Width="100px"></asp:LinkButton>
                                    <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                        Text="取消事件追踪" Width="100px"></asp:LinkButton>
                                </td>
                            </tr>
                        </table>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td class="TitleCell">
                                    事件后续进展
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:Label ID="EventDescriptionLabel" runat="server" Text='<%# Bind("EventDescription") %>' Width="127px"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td class="TitleCell">
                                    采取相关措施
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:Label ID="EventActionLabel" runat="server" Text='<%# Bind("EventAction") %>' Width="120px"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td class="TitleCell">
                                    实际损失金额更新
                                </td>
                                <td class="InputCell" colspan="3">
                                    <asp:Label ID="AmountRealChangeLabel" runat="server" Text='<%# Bind("AmountRealChange","{0:C2}") %>'>
                                    </asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td class="TitleCell" colspan="4">
                                    <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                                        Text="登记事件追踪" Width="100px" OnClick="NewButton_Click"></asp:LinkButton>
                                    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                                        Text="编辑事件追踪" Width="100px"></asp:LinkButton>
                                    <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                                        Text="提交事件追踪" Width="100px" OnClientClick="javascript:return(confirm('提交后将不能编辑。确认提交?'));">
                                    </asp:LinkButton>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                    <EmptyDataTemplate>
                        <h3>
                            没有未提交的事件追踪</h3>
                        <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                            Text="新建事件追踪" Width="100px">
                        </asp:LinkButton>
                    </EmptyDataTemplate>
                </asp:FormView>
            <br />
         
      

  5.   

    数据源如下: 
      <asp:SqlDataSource ID="Auth_TB_Operator" runat="server" ConnectionString="<%$ ConnectionStrings:CcisConnectionString %>"
                DataSourceMode="DataReader" ProviderName="<%$ ConnectionStrings:CcisConnectionString.ProviderName %>"
                SelectCommand="SELECT NULL AS [Id], NULL AS [Name], NULL AS [Caption] UNION SELECT [Id], [Name], [Caption] +'('+ [Name] +')' AS [Caption] FROM [Auth_TB_Operator]">
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="Department" runat="server" ConnectionString="<%$ ConnectionStrings:CcisConnectionString %>"
                DataSourceMode="DataReader" ProviderName="<%$ ConnectionStrings:CcisConnectionString.ProviderName %>"
                SelectCommand="SELECT Id, Caption, ManagerId FROM dbo.OpRsk_TB_Department"></asp:SqlDataSource>
            <asp:SqlDataSource ID="EventTypes" runat="server" ConnectionString="<%$ ConnectionStrings:CcisConnectionString %>"
                DataSourceMode="DataReader" ProviderName="<%$ ConnectionStrings:CcisConnectionString.ProviderName %>"
                SelectCommand="SELECT [Code], [Code]+':'+[Caption] AS Caption FROM [OpRsk_TB_EventType]">
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="Dic_OperationRiskLevel" runat="server" ConnectionString="<%$ ConnectionStrings:CcisConnectionString %>"
                DataSourceMode="DataReader" ProviderName="<%$ ConnectionStrings:CcisConnectionString.ProviderName %>"
                SelectCommand="SELECT [KeyId], [Caption] FROM [Dic_VR_Type_Value] WHERE ([TypeName] = 'OperationRiskLevel')">
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="UserAddEventDetail" runat="server" ConnectionString="<%$ ConnectionStrings:CcisConnectionString %>"
                SelectCommand="SELECT [Id], [EventId], [EventDescription], [EventAction], [Recorder], [RecordDate], [Reviewer], [ReviewDate], [AmountRealChange] FROM [OpRsk_TB_EventDetail] WHERE [Recorder]=@UserId AND [EventId]=@EventId And [Reviewer] IS NULL"
                DeleteCommand="UPDATE dbo.OpRsk_TB_EventDetail SET Reviewer=@Reviewer WHERE Id=@Id"
                InsertCommand="INSERT INTO dbo.OpRsk_TB_EventDetail(EventId, EventDescription, EventAction, Recorder, AmountRealChange) VALUES (@EventId,@EventDescription,@EventAction,@Recorder,CASE WHEN RTRIM(@AmountRealChange)='' THEN NULL ELSE @AmountRealChange END)"
                UpdateCommand="UPDATE dbo.OpRsk_TB_EventDetail SET EventDescription =@EventDescription, EventAction =@EventAction, AmountRealChange = CASE WHEN RTRIM(@AmountRealChange)='' THEN NULL ELSE @AmountRealChange END WHERE Id=@Id">
                <DeleteParameters>
                    <asp:ControlParameter ControlID="Form_OpRsk_TB_EventDetail" Name="Id" PropertyName="SelectedValue" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:ControlParameter ControlID="Form_OpRsk_TB_EventDetail" Name="Id" PropertyName="SelectedValue" />
                </UpdateParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="CurrentUserId" Name="UserId" PropertyName="Text" />
                    <asp:ControlParameter ControlID="FormViewTrackInfo" Name="EventId" PropertyName="SelectedValue" />
                </SelectParameters>
                <InsertParameters>
                    <asp:ControlParameter ControlID="FormViewTrackInfo" Name="EventId" PropertyName="SelectedValue" />
                    <asp:ControlParameter ControlID="CurrentUserId" Name="Recorder" PropertyName="Text" />
                </InsertParameters>
            </asp:SqlDataSource>
            <asp:Label ID="CurrentUserId" runat="server"></asp:Label>