我有一个添加项目的表单,每个项目都可以连接到多个公司,所以我在页面底部有一个gridview来显示和指定与项目相关的公司。
现在我在gridview下使用这个用途:
<asp:GridView ID="gvCompany" runat="server" AutoGenerateColumns="false" AllowPaging="True" AllowSorting="true" PageSize="20" DataSourceID="odsCompany" DataKeyNames="ID" PagerStyle-CssClass="gridpaging" CssClass="table table-striped table-condensed table-bordered ">
<Columns>
<asp:TemplateField HeaderText="<%$ Resources:Labels, Select %>">
<ItemTemplate>
<input type="checkbox" id="chbSelect" name="select[]" value="<%# DataBinder.Eval(Container.DataItem, "ID") %>" />
</ItemTemplate>
<ItemStyle Width="20px" HorizontalAlign="Center" CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:BoundField HeaderText="<%$ Resources:Labels,Code %>" DataField="Code" SortExpression="Code">
<ItemStyle Width="20%" />
</asp:BoundField>
<asp:BoundField HeaderText="<%$ Resources:Labels,Name %>" DataField="Name" SortExpression="Name">
<ItemStyle Width="65%" />
</asp:BoundField>
<asp:TemplateField HeaderText="<%$ Resources:Labels, DefaultCompany %>">
<ItemTemplate>
<input type="radio" name="Default[]" value="<%# DataBinder.Eval(Container.DataItem, "ID") %>" />
</ItemTemplate>
<ItemStyle Width="20px" HorizontalAlign="Center" CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<%= Resources.Messages.NoCompanyExist %>
</EmptyDataTemplate>
<PagerSettings Mode="NumericFirstLast" />
</asp:GridView>
第一栏用于选择公司,最后一栏用于选择一家默认公司。
但我不知道如何处理在编辑模式下显示所选公司(检查该公司的第一栏)或这种方式......?有什么想法吗?
答案 0 :(得分:0)
我最终可以通过将我的代码更改为以下目的达到上述目的:
<asp:GridView ID="gvCompany" runat="server" AutoGenerateColumns="false" AllowPaging="True" AllowSorting="true" PageSize="20" DataSourceID="odsCompany" DataKeyNames="ID" PagerStyle-CssClass="gridpaging" CssClass="table table-striped table-condensed table-bordered " OnRowCommand="gvCompany_RowCommand" OnRowDataBound="gvCompany_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="<%$ Resources:Labels, Select %>">
<ItemTemplate>
<asp:CheckBox ID="chbSelected" runat="server" />
</ItemTemplate>
<ItemStyle Width="20px" HorizontalAlign="Center" CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
<asp:BoundField HeaderText="<%$ Resources:Labels,Code %>" DataField="Code" SortExpression="Code">
<ItemStyle Width="20%" />
</asp:BoundField>
<asp:BoundField HeaderText="<%$ Resources:Labels,Name %>" DataField="Name" SortExpression="Name">
<ItemStyle Width="65%" />
</asp:BoundField>
<asp:TemplateField HeaderText="<%$ Resources:Labels, DefaultCompany %>">
<ItemTemplate>
<asp:RadioButton ID="rbDefault" runat="server" AutoPostBack="True" OnCheckedChanged="rbDefault_CheckedChanged" />
</ItemTemplate>
<ItemStyle Width="20px" HorizontalAlign="Center" CssClass="center" />
<HeaderStyle CssClass="center" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<%= Resources.Messages.NoCompanyExist %>
</EmptyDataTemplate>
<PagerSettings Mode="NumericFirstLast" />
</asp:GridView>
并在代码中使用以下事件和方法(假设gridview是有界的):
protected void gvCompany_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (editType == eEditType.Edit)
{
int productId;
int companyId;
e.Row.Cells[0].Enabled = false;
if (Int32.TryParse(Request.QueryString["Edit"], out productId))
{
companyId = Convert.ToInt32(gvCompany.DataKeys[e.Row.RowIndex].Value);
if (Providers.CompanyToProject.Find(string.Format("ProjectID={0} AND CompanyID={1}", productId, companyId)).Count == 1)
((CheckBox)e.Row.Cells[0].FindControl("chbSelected")).Checked = true;
if (Providers.CompanyToProject.Find(string.Format("ProjectID={0} AND CompanyID={1} AND IsDefault={2}", productId, companyId, true)).Count == 1)
((RadioButton)e.Row.Cells[3].FindControl("rbDefault")).Checked = true;
}
}
}
}
protected void rbDefault_CheckedChanged(object sender, EventArgs e)
{
DiselectRadioButtons();
((RadioButton)sender).Checked = true;
}
private void DiselectRadioButtons()
{
foreach (GridViewRow gvr in gvCompany.Rows)
((RadioButton)gvr.FindControl("rbDefault")).Checked = false;
}
并且它对我有用,我希望这对那些有同样问题的人有所帮助......