5

好的,我有一个 Gridview 控件,用于编辑数据库中的记录。我也参数化了查询。这是代码:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
    AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" 
    DataSourceID="AccessDataSource1"
    AutoGenerateColumns="False" DataKeyNames="ID"
    AlternatingRowStyle-BackColor="Gray"  
    AlternatingRowStyle-CssClass="editGridFormat" RowStyle-CssClass="editGridFormat"        
    RowStyle-VerticalAlign="Top" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">

    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
            ReadOnly="True" SortExpression="ID" />
        <asp:BoundField DataField="BlogTitle" HeaderText="BlogTitle" 
            SortExpression="BlogTitle" />
        <asp:ImageField DataImageUrlField="Image" HeaderText="Image"
            DataImageUrlFormatString="~/PlaceImages/{0}" ControlStyle-CssClass="editPhotoGridFormat"
            AlternateText="Something went wrong" 
            NullDisplayText="No picture on file" />
        <asp:TemplateField headertext="PicText">
            <EditItemTemplate>
                <asp:TextBox id="PicTextBox" runat="server" text='<%# Eval("PicText")%>' textmode="MultiLine" height="300px" width="300px" />
            </EditItemTemplate>
            <ItemTemplate>
                <%# Eval("PicText")%>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="TravelDate" HeaderText="TravelDate" SortExpression="TravelDate" />
        <asp:TemplateField headertext="BeginText">
            <EditItemTemplate>
                <asp:TextBox id="BeginTextBox" runat="server" text='<%# Eval("BeginText")%>' textmode="MultiLine" height="300px" width="300px" />
            </EditItemTemplate>
            <ItemTemplate>
                <%# Eval("BeginText")%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Caption" HeaderText="Caption" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" 
            SortExpression="Country" />
        <asp:TemplateField headertext="EndText">
            <EditItemTemplate>
                <asp:TextBox id="EndTextBox" runat="server" text='<%# Eval("EndText")%>' textmode="MultiLine" height="300px" width="300px" />
            </EditItemTemplate>
            <ItemTemplate>
                <%# Eval("EndText")%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

</asp:GridView>

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/TravelJoansDB.mdb" 
    SelectCommand="SELECT * FROM [Table2]" 
    DeleteCommand="DELETE FROM Table2 WHERE [ID] = ?"
    UpdateCommand="UPDATE Table2 SET [BlogTitle] = ?, 
                                     [Image] = ?, 
                                     [PicText] = ?, 
                                     [TravelDate] = ?, 
                                     [BeginText] = ?, 
                                     [Caption] = ?, 
                                     [City] = ?, 
                                     [Country] = ?, 
                                     [EndText] = ? 
                                     WHERE [ID] = ? " >

    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="BlogTitle" Type="String" />
        <asp:Parameter Name="Image" Type="String" />
        <asp:Parameter Name="PicText" Type="String" />
        <asp:Parameter Name="TravelDate" DbType="Date" />
        <asp:Parameter Name="BeginText" Type="String" />
        <asp:Parameter Name="Caption" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="Country" Type="String" />
        <asp:Parameter Name="EndText" Type="String" />
    </UpdateParameters>
</asp:AccessDataSource>

不确定它是否与 TemplateField 控件或 Parameter 类型为字符串有关。我提到 Type 属性的原因是因为在我的 Access 数据库中,它们属于备忘录类型,但我能看到的唯一具有一半可比性的是字符串类型。我敢打赌,字符串类型不足以容纳备忘录字段,但我不知道还会发生什么。任何帮助将不胜感激。

4

1 回答 1

6

备注字段没有任何问题。它的edititemtemplatefields,您没有绑定数据。更改EvalBind,例如,更改此:

<EditItemTemplate>
    <asp:TextBox id="PicTextBox" runat="server" text='<%# Eval("PicText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>

对此:

<EditItemTemplate>
    <asp:TextBox id="PicTextBox" runat="server" text='<%# Bind("PicText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>

改变这个:

<EditItemTemplate>
    <asp:TextBox id="BeginTextBox" runat="server" text='<%# Eval("BeginText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>

对此:

<EditItemTemplate>
    <asp:TextBox id="BeginTextBox" runat="server" text='<%# Bind("BeginText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>

并改变这个:

<EditItemTemplate>
    <asp:TextBox id="EndTextBox" runat="server" text='<%# Eval("EndText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>

对此:

<EditItemTemplate>
    <asp:TextBox id="EndTextBox" runat="server" text='<%# Bind("EndText")%>' textmode="MultiLine" height="300px" width="300px" />
</EditItemTemplate>

我已经使用 access db 对其进行了测试。希望能帮助到你!

于 2013-10-22T04:55:11.920 回答