Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 .
C#
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";
           }




   }
Posted
Comments
Alf1har 7-Jun-12 3:34am    
Hi Manohar

Could you give us a bit more detail on how create your controls dynamically, i.e. using a repeater or some other way? The answer really lies in this as the problem is not in the SQL statement but in how you fin out which of the LinkButtons were pressed, if you browse your page in firebug for instance you'll see that each linkbutton has a different prefix added, and the name you picked for the id as well.

I will attach an example of something like this for you in the answere once you can tell us exactly how you create the controls.

Best Regards

Alf

1 solution

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


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



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

i am giving a small example.

C#
//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; }
            }
        }


change this line in your for statement

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


like this

C#
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);


and get div and page in your Click event like this

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


i hope this will help you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900