复选框在DetailView中 [英] CheckBox In DetailView

查看:97
本文介绍了复选框在DetailView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在detaill视图中选中一个复选框,以供已批准和未批准的Sql Server使用,如果我选中该复选框,则应将其保存在数据库中,如果批准或为true,
此链接对gridview来说对我来说很好用,我希望在DetailView中使用它..

http://www.ezzylearning.com/tutorial.aspx?tid=5187857&q=using-checkbox-in-asp-net-gridview-control

这是我的代码,单元格"上出现错误

错误18``System.Web.UI.WebControls.DetailsView''不包含``Cells''的定义,并且没有扩展方法``Cells''接受类型为``System.Web.UI.WebControls的第一个参数'' .DetailsView''(您是否缺少using指令或程序集引用?)C:\ Users \ Administrator \ Documents \ Visual Studio 2010 \ KSMC_EmpDtl \ KSMC_EmpDtl \ search.aspx.cs 50 30 KSMC_EmpDtl



i want a check box in detaill view for approved and not approved with Sql Server if i checked checkbox it should save in data base as approved or true ,

this link work fine for me for gridview i want it in DetailView ,..

http://www.ezzylearning.com/tutorial.aspx?tid=5187857&q=using-checkbox-in-asp-net-gridview-control

here is my code i am getting error on "Cells"

Error 18 ''System.Web.UI.WebControls.DetailsView'' does not contain a definition for ''Cells'' and no extension method ''Cells'' accepting a first argument of type ''System.Web.UI.WebControls.DetailsView'' could be found (are you missing a using directive or an assembly reference?) C:\Users\Administrator\Documents\Visual Studio 2010\KSMC_EmpDtl\KSMC_EmpDtl\search.aspx.cs 50 30 KSMC_EmpDtl



protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               LoadData();
           }
       }

       private void LoadData()
       {

           string query = @"SELECT ID,Name,Emp_No,Job_Title,Nationality,Administration,Section,Another_job,health_no,Exp_Date,National_ID,serial_no,reference,confirm_rcrd,arabic_name,arabic_jobtitle FROM KSMCemp_data";

           SqlDataAdapter da = new SqlDataAdapter(query, con);
           DataTable table = new DataTable();
           da.Fill(table);

           GridView2.DataSource = table;
           GridView2.DataBind();

       }

       public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
       {
           CheckBox chkStatus = (CheckBox)sender;
           DetailsView row = (DetailsView)chkStatus.NamingContainer;


           string cid = row.Cells[1].Text;                              <<--- ERROR
           bool status = chkStatus.Checked;



           string query = "UPDATE KSMCemp_data SET confirm_rcrd = @confirm_rcrd WHERE ID = @ID";


           SqlCommand com = new SqlCommand(query, con);


           com.Parameters.Add("@confirm_rcrd", SqlDbType.Bit).Value = status;

           com.Parameters.Add("@ID", SqlDbType.Int).Value = cid;


           con.Open();
           com.ExecuteNonQuery();
           con.Close();

           LoadData();

       }





<asp:DetailsView ID="DetailsView1" runat="server"
          AutoGenerateRows="False" CellPadding="4"  DataKeyNames="ID"
          ForeColor="#333333" GridLines="Horizontal" Height="50px"
          Width="291px" HorizontalAlign="Left" ViewStateMode="Enabled">
          <AlternatingRowStyle BackColor="White" />
          <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
          <EditRowStyle BackColor="#2461BF" />
          <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
          <Fields>



                  <asp:TemplateField HeaderText="Confirm Records">
                  <ItemTemplate>
              <asp:CheckBox ID="chkStatus" runat="server"
                          AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
                          Checked='<%# Convert.ToBoolean(Eval("confirm_rcrd")) %>'
                          Text='<%# Eval("confirm_rcrd").ToString().Equals("True") ? " Confirm " : " Pending " %>'  />
                  </ItemTemplate>
                   </asp:TemplateField>

               <asp:BoundField DataField="ID" HeaderText="Serial No" />
              <asp:BoundField DataField="Name" HeaderText="Name"  />
              <asp:BoundField DataField="Emp_No" HeaderText="Emp_No"
                  />

推荐答案

错误消息不是很明显吗?类System.Web.UI.WebControls.DetailsView中没有像Cells这样的成员.

变量命名(row)产生的印象是,您以某种方式混淆了两个不同的类:System.Web.UI.WebControls.DetailsView和可能的System.Web.UI.WebControls.DetailsViewRow,它们确实具有属性Cells.请参阅:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsviewrow.aspx [ ^ ].

即使您在上一行代码中正确计算了row引用(因此类型转换也不会抛出无效的强制转换异常),该变量也会获得对某些System.Web.UI.WebControls.DetailsViewRow实例的引用,您可以使用该实例来引用特定的行实例;请参阅上面引用的第一篇MSDN文章.

—SA
Isn''t the error message quite obvious? There is no such member as Cells in the class System.Web.UI.WebControls.DetailsView.

Your variable naming (row) creates the impression that you somehow mixed up two different classes: System.Web.UI.WebControls.DetailsView and, probably, System.Web.UI.WebControls.DetailsViewRow, which does have the property Cells. Please see:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsviewrow.aspx[^].

Even if you correctly calculated the row reference in the preceding line of code (so the typecast won''t throw invalid cast exception), this variable will get a reference to some System.Web.UI.WebControls.DetailsViewRow instance which you can use to reference on of the particular row instances; please see the first MSDN article referenced above.

—SA


这篇关于复选框在DetailView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