选中复选框时在gridview中添加文本框

时间:2019-05-10 02:52:41

标签: c# asp.net gridview

在我的Gridview中,有一个名为“ ReasonsOfReject”的文本框。
      当我在文本框内键入数据并使用BtnReject_Click更新数据时,由于某种原因该功能无法正常工作。

我尝试添加以下代码,但也无法正常工作:

var txtReasonsOfReject = row.Cells[9].FindControl("txtReasonsOfReject") as TextBox;

aspx.cs

 protected void BtnReject_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox status = (row.Cells[3].FindControl("ApprovalCheckBox") as CheckBox);
            int EmployeeID = Convert.ToInt32(row.Cells[1].Text);
            if (status.Checked)
            {
                //var txtReasonsOfReject = row.Cells[9].FindControl("txtReasonsOfReject") as TextBox;
                updaterowforReject(EmployeeID, "Rejected");
            }
            else
            {
                updaterowforReject(EmployeeID, "Pending");

            }

        }
        lblStatus0.Text = "Applications Has Been Rejected";
        SqlDataSource1.DataBind();
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
        //add in
        GridView2.DataBind();
        GridView3.DataBind();            

    }
    private void updaterowforReject(int EmployeeID, String Status)
    {
        String mycon = "Data Source=ipaddr;Network Library=DBMSSOCN;Initial Catalog=db;User ID=test;Password=test";
        String updatedata = "Update tbOT set ReasonsOfReject=@ReasonsOfReject, Status='" + Status + "' where EmployeeID=" + EmployeeID;
        SqlConnection con = new SqlConnection(mycon);
        con.Open();
        SqlCommand cmd = new SqlCommand();
       // cmd.Parameters.AddWithValue("@ReasonsOfReject", txt);
        //cmd.ExecuteNonQuery();
        cmd.CommandText = updatedata;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();

    }

.aspx

<asp:TemplateField HeaderText="Status">
            <ItemTemplate>
                <asp:CheckBox ID="ApprovalCheckBox" runat="server" style="text-align: center"/>               
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID">
        <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>

          <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:label ID="lblEmployeeID" runat="server"  Text='<%# Eval("EmployeeID") %>' Visible="false"> </asp:label>                   
            </ItemTemplate>
        </asp:TemplateField>    

        <asp:TemplateField HeaderText="ReasonsOfReject" Visible="true">
            <ItemTemplate>
                <asp:TextBox ID="txtReasonsOfReject" runat="server" Text='<%# Eval("ReasonsOfReject") %>'></asp:TextBox>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>

0 个答案:

没有答案