多种控制类型
问题描述:
用:SELECT ID,名称,标志1从表多种控制类型
是否有可能动态地显示任一按钮或基于标记1字段的比特值在一列中一个CheckBox ?
换句话说: 如果Flag1 = true,则将复选框显示为选中状态和只读状态。 如果Flag1 = false show click with click event并将行的id传递给handler。
我需要勾选哪个事件才能进行更改?数据绑定? 如何设置.aspx或.cs中的复选框/按钮?通常我使用的ItemTemplate与GridView的...
<asp:TemplateField HeaderText="Under Warranty" >
<ItemTemplate>
<asp:Button ID="WButton" runat="server" CommandName="AddWarrantyTrackerItem" CommandArgument = "<%# (Container.DataItemIndex) %>" Text="Track Warranty" Visible="false" />
<asp:CheckBox ID="WFlag" runat="server" Visible="false" readonly="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Depot" >
<ItemTemplate>
<asp:Button ID="DButton" runat="server" CommandName="AddDepotTrackerItem" CommandArgument = "<%# (Container.DataItemIndex) %>" Text="Track Depot" Visible="false" />
<asp:CheckBox ID="DFlag" runat="server" Visible="false" readonly="true" />
</ItemTemplate>
</asp:TemplateField>
代码隐藏:
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox WFlag = (CheckBox)e.Row.FindControl("WFlag");
Button WButton = (Button)e.Row.FindControl("WButton");
CheckBox DFlag = (CheckBox)e.Row.FindControl("DFlag");
Button DButton = (Button)e.Row.FindControl("DButton");
var record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
bool flagW = (bool)record.GetValue(14);
bool flagD = (bool)record.GetValue(15);
int reqnum = (int)record.GetValue(0);
if (flagW == false)
{
WButton.Visible = true;
WFlag.Visible = false;
}
else
{
WButton.Visible = false;
WFlag.Visible = true;
WFlag.Checked = true;
}
if (flagD == false)
{
DButton.Visible = true;
DFlag.Visible = false;
}
else
{
DButton.Visible = false;
DFlag.Visible = true;
DFlag.Checked = true;
}
}
}
答
是的,你可以使用一个TemplateField
和RowDataBound
切换可视性:
ASPX:
<Columns>
<asp:TemplateField HeaderText="Flagged">
<ItemTemplate>
<asp:Button ID="ButtonFlag" runat="server" Text="flag it" Visible="true" />
<asp:CheckBox ID="CheckFlag" runat="server" Checked="true" Visible="false" />
</ItemTemplate>
</asp:TemplateField>
Codebehind:
protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkFlag = (CheckBox)e.Row.FindControl("CheckFlag");
Button buttonFlag = (Button)e.Row.FindControl("ButtonFlag");
DataRow row = ((DataRowView)e.Row.DataItem).Row;
bool flagged = row.Field<bool>("Flag1");
buttonFlag.Visible = !flagged;
chkFlag.Visible = flagged;
}
}
DataRow row =((DataRowView)e.Row.DataItem).Row;错误:无法将类型为“System.Data.Common.DataRecordInternal”的对象转换为键入“System.Data.DataRowView”。 – user1431356
@ user1431356:你现在使用'DataReader'作为数据源吗?尝试'var rd =(System.Data.Common.DbDataRecord)e.Row.DataItem;'。如果这样做,您可以使用['DbDataRecord'](http://msdn.microsoft.com/en-us/library/system.data.common.dbdatarecord.aspx)的方法获取所有字段。 –
谢谢!这工作! – user1431356