1

我尝试批准文件,但是当我单击批准按钮时,它显示错误

无法将“System.Web.UI.WebControls.Button”类型的对象转换为“System.Web.UI.WebControls.LinkBut​​ton”类型。

网格视图

<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();
                }
            }
        }

单击批准按钮时,它会显示捕获错误

4

3 回答 3

2

问题和解决方案应该非常简单。

在您的代码中,您将按钮转换为LinkButton

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

当很明显在您的 html 中,您使用常规按钮:

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

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

只需更改它,以便您改为使用它Button,一切都应该没问题

(Button)row.FindControl("BtnApprove");
于 2013-10-01T07:41:09.627 回答
0

首先,错误消息告诉您究竟出了什么问题 - 它不能将其转换为 LinkBut​​ton,因为它不是LinkBut​​ton,您使用的是普通按钮。

其次,我不明白你为什么要费心去抓住按钮,更不用说施放它了,因为你似乎根本没有用它做任何事情。一旦你将它存储在你的 Prove_Button 变量中,你就再也不会触摸 Prove_Button 来对它做任何事情,所以一开始就没有任何意义。

于 2013-10-01T07:43:20.557 回答
0
var btnApprove = (Button)e.CommandSource;
于 2017-06-12T21:06:35.753 回答