我有一個網格,下面是我爲它編寫的代碼。在我的這個專欄的網格中,即<asp:BoundField DataField = "IsNOWEnabled" HeaderText = "Enabled/Disabled" HtmlEncode = "true"/>
,來自數據庫的1或0即將到來。我希望在綁定網格的時候,通過啓用替換文本1,爲此列替換文本1,禁用此列,以便在網格綁定時顯示1作爲已啓用,0代表禁用列已啓用/禁用。將1更改爲啓用,將0更改爲禁用,同時綁定列的網格
請指導我,我是如何實現這一目標的。
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" Width = "550px" AutoGenerateColumns = "false" AlternatingRowStyle-BackColor = "#C2D69B" HeaderStyle-BackColor = "green" AllowPaging ="true" OnPageIndexChanging = "OnPaging" PageSize = "10" >
<Columns>
<asp:BoundField DataField = "NatureOfWorkID" HeaderText = "Nature Of WorkID" HtmlEncode = "true" Visible="false" />
<asp:BoundField DataField = "NatureOFWorkName" HeaderText = "Nature Of Work Name" HtmlEncode = "true" />
<asp:BoundField DataField = "IsNOWEnabled" HeaderText = "Enabled/Disabled" HtmlEncode = "true"/>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "NatureOfWorkID">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text = "Edit" OnClick = "Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#C2D69B" />
</asp:GridView>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick = "Add" />
<asp:Panel ID="pnlAddEdit" runat="server" CssClass="modalPopup" style = "display:none">
<asp:Label Font-Bold = "true" ID = "Label4" runat = "server" Text = "Nature Of Work Details" ></asp:Label>
<br />
<table align = "center">
<tr>
<td>
<asp:Label ID = "Label2" runat = "server" Text = "Nature Of Work Name" ></asp:Label>
</td>
<td>
<asp:TextBox ID="txtNOWname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:RadioButton id="RBEnable" Text="Enabled" runat="server"/>
<br>
<asp:RadioButton id="RBDisable" Text="Disabled" runat="server"/>
<br>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick = "Save" />
</td>
<td>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick = "return Hidepopup()"/>
</td>
</tr>
</table>
</asp:Panel>
<asp:LinkButton ID="lnkFake" runat="server"></asp:LinkButton>
<cc1:ModalPopupExtender ID="popup" runat="server" DropShadow="false"
PopupControlID="pnlAddEdit" TargetControlID = "lnkFake"
BackgroundCssClass="modalBackground">
</cc1:ModalPopupExtender>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID = "GridView1" />
<asp:AsyncPostBackTrigger ControlID = "btnSave" />
</Triggers>
後面的代碼是: -
private String strConnString = ConfigurationManager.ConnectionStrings["TempConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
string strQuery = "select NatureOfWorkID,NatureOfWorkName,IsNOWEnabled" +
" from NatureOfWork";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
}
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
this.BindData();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}