我有一个网格视图,当我点击radiobutton批准时,它应该根据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>
答案 0 :(得分:1)
当您尝试使用尚未正确初始化的对象时,您将看到该错误。
可能的候选人是:
RadioButton rbpApprove = (RadioButton)grow.FindControl("rbtnapprove");
RadioButton rbpReject = (RadioButton)grow.FindControl("rbtnreject");
如果找不到rbpApprove
控件,则此处此行:
if (rbpApprove.Checked == true)
会给你错误的答案。
但是只看到你的代码的一部分,它可能在其他地方。
答案 1 :(得分: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();
}