无法将System.Web.UI.WebControls.Label类型的对象强制转换为System.IConvertible类型

时间:2015-04-26 06:02:13

标签: c# asp.net gridview

这是gridview代码

<asp:GridView ID="gvwSearch" runat="server" 
    AutoGenerateColumns="False" 
    BackColor="White"
    BorderColor="#999999" 
    BorderStyle="Solid" 
    BorderWidth="1px" 
    CellPadding="3" 
    DataSourceID="SearchSqlDataSource"
    ForeColor="Black" 
    GridLines="Vertical" 
    Width="100%" 
    Visible="False" 
    AllowSorting="True"
    AllowPaging="True" 
    OnSelectedIndexChanged="gvwSearch_SelectedIndexChanged" 
    OnPageIndexChanging="gvwSearch_PageIndexChanging">
    <AlternatingRowStyle BackColor="#CCCCCC" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkbtnEdit" runat="server" OnClick="lnkbtnEdit_Click">
                    Edit</asp:LinkButton>
                &nbsp;|
                    <asp:LinkButton ID="nkbtnView" runat="server" OnClick="nkbtnView_Click">
                        View</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Employee Code" SortExpression="Employee Code">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" DataField="EmployeeCode" runat="server" 
                    Text='<%# Bind("[Employee Code]") %>'>
                </asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblCode" DataField="EmployeeCode" runat="server" 
                    Text='<%# Bind("[Employee Code]") %>'>

                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="Full Name" HeaderText="Full Name" SortExpression="Full Name" />
        <asp:BoundField DataField="CPR" HeaderText="CPR" SortExpression="CPR" />
        <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
        <asp:BoundField DataField="Designation" HeaderText="Designation" SortExpression="Designation" />
        <asp:BoundField DataField="Nationality" HeaderText="Nationality" SortExpression="Nationality" />
        <asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
        <asp:BoundField DataField="Joining Date" HeaderText="Joining Date" SortExpression="Joining Date" />
        <asp:BoundField DataField="Local Phone" HeaderText="Local Phone" SortExpression="Local Phone" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
        <asp:BoundField DataField="End of Service" HeaderText="End of Service" ReadOnly="True"
            SortExpression="End of Service" />
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="Gray" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="Gray" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>

这是我的c#代码:

protected void lnkbtnEdit_Click(object sender, EventArgs e)
{
    GridViewRow gridViewRow = (GridViewRow)(sender as Control).Parent.Parent;
    int index = gridViewRow.RowIndex;

    Label Code_Label = (Label)(gvwSearch.Rows[index].FindControl("lblCode"));

    int CodeLabel = Convert.ToInt32(Code_Label);//At thiline error occurs.
    New_Employee Selected_Employee = new New_Employee(CodeLabel);
    Session["Selected_Employee"] = Selected_Employee;
    Response.Redirect("~/Forms/New_Employee/Employees/Edit_Employee.aspx");
}

是的,我尝试过更改此行

int CodeLabel =Convert.ToInt32(Code_Label);

int CodeLabel =Convert.ToInt32(Code_Label.Text);

这会删除错误,但不会将此员工的数据从此页面传送到Edit_Employee.aspx当在GridView中单击Edit按钮时 任何帮助,将不胜感激。在此先感谢。

1 个答案:

答案 0 :(得分:1)

我知道您想要使用员工代码将用户重定向到“编辑员工”页面。我建议你使用Anchor标签而不是LinkBut​​ton。尝试下面的代码,它很容易集成,它会阻止服务器请求和响应。

您的编辑链接按钮:

<asp:LinkButton ID="lnkbtnEdit" runat="server" OnClick="lnkbtnEdit_Click">
                Edit</asp:LinkButton>

替换为:

<a href='<%# "Forms/New_Employee/Employees/Edit_Employee.aspx?Selected_Employee=" + Eval("[Employee Code]") %>'>Edit</a>

我在这里做了什么,我在查询字符串而不是Session中传递了Employee Code,您可以使用以下代码在Edit_Employee页面中检索查询字符串变量:

int liEmployeeCode = Convert.ToInt32(Request.QueryString["Selected_Employee"]);

如果您有任何疑问,请与我们联系。