| |

VerySource

 Forgot password?
 Register
Search
View: 923|Reply: 9

gridview delete in bulk, why can't I delete it !!!

[Copy link]

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-4 01:00:01
| Show all posts |Read mode
The gridview was deleted in batches. I debugged and found that CheckBox2 could not be passed. After selecting all CheckBox2, after clicking the delete button, I found that CheckBox2 is still unselected. Why can't it pass?

I posted the code to help you see!

<body>
    <form id = "form1" runat = "server">
    <div>
    
<table border = "0" width = "530"> <tr> <td style = "width: 5%" align = "center">
              <asp: CheckBox ID = "CheckBox1" runat = "server" AutoPostBack = "True" OnCheckedChanged = "CheckBox1_CheckedChanged" /> </ td> <td style = "width: 10%" align = "center"> Update </ td > <td style = "width: 15%" align = "center"> name </ td> <td style = "width: 25%" align = "center"> picture </ td> <td style = "width: 20% "align =" center "> Image size </ td> <td style =" width: 25% "align =" center "> Upload date </ td> </ tr> </ table>
          <asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False"
BackColor = "White" BorderColor = "# CC9966" BorderWidth = "1px" CellPadding = "4"
DataKeyNames = "pid" HorizontalAlign = "Center"
Width = "530" BorderStyle = "None" ShowHeader = "False" AllowPaging = "True">
 <FooterStyle BackColor = "# FFFFCC" ForeColor = "# 330099" />
                <Columns>
                <asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "5%" />
<ItemTemplate>
<asp: CheckBox ID = "CheckBox2" runat = "server" />
</ ItemTemplate>
</ asp: TemplateField>
<asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "10%" />
<ItemTemplate>
Update
</ ItemTemplate>
</ asp: TemplateField>
<asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "15%" />
<ItemTemplate>
<% # Eval ("pname")%>
</ ItemTemplate>
</ asp: TemplateField>
<asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "24%" />
<ItemTemplate>
<img src = "<% # Eval (" paddr ")%>" alt = "" width = "50" />
</ ItemTemplate>
</ asp: TemplateField>
<asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "20%" />
<ItemTemplate>
<% # Eval ("pdx")%> bytes
</ ItemTemplate>
</ asp: TemplateField>
<asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "25%" />
<ItemTemplate>
<% # Eval ("pdate")%>
</ ItemTemplate>
</ asp: TemplateField>
<asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "1%" />
<ItemTemplate>
<asp: Label Text = '<% # Eval ("pid")%>' Visible = "False" runat = "server" Width = "80%" ID = "lblColumn" />
</ ItemTemplate>
</ asp: TemplateField>
                </ Columns>
            </ asp: GridView>
            
               <table border = "0" width = "530"> <tr> <td>
          <asp: Button ID = "Button1" runat = "server" Text = "Select All" OnClick = "Button1_Click" />&nbsp;
          <asp: Button ID = "Button2" runat = "server" OnClientClick = "javascript: return confirm ('Are you sure you want to delete the selected picture?')" Text = "Remove" OnClick = "Button2_Click" />&nbsp;
          <asp: Button ID = "Button3" runat = "server" OnClientClick = "javascript: return confirm ('Are you sure you want to delete this album?')" Text = "Delete this album" /> </ td> </ tr > </ table>
  <asp: LinkButton ID = "btnFirst" CommandArgument = "first" OnClick = "PagerButtonClick"
                            runat = "server"> Home </ asp: LinkButton>
                        <asp: LinkButton ID = "btnPrev" CommandArgument = "prev" OnClick = "PagerButtonClick" runat = "server"> Previous page </ asp: LinkButton>
                        <asp: LinkButton ID = "btnNext" CommandArgument = "next" OnClick = "PagerButtonClick" runat = "server"> Next page </ asp: LinkButton>
                        <asp: LinkButton ID = "btnLast" CommandArgument = "last" OnClick = "PagerButtonClick" runat = "server"> Last page </ asp: LinkButton>
                        <asp: Label ID = "LblCurrentIndex" runat = "server"> </ asp: Label>
                        <asp: Label ID = "LblPageCount" runat = "server"> </ asp: Label>
                        <asp: Label ID = "LblRecordCount" runat = "server"> </ asp: Label>
            </ div>
    </ form>
