开发者

UpdatePanel Gridview not updating

开发者 https://www.devze.com 2023-02-21 02:53 出处:网络
For some reason, I can\'t get the Gridview in the Updatepanel to refresh after I\'ve made changes. can someone help?

For some reason, I can't get the Gridview in the Updatepanel to refresh after I've made changes. can someone help?

I'm using the ToolkitScriptManager control and the UpdatePanel.

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
   <ContentTemplate>
        <asp:GridView blah...

         </asp:GridView>

    </ContentTemplate>
   <Triggers>
       <asp:AsyncPostBackTrigger ControlID="DeleteButton" EventName="Click" />
       <asp:AsyncPostBackTrigger ControlID="IBUpUp" EventName="Click" />
       <asp:AsyncPostBackTrigger ControlID="IBDownDown" EventName="Click" />
       <asp:AsyncPostBackTrigger ControlID="IBUp" EventName="Click" />
       <asp:AsyncPostBackTrigger ControlID="IBDown" EventName="Click" />
       <asp:AsyncPostBackTrigger ControlID="EditProfile" EventName="Click" />
   </Triggers>

Cs Page

protected void Unnamed3_Click(object sender, ImageClickEventArgs e)
        {

            int rowIndex = GridView1.SelectedIndex;
            GridViewRow gvr = GridView1.SelectedRow;

            if (rowIndex >= 0)
            {
                //delete

                String GridViewOne = GridView1.DataKeys[rowIndex].Value.ToString();

                //delete image

                string imagename = gvr.Cells[2].Text;

                string pathToImage = @"C:\Images\";

                pathToImage = pathToImage + imagename;


                if (System.IO.File.Exists(pathToImage))
                {
                    // Use a try block to catch IOExceptions, to
                    // handle the case of the file already being
                    // opened by another process.
                    try
                    {
                      开发者_StackOverflow社区  System.IO.File.Delete(pathToImage);
                    }
                    catch (System.IO.IOException m)
                    {
                        Console.WriteLine(m.Message);
                        return;
                    }
                }

                int bannerid = Convert.ToInt32(GridViewOne); 

                SqlDataReader sdr = null;

                SqlConnection conn = GetConnection();

                SqlCommand cmd = new SqlCommand("Tool_DeleteBannerAds", conn);

                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter param1 = new SqlParameter();

                param1.ParameterName = "@BannerID";
                param1.Value = bannerid;

                cmd.Parameters.Add(param1);

                conn.Open();

                sdr = cmd.ExecuteReader();

                sdr.Close();

                UpdatePanel1.Update();
                GridView1.DataBind();

            }
            else
            {
                //don't do anything
                //keep
                //Response.Redirect("Default.aspx");
            }


        }


change the order:

 GridView1.DataBind();
 UpdatePanel1.Update();


Here is my code for your question

ASPX FILE

<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField HeaderText="Name List" DataField="EmpName" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>

    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
    </form>
</body>

CODE BEHIND FILE

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var Employee = new { EmpID = 1, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" };
            var customerList = (new[] { Employee }).ToList();
            customerList.Add(new { EmpID = 2, EmpName = "Sheetal Jain", Department = "IT", Age = 33, Address = "Hello" });
            GridView1.DataSource = customerList;
            GridView1.DataBind();
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        try
        {
            var Employee = new { EmpID = 1, EmpName = "Rahul Jain", Department = "IT", Age = 33, Address = "Hello" };
            var customerList = (new[] { Employee }).ToList();
            customerList.Add(new { EmpID = 2, EmpName = "Sheetal Jain", Department = "IT", Age = 33, Address = "Hello" });
            customerList.Add(new { EmpID = 2, EmpName = "Minakshi Jain", Department = "IT", Age = 33, Address = "Hello" });
            GridView1.DataSource = customerList;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

This Code Dosent Created the postback for me hope it will work for you as well....


As an alternative, you could set the UpdateMode parameter to "Always" (which is the default) to allow any web control that triggers a postback to automatically update the GridView.

This saves you a few lines of code (configuring the triggers) but will also help if you dynamically add controls to your GridView that will also trigger postback events on your page.

0

精彩评论

暂无评论...
验证码 换一张
取 消