如何在GridView中修改此复选框? [英] How to modify this checkbox inside the GridView?
问题描述
我具有以下数据库设计:
员工表:用户名,名称,职位名称,徽章编号,IsActive,部门代码
部门表:SapCode,部门快捷方式
我有一个GridView,我正在用它来添加,删除和更新/编辑员工信息.此信息是员工用户名,名称,徽章编号,JobTitle,IsActive和DivisionShortcut. *** IsActive是一个标志,用于指示该雇员是否有空或正在分配中***.我将其设为复选框,并且该列应显示两个值;活动和不活动.在编辑模式下,将显示复选框.如果选中,则表示该员工有空,否则处于非活动状态.
我编写了代码,一切正常,但是现在我面临两个问题,我不知道如何使它们与我的代码一起使用.
1. GridView显示True或False而不是Active和Inactive,我不知道为什么.
2.在编辑"模式下,将显示该复选框,并在其旁边显示值(活动"或不活动"),并且不应显示该复选框.我只希望显示该复选框.
**那么如何修改它们呢?**
ASP.NET代码:
I have the following database design:
Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
Divisions Table: SapCode, DivisionShortcut
And I have a GridView that I am using it to add, delete and update/edit the employees information. This information is employee Username, Name, BadgeNo, JobTitle, IsActive and the DivisionShortcut. ***IsActive is a flag that indicates if the employee is available or in an assignment***. I made it as a checkbox and the column should show two values; Active and Inactive. In the Edit mode, the Checkbox will be displayed. If it is checked, then it means the employee is avaiable, otherwise it is inactive.
I wrote the code and everything works fine, but now I am facing two problems and I don''t know how to make them work with my code.
1. The GridView shows True or False instead of Active and Inactive and I don''t know why.
2. In the Edit mode, the checkbox will be displayed and the values (Active or Inactive) will be displayed besides to it and it shouldn''t be displayed. I just want the checkbox to be displayed.
**So how to modify them?**
ASP.NET code:
<%-- GridView for User Management Subsystem --%>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="Username"
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" BorderWidth="1px" BackColor="#DEBA84"
CellPadding="3" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84">
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510"
HorizontalAlign="Center"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#A55129"></HeaderStyle>
<Columns>
<asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true"
EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png"
CancelImageUrl="Images/icons/cancel324.png" />
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<%# Eval("DivisionShortcut")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource"
DataTextField="DivisionShortcut" DataValueField="SapCode"
SelectedValue=''<%# Bind("DivisionCode")%>''>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Username" HeaderText="Network ID" ReadOnly="True"
SortExpression="Username" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeName" runat="server" Text=''<%# Bind("Name")%>'' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title">
<ItemTemplate>
<%# Eval("JobTitle")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJobTitle" runat="server" Text=''<%# Bind("JobTitle")%>'' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Badge No.">
<ItemTemplate>
<%# Eval("BadgeNo")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBadgeNo" runat="server" Text=''<%# Bind("BadgeNo")%>'' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active?">
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text=''<%# Eval("IsActive")%>''></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="isActive" runat="server"
AutoPostBack="true" OnCheckedChanged="isActive_OnCheckedChanged"
Checked=''<%# Convert.ToBoolean(Eval("IsActive")) %>''
Text=''<%# Eval("IsActive")%>''/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span önclick="return confirm(''Are you sure to Delete the record?'')">
<asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" />
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代码:
Code-behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// here you will select the IsActive column and modify it's text
Label isActive=new Label();
isActive = (Label) e.row.FindControl("lblIsActive");
// after getting the reference set its text
isActive.Text="Active or InActive";
}
}
//for updating the (IsActive) column using checkbox inside the GridView
protected void isActive_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkStatus = (CheckBox)sender;
GridViewRow gvrow = (GridViewRow)chkStatus.NamingContainer;
//Get the ID which is the NetworkID of the employee
string username = gvrow.Cells[2].Text;
bool status = chkStatus.Checked;
string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string updateIsActive = "UPDATE Employee SET IsActive = @IsActive WHERE Username = @Username";
SqlCommand cmd = new SqlCommand(updateIsActive, conn);
cmd.Parameters.AddWithValue("@IsActive", status);
cmd.Parameters.AddWithValue("@Username", username);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (SqlException se)
{
throw se;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
}
推荐答案
您好,
1.为此,您需要将存储过程修改为,如果它返回True
,则在Active
中进行修改,使False
返回Inactive
.另外,如果您不想在存储过程中进行任何更改,则还可以通过检查IsActive
的值来在RowDataBound
事件中进行更改.如果为true,则将标签文本显示为"Active
",反之亦然.
Hello,
1. For this, you will need to modify your stored procedure to , if it is returningTrue
then make inActive
forFalse
returnInactive
. Alternatively if you do not wish to make any changes in stored procedure, you can as well make changes inRowDataBound
event, by checking the value ofIsActive
. If it is true, display the label text as "Active
" or vice versa.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// here you will select the IsActive column and modify it's text
Label isActive =(Label) e.row.FindControl("lblIsActive");
// after getting the reference set its text
if(isActive.Text.ToLower() == "true")
isActive.Text = "Active";
else
isActive.Text = "Inactive";
}
}
2.回答第二个问题:在gridview中的EditItemTemplate
中删除以下代码行
2.Answer to your second question: Remove below line of code from EditItemTemplate
in your gridview
Text='<%# Eval("IsActive")%>'/>
这篇关于如何在GridView中修改此复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!