这是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>
|
<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);
这样可以消除错误,但是在GridView中单击
Edit_Employee.aspx
按钮时,不会将员工的任何数据从此页传送到Edit
任何帮助,将不胜感激。提前致谢。
最佳答案
我知道您想使用Employee Code将用户重定向到Edit Employee页面。我建议您使用Anchor标签而不是LinkButton。试试下面的代码,它很容易集成,并且可以防止服务器请求和响应。
您的“编辑链接”按钮:
<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>
我在这里所做的是,我在查询字符串而不是会话中传递了员工代码,您可以使用以下代码在Edit_Employee页面中检索查询字符串变量:
int liEmployeeCode = Convert.ToInt32(Request.QueryString["Selected_Employee"]);
请让我知道,如果你有任何问题。
关于c# - 无法将类型为System.Web.UI.WebControls.Label的对象强制转换为类型为System.IConvertible,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29874349/