本文介绍了刷新页面时如何在gride视图中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击gride view中的编辑按钮任务是performe但实际值是nox显示我刷新页面然后实际值来了



这个是代码



when i click on edit button in gride view task is performe but the actual value is nox show when i refresh the page then after actual value is come

this is the code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames="ID,Name,ImagePath,Status"



                    Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None"

                    onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"

                    PageIndex="5" PageSize="4" >
                    <RowStyle BackColor="#EFF3FB" />
                    <Columns>
                        <asp:TemplateField HeaderText="NO">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="NAME">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:Label ID="Label2" runat="server" Text='<%#Bind("Name") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="IMAGE">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("ImagePath") %>' Width="50px" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="STATUS">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:Label ID="Label3" runat="server" Text='<%#Bind("Status") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="EDIT">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:ImageButton ID="ImageButton2" runat="server"  CommandName='edit'

                                    ImageUrl="~/Img/icon-delete.gif" onclick="ImageButton2_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="DELETE">
                            <ItemTemplate>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <asp:ImageButton ID="ImageButton1" runat="server" CommandName='delete' ImageUrl="~/Img/icon-delete.gif" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#990033" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>







using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
public partial class BannerManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
               filldata();
              hope();
        }
        Panel1.Visible = false;
        Panel3.Visible = false;
    }
    SqlDataReader dr;
    SqlCommand cmd;
    protected void Button1_Click(object sender, EventArgs e)
    {
        filldata();
        string f = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("images/" + f));
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
        //SqlConnection con = new SqlConnection("Data Source=MNISH-PC\\SQLEXPRESS;Initial Catalog=Rakesh;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("INSERT INTO ImagesPath (Name,ImagePath,Link,Status)VALUES ('" + TextBox2.Text + "','" + "images/" + f + "','" + TextBox3.Text + "', '" + CheckBox1.Checked + "' )", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    public void filldata()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
        //SqlConnection con = new SqlConnection("Data Source=MNISH-PC\\SQLEXPRESS;Initial Catalog=Rakesh;Integrated Security=True");
        con.Open();
        string ret = "select * from ImagesPath";
        cmd = new SqlCommand(ret, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
        TextBox2.Text = "";
        TextBox3.Text = "";
                
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        filldata();
        TextBox2.Text = "";
        TextBox3.Text = "";
        Panel3.Visible = false;
        Panel1.Visible = false;

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //Label t = (Label)GridView1.Rows[e.RowIndex].FindControl("lable1");
        int id = (int)GridView1.DataKeys[e.RowIndex].Value;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
        //SqlConnection con = new SqlConnection("Data Source=MNISH-PC\\SQLEXPRESS;Initial Catalog=Rakesh;Integrated Security=True");
        con.Open();
        string r = "delete from ImagesPath where ID='" + id + "'";
        cmd = new SqlCommand(r, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
    }
    public void update()
    { 
    
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        
    }
    public void hope()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());

        SqlCommand cmd = new SqlCommand("Select * from ImagesPath", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataTextField = "ID";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataSource = ds.Tables[0];
        DropDownList1.DataBind();
    }
   
    protected void Button5_Click(object sender, EventArgs e)
    {
        Panel3.Visible = true;
        Panel1.Visible = false;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        filldata();

        if (FileUpload2.HasFile)
        {
            string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
            FileUpload2.SaveAs(Server.MapPath("images/" + f));
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
            con.Open();
            string str = "update ImagesPath set ImagePath='" + "images/" + f + "' where ID='" + DropDownList1.SelectedItem.Text + "'";
            cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();

        }
        else
        {
            //string f = Path.GetFileName(FileUpload2.PostedFile.FileName);
            //FileUpload2.SaveAs(Server.MapPath("images/" + f));
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());
            con.Open();
            string str = "update ImagesPath set Name='" + TextBox4.Text + "',Link='" + TextBox5.Text + "',Status='" + DropDownList2.SelectedItem.Text + "' where ID='" + DropDownList1.SelectedItem.Text + "'";
            cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
   
    protected void Button4_Click2(object sender, EventArgs e)
    {
        Panel3.Visible = true;
    }
    
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Panel1.Visible = true;
        Panel3.Visible = false;
        //hope();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EatProject"].ToString());

        con.Open();
        string str = "select Name,ImagePath,Link,Status from ImagesPath where ID ='" + DropDownList1.SelectedItem.Text + "'";
        cmd = new SqlCommand(str, con);

        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            TextBox4.Text = dr["Name"].ToString();
            Image2.ImageUrl = "~/" + dr["Imagepath"].ToString();
            TextBox5.Text = dr["Link"].ToString();
            //DropDownList2.SelectedValue = dr["Status"].ToString();
            
        }
        con.Close();
    }
   
}

推荐答案

If You''re Inserting Updating or Deleting Data from gridview then call the Fill()<br />
Method on Each Event.<br />
<br />
To Get Updated Data.


SqlCommand comm = new SqlCommand("Select required fields from YourTable", conn);
conn.Open();
SqlDataReader rdr = comm.ExecuteReader();
GridView1.DataSource = rdr;
GridView1.DataBind();
rdr.Close();


这篇关于刷新页面时如何在gride视图中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 20:02