[asp.net]无法将对象强制转换的类型 'System.Web.UI.WebControls.Button',键入 'System.Web.UI.WebControls.LinkButton'

标签: Asp.net html5 C#
发布时间: 2016/8/21 7:35:53
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试着要审批的文档,但是当我点击同意按钮它显示我错误

无法强制转换类型 'System.Web.UI.WebControls.Button',键入 System.Web.UI.WebControls.LinkButton 的对象。

gridview

<asp:GridView ID="GrdFileApprove" runat="server" AutoGenerateColumns="False" 
                onrowcommand="GrdFileApprove_RowCommand">
               <Columns>
                   <asp:TemplateField HeaderText="S no">
                       <ItemTemplate>
                           <%# Container.DataItemIndex+1 %>
                           <asp:HiddenField runat="server" ID="HdnFileID" Value='<%# 
                      Eval("DocID") %>' />
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:BoundField DataField="DocID" HeaderText="DocumentID"  />
                   <asp:BoundField DataField="DocName" HeaderText="DocName"  />
                   <asp:BoundField DataField="Uploadfile" HeaderText="File Name" />
                   <asp:BoundField DataField="DocType" HeaderText="Document" />
                   <asp:BoundField DataField="DepType" HeaderText="Department" />
                   <asp:TemplateField HeaderText="S no">
                       <ItemTemplate>


                           <asp:Button runat="server" Id="BtnApprove"
                           CommandName="_Approve" 

                                CommandArgument='<%# Eval("DocID") %>' Text="Aprrove"
                             />

                           <asp:Button runat="server" Id="Button1" 
                         CommandName="_Reject" 
                                CommandArgument='<%# Eval("DocID") %>' Text="Reject" />
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:ButtonField Text="Button" />
               </Columns>
           </asp:GridView>

</div>

代码

if (e.CommandName == "_Approve")
        {
            //using (SqlConnection con = DataAccess.GetConnected())
            using (SqlConnection con = new 
        SqlConnection(ConfigurationManager.ConnectionStrings

         ["mydms"].ConnectionString))
            {
                try
                {
                    int rowindex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = (GridViewRow)
          ((Control)e.CommandSource).NamingContainer;
                    LinkButton Prove_Button = 
             (LinkButton)row.FindControl("BtnApprove");
                    SqlCommand cmd = new SqlCommand("spinsertapprove", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID", UserID));
                    cmd.Parameters.Add(new SqlParameter("@DocID", DocID));
                    cmd.Parameters.Add(new SqlParameter("@ApproveType", "Approve"));
                    int result = cmd.ExecuteNonQuery();
                    if (result != 0)
                    {
                        GrdFileApprove.DataBind();
                    }
                }

                catch
                {
                    apfi.Text = "Not Approve";



                }
                finally
                {
                    con.Close();
                }
            }
        }


        else if (e.CommandName == "_Reject")
        {
            using (SqlConnection con = new 
             SqlConnection(ConfigurationManager.ConnectionStrings

            ["mydms"].ConnectionString))
            {
                try
                {
                    int rowindex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = (GridViewRow)
              ((Control)e.CommandSource).NamingContainer;
                    LinkButton Prove_Button = (LinkButton)row.FindControl("Button1");
                    SqlCommand cmd = new SqlCommand("sprejectapprove", con);

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID",UserID));
                    cmd.Parameters.Add(new SqlParameter("@DocID", DocID));
                    cmd.Parameters.Add(new SqlParameter("@ApproveType", "Reject"));
                    int result = cmd.ExecuteNonQuery();
                    if (result != 0)
                    {
                        GrdFileApprove.DataBind();
                    }
                }

                catch 
                {
                    apfi.Text = "Rejct";
                }
                finally
                {
                    con.Close();
                }
            }
        }

它显示我错误在抓捕时单击批准按钮

解决方法 1:

问题和  应该是相当简单的。

在代码中你投下您的按钮到 LinkButton :

(LinkButton)row.FindControl("BtnApprove");

当很明显在你的 html,您使用常规的按钮︰

<asp:Button runat="server" Id="BtnApprove"
                           CommandName="_Approve" 

                                CommandArgument='<%# Eval("DocID") %>' Text="Aprrove"
                             />

只是改变它,所以你强制转换为 Button 相反和所有应该没事

(Button)row.FindControl("BtnApprove");
赞助商