当我添加产品类别时,详细视图不会自行更新。无论如何我能做到吗?
这是添加类别的代码
<div class="container "style="background-color:lavender">
<h2>Product Category </h2>
<asp:DetailsView ID="DetailsViewCategory" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="CategoryId" DataSourceID="SqlDataSourceCategory" DefaultMode="Insert" Height="83px" Width="360px" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="CategoryId" HeaderText="CategoryId" InsertVisible="False" ReadOnly="True" SortExpression="CategoryId" />
<asp:BoundField DataField="CategoryName" HeaderText="Category Name" SortExpression="CategoryName" />
<asp:CommandField CancelText="" ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#284775" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>
<br />
</div>
这是我的详细视图的代码
<asp:DetailsView ID="DetailsViewProduct" runat="server" AutoGenerateRows="False" DataKeyNames="ProductId" DataSourceID="SqlDataSourceProduct" DefaultMode="Insert" Height="16px" Width="46%" OnItemInserted="InsertProduct" HorizontalAlign="Center">
<Fields>
<asp:TemplateField HeaderText="Picture">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:FileUpload ID="FileUploadPicture" runat="server" Height="22px" Width="400px" CssClass="center-block" ForeColor="Black"/>
</InsertItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductId" HeaderText="ProductId" SortExpression="ProductId" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ProductName" HeaderText="Name" SortExpression="ProductName"/>
<asp:TemplateField HeaderText="Description" SortExpression="ProductDescription">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ProductDescription") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Height="42px" Text='<%# Bind("ProductDescription") %>' TextMode="MultiLine" Width="400px" CssClass="center-block"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductDescription") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="ProductCategory">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ProductCategory") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:SqlDataSource ID="SqlDataSourcePC" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [ProductCategory]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSourcePC" DataTextField="CategoryName" DataValueField="CategoryId" Height="18px" SelectedValue='<%# Bind("ProductCategory") %>' Width="400px" CssClass="center-block">
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductCategory") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand" SortExpression="ProductBrand">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("ProductBrand") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:SqlDataSource ID="SqlDataSourceBrand1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT * FROM [ProductBrand]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SqlDataSourceBrand1" DataTextField="ProductBrand" DataValueField="BrandId" SelectedValue='<%# Bind("ProductBrand") %>' Width="400px" CssClass="center-block">
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductBrand") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductPrice" HeaderText="Price (RM)" SortExpression="ProductPrice" />
<asp:BoundField DataField="ProductQty" HeaderText="Stock Quantity" SortExpression="ProductQty" />
<asp:CommandField CancelText="" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
答案 0 :(得分:1)
是数据控件不会自动更新,因为它们需要页面重新加载和数据绑定。在页面加载事件中调用控件的DataBind()方法
DetailsViewCategory.DataBind();