</ body>
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-5-16 09:45:02
| Show all posts
protected void Button1_Click (object sender, EventArgs e)
    {
        if (Button1.Text == "Select All")
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox CheckBox2 = (CheckBox) row.Cells [0] .FindControl ("CheckBox2");
                CheckBox2.Checked = true;
               
            }
            Button1.Text = "Uncheck all";
        }
        else
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox CheckBox2 = (CheckBox) row.Cells [0] .FindControl ("CheckBox2");
                CheckBox2.Checked = false;
            }
            Button1.Text = "Select All";
        }


    }


 protected void Button2_Click (object sender, EventArgs e)
    {
foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox CheckBox2 = (CheckBox) row.Cells [0] .FindControl ("CheckBox2");
            if (CheckBox2.Checked == true)
            {
                string connStr = ConfigurationManager.AppSettings ["LawConnStr"];
                SqlConnection conn = new SqlConnection (connStr);
                SqlCommand comm = new SqlCommand ();
                comm.Connection = conn;
                comm.CommandText = "delete from pictures_pic where pid = '" + row.Cells [2] .Text + "'";
                conn.Open ();
                comm.ExecuteNonQuery ();
                conn.Close ();
            }
        }

    }


    protected void CheckBox1_CheckedChanged (object sender, EventArgs e)
    {
        if (this.CheckBox1.Checked == true)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox CheckBox2 = (CheckBox) row.Cells [0] .FindControl ("CheckBox2");
                CheckBox2.Checked = true;
            }
            Button1.Text = "Uncheck all";
        }
        else
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox CheckBox2 = (CheckBox) row.Cells [0] .FindControl ("CheckBox2");
                CheckBox2.Checked = false;
            }
            Button1.Text = "Select All";
        }
    }
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-18 17:15:01
| Show all posts
Did you bind the data again in the PAGE_LOAD event?
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-5-19 17:15:01
| Show all posts
I wrote it like this
 protected void Page_Load (object sender, EventArgs e)
    {
        if (! Page.IsPostBack)
        {
            GridViewBind ();
        }
    }

May I?
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-5-21 12:30:01
| Show all posts
You follow it step by step
In addition, from the perspective of efficiency, it is recommended not to write the program like this
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-5-21 17:30:02
| Show all posts
In addition, from the perspective of efficiency, it is recommended not to write the program like this

Haha, how to write it better?
Reply

Use magic Report

3

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-5-29 09:00:01
| Show all posts
Add the following code in GridView

<asp: TemplateField>
<ItemStyle HorizontalAlign = "Center" Width = "12%" />
<ItemTemplate>
<asp: LinkButton ID = "Link1" OnCommand = "Link1_clink" runat = "server" Text = "Set as cover"> </ asp: LinkButton>
</ ItemTemplate>
</ asp: TemplateField>

The backstage is
 protected void Link1_clink (object sender, EventArgs e)
    {
     }

How do I get the ID of the record that clicked this button in the background?
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-2 08:15:01
| Show all posts
CheckBox CheckBox2 = (CheckBox)row.Cells[0].FindControl("CheckBox2"); Replace with
CheckBox CheckBox2 = (CheckBox)GridView1.Rows[0].FindControl("CheckBox2"); try
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-22 10:15:01
| Show all posts
protected void Button2_Click(object sender, EventArgs e)
    {
foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox CheckBox2 = (CheckBox)row.Cells[0].FindControl("CheckBox2");
            if (CheckBox2.Checked == true)
            {
                string connStr = ConfigurationManager.AppSettings["LawConnStr"];
                SqlConnection conn = new SqlConnection(connStr);
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "delete from pictures_pic where pid='" + row.Cells[2].Text + "'";
                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }

    }
This is one point, you can use a query like this:
update ... set ... = ...
where id in (1,2,...)
Get them all at once

In addition, the number of PostBack should be reduced as much as possible to enhance the user experience. Like select all, it should be implemented by JS.
Reply

Use magic Report

4

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-8-25 17:15:01
| Show all posts
Why the old prompt if (CheckBox2.Checked == true) does not reference the object to instantiation?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list