动态创建控件时如何从数据库中删除记录 [英] how to delete record from database when control created dynamically

查看:72
本文介绍了动态创建控件时如何从数据库中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我以动态方式创建标签,超链接和链接按钮.标签用于显示数据库值,超链接用于重定向另一个页面,链接按钮用于更改数据库中记录的状态.当我单击特定记录的已删除按钮时,记录状态列未被删除",因此它们会变为其他记录的状态,因为它们会得到最后一个记录的参数,而不是我删除的记录.我想当我单击任何记录按钮时,该记录的状态被更改,而不是其他任何记录.下面我写代码来理解它.问题是视图状态参数上的白羊座.

in my application i m create label,hyperlink and linkbutton dynmaically.label is used to display database value and hyperlink is used to redirect another page and linkbutton is used to change the status of record in database. when i click on deleted button of specific record the record staus column is not ''deleted'' insted of they chenge the status of other record because they get parametr of last record not record of which i deleted. i want when i click on any record butoon the status of that record is changed not any other record. below i m write code to understand it. THE problem is aries on view state parameter .

ViewState["page_id"] = dr["page_id"].ToString();
          ViewState["div"] = dr["div"].ToString();









protected void Page_Load(object sender, EventArgs e)
   {

           retrive_record();


   }
   protected void retrive_record()
   {

       SqlConnection con = new SqlConnection();
       con.ConnectionString = ConfigurationManager.ConnectionStrings             ["ConnStringDb1"].ConnectionString;
       SqlCommand cmd = new SqlCommand("select * from content_managment where status !=''deleted''", con);
       DataTable dt = new DataTable();
       SqlDataAdapter ad = new SqlDataAdapter();

       ad.SelectCommand = cmd;
       ad.Fill(dt);
       //DataRow dr1 = dt.Rows[0];

       HtmlTable table = new HtmlTable();
       //HtmlTableRow tr = new HtmlTableRow();
       table.Border = 1;
       table.Width = "450PX";
       table.Align = "center";
       table.CellSpacing = 5;
       foreach (DataRow dr in dt.Rows)
       {

           HtmlTableRow tr = new HtmlTableRow();
           HtmlTableCell tc = new HtmlTableCell();
           tc.Width = "140px";
           tc.Align = "left";
           HtmlTableCell tc1 = new HtmlTableCell();
           tc1.Width = "5px";
           tc1.Align = "right";
           HtmlTableCell tc2 = new HtmlTableCell();
           tc2.Width = "5px";
           tc2.Align = "right";
           Label lb = new Label();
           lb.Font.Bold = true;
           lb.Font.Size = 11;
           lb.Text = dr["content_heading"].ToString();
           HyperLink hp = new HyperLink();
           hp.Text = "Edit";

           LinkButton linkbut = new LinkButton();
           linkbut.Text = "Delete";
           linkbut.Click += new System.EventHandler(this.delete_record);
           tc2.Controls.Add(linkbut);

         tc.Controls.Add(lb);
           tc1.Controls.Add(hp);

           tr.Cells.Add(tc);
           tr.Cells.Add(tc1);
           tr.Cells.Add(tc2);
           table.Rows.Add(tr);
           Panel1.Controls.Add(table);
           ViewState["page_id"] = dr["page_id"].ToString();
           ViewState["div"] = dr["div"].ToString();
           hp.NavigateUrl = "Edit_Home_Page_Content.aspx?f1=" + dr["content_heading"].ToString();

       }

       ad.Dispose();
       cmd.Dispose();
       con.Dispose();
   }
   protected void delete_record(object sender, EventArgs e)
   {
       SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString);
       con1.Open();


           string str1 = "update content_managment set status = ''deleted'' where page_id = @page_id and div = @div_id";
           SqlCommand cmd1 = new SqlCommand(str1, con1);
           cmd1.Parameters.Add("@page_id",SqlDbType.Int);
           cmd1.Parameters.Add("@div_id", SqlDbType.Int);
           cmd1.Parameters["@page_id"].Value = ViewState["page_id"].ToString();
           cmd1.Parameters["@div_id"].Value = ViewState["div"].ToString();
           int updaterow = cmd1.ExecuteNonQuery();
           if (updaterow > 0)
           {

               Label1.Visible = true;
               Label1.Text = "Deleted Sucessfully";
               Response.Redirect("Default2.aspx");
           }
           else
           {
               Label1.Visible = true;
               Label1.Text = "Not Deleted";
           }




   }

推荐答案

在您的foreach瓢中,您仅使用数据库中的最后一个值设置Viewstate


in your foreach scoope you are setting Viewstate only with last value from database


Panel1.Controls.Add(table);
 ViewState["page_id"] = dr["page_id"].ToString();
 ViewState["div"] = dr["div"].ToString();




我的建议为您的linkbutton创建新属性,并将此属性设置为for scoope.

我举一个小例子.




my advice create new property for your linkbutton and set this prop in the for scoope.

i am giving a small example.

//you can inherit and set new propertyies like this.
public class myButton : LinkButton
        {          
            public myButton() : base() { }

            private string _divId;

            public string DivId
            {
                get { return _divId; }
                set { _divId = value; }
            }
            private string _pageId;

            public string PageId
            {
                get { return _pageId; }
                set { _pageId = value; }
            }
        }



在您的for语句中更改此行



change this line in your for statement

LinkButton linkbut = new LinkButton();
linkbut.Text = "Delete";
linkbut.Click += new System.EventHandler(this.delete_record);
tc2.Controls.Add(linkbut);



像这样



like this

myButton linkbut = new myButton ();
linkbut.Text = "Delete";
linkbut.PageId = dr["page_id"].ToString();
linkbut.DivId = dr["div"].ToString();

linkbut.Click += new System.EventHandler(this.delete_record);
tc2.Controls.Add(linkbut);



并在您的Click事件中获取div和页面,例如



and get div and page in your Click event like this

myButton btn = (myButton)sender;
cmd1.Parameters["@page_id"].Value = myButton.PageId;
cmd1.Parameters["@div_id"].Value = myButton.DivId;



希望对您有帮助



i hope this will help you


这篇关于动态创建控件时如何从数据库中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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