我有一个数据绑定到通用列表的网格。我编辑并需要将更改保留到db。
问题是,当我点击提交按钮时,它会进行PostBack。并将gridview的数据源设置为null。
我已经尝试将数据源存储在视图状态ViewState [“data”]中,该状态很好但在网格上进行更改时,ViewState中的数据没有更新。
有什么想法吗?
以下是GridView的代码:
<asp:GridView ID="dgMerchantConfiguration" runat="server"
AutoGenerateColumns="False"
CausesValidation="False" CustomPagerCssClass="" HeaderHeight="30px" NumberOfPagesHeight="15px"
NumberOfPagesWidth="40px" PagerHeight="15px" PagerWidth="40px" ShowDataGridCustomPaging="None"
SortHeight="15px" SortWidth="70px" BackColor="White" BorderColor="#999999" BorderWidth="1px"
CellPadding="1" Font-Size="Smaller" GridLines="Vertical" Width="100%"
onrowdatabound="dgMerchantConfiguration_RowDataBound" ViewStateMode="Enabled" EnableViewState="False">
<SelectedRowStyle Font-Bold="True" ForeColor="White" BackColor="#f80000"></SelectedRowStyle>
<AlternatingRowStyle BackColor="Gainsboro"></AlternatingRowStyle>
<RowStyle ForeColor="Black" BackColor="#EEEEEE"></RowStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#bd0000" HorizontalAlign="Center"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="CommissionType" HeaderText="Commission Type">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Select">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Checked='<%# Bind("Selected") %>' OnCheckedChanged="chkSelect_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Override">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkOverride" runat="server" Checked='<%# Bind("Default") %>' OnCheckedChanged="chkOverride_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comission Structure">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:DropDownList ID="drpCommissionStructure" runat="server" Width="200px" OnSelectedIndexChanged="drpCommissionStructure_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Min">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtMin" runat="server" Width="50px" Text='<%# Bind("Min") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Max">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtMax" runat="server" Width="50px" Text='<%# Bind("Max") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fixed">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtFixed" runat="server" Width="50px" Text='<%# Bind("Fixed") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost Per Unit">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtCostPerUnit" runat="server" Width="50px" Text='<%# Bind("CostPerUnit") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Value">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtUnitValue" runat="server" Width="50px" Text='<%# Bind("UnitValue") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Excluded Amount">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtExclAmount" runat="server" Width="50px" Text='<%# Bind("ExcludedAmount") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Percentage">
<HeaderStyle HorizontalAlign="Center" CssClass="NormalBold"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" CssClass="Normal"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtPercentage" runat="server" Width="50px" Text='<%# Bind("Percentage") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
如何设置数据源:
protected void ddlMerchants_SelectedIndexChanged(object sender, EventArgs e)
{
// Load the selected businesses config from the db
var t = client.GetMerchantCommissionStructure(new GetMerchantCommissionStructureRequest() { MerchantID = Int32.Parse(ddlMerchants.SelectedItem.Value) });
ViewState["data"] = t.Configurations;
dgMerchantConfiguration.DataSource = t.Configurations;
dgMerchantConfiguration.DataBind();
}
提前致谢!