0

我有一个网格视图,当我单击单选按钮批准时,它应该根据employeeID 将推荐标签值更新为1。但是我收到一个错误消息,指出对象未设置为对象的实例。我的列是也被添加到左边..而不是右边..下面是我尝试过的代码。

C# 代码

public void GridViewBind()
        {
            dadapter = new SqlDataAdapter("SELECT M_Emp_Personal.EmpName, M_Division.DivShort, M_Designation.DesigShort, T_TADA_tempform.BasicSalary, T_TADA_tempform.GPFNo, T_TADA_tempform.Gradepay,T_TADA_tempform.move_date, T_TADA_tempform.purpose, M_City.CityDesc, T_TADA_tempform.estt_visited, T_TADA_tempform.duration_stay, M_mode.mode_type, T_TADA_tempform.duration_unit, T_TADA_tempform.place, T_TADA_tempform.authority, T_TADA_tempform.exp_debited, T_TADA_tempform.reason FROM T_TADA_tempform INNER JOIN M_Emp_Personal ON T_TADA_tempform.EmpID = M_Emp_Personal.EmpID INNER JOIN M_Division ON T_TADA_tempform.DivisionID = M_Division.DivisionID INNER JOIN M_Designation ON M_Emp_Personal.DesigID = M_Designation.DesigID INNER JOIN M_City ON T_TADA_tempform.CityID = M_City.CityID INNER JOIN M_mode ON T_TADA_tempform.mode_ID = M_mode.mode_ID where M_Emp_Personal.EmpID=" + ddlname.SelectedValue + "", conn);
            dset = new DataSet();
            dadapter.Fill(dset);
            GridView1.DataSource = dset.Tables[0];
            GridView1.DataBind();
        }


protected void submit_info(object sender, EventArgs e)
        {


            GridViewRow grow = (GridViewRow)(sender as Control).Parent.Parent;

            RadioButton rbpApprove = (RadioButton)grow.FindControl("rbtnapprove");
            RadioButton rbpReject = (RadioButton)grow.FindControl("rbtnreject");



            if (rbpApprove.Checked == true)
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("UPDATE T_TADA_tempform SET Recommened_tag =1 where EmpID=@EmpID", conn);

                cmd.Parameters.AddWithValue("@EmpID", ddlname.SelectedValue);

                conn.Close();

            }

这是我的 ASP.NET 代码

 <asp:GridView ID="GridView1" runat="server"  CssClass="vutblrow" TabIndex="6" 
 CellPadding="4" ForeColor="#333333" GridLines="None"  Width="100%"  
PagerStyle-Mode="NumericPages" > 
<PagerStyle CssClass="pgr"  Height="25px" BorderStyle="Solid" />

<Columns>
<asp:TemplateField HeaderText="Approve">
<ItemTemplate>
<asp:RadioButton  runat="server" GroupName="status"  />
                                                                   </ItemTemplate>                                                                                                                                  
</asp:TemplateField>

<asp:TemplateField HeaderText="Reject">
<ItemTemplate>
<asp:RadioButton   runat="server" GroupName="status"  />
                                                                   </ItemTemplate>                                                                                                                                  
</asp:TemplateField><asp:TemplateField HeaderText="Submit">

<ItemTemplate>
<asp:Button CssClass="btnAction" Text="Sumbit"  runat="server" OnClick="submit_info"  />
                                                                  </ItemTemplate>                                                                                                                              
</asp:TemplateField>

                                                                </Columns>                                                        
<HeaderStyle CssClass="vutblhdr" />

</asp:GridView>
4

2 回答 2

1

当您尝试使用尚未正确初始化的对象时,您将看到该错误。

可能的候选人是:

 RadioButton rbpApprove = (RadioButton)grow.FindControl("rbtnapprove");
 RadioButton rbpReject = (RadioButton)grow.FindControl("rbtnreject");

如果rbpApprove找不到控件,则此行:

if (rbpApprove.Checked == true)

会给你你得到的错误。

然而,只看到你的代码的一个子集,它可能在其他地方。

于 2013-10-18T09:07:27.183 回答
0

我认为您缺少单选按钮的 ID,试试这个

        <asp:TemplateField HeaderText="Approve">
            <ItemTemplate>
                <asp:RadioButton ID="rbtnapprove" runat="server" GroupName="status" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Reject">
            <ItemTemplate>
                <asp:RadioButton ID="rbtnreject" runat="server" GroupName="status" />
            </ItemTemplate>
        </asp:TemplateField>

并调用执行

if (rbpApprove.Checked == true)
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("UPDATE T_TADA_tempform SET Recommened_tag =1 where EmpID=@EmpID", conn);

                cmd.Parameters.AddWithValue("@EmpID", ddlname.SelectedValue);
                cmd.ExecuteNonQuery();

                conn.Close();

            }
于 2013-10-18T09:07:48.343 回答