在一个Gridview中,每一行都有一个操作列,放置一个LinkButton,控制用户控件的显示,<asp:LinkButton ID="lbtnEditMulit" runat="server" CommandArgument='<%# Eval("Property_ID")+","+Eval("Pro_ExtendID")+","+((GridViewRow)Container).RowIndex %>' CommandName="updateMulit" Font-Size="14px">编辑多选值</asp:LinkButton>显示用户控件代码:
protected void ShowUpdateMulitValues()
        {
            this.PlaceHolder1.Controls.Clear();
            this.div_editpro_value.Style.Add(HtmlTextWriterStyle.Display, "block");
            double offsetTop = 290 + (Convert.ToDouble(Up_multsel_temp["2"]) + 1) * 37 + (Convert.ToDouble(Up_multsel_temp["2"]) + 2)*0.5;
            int offsetLeft = Convert.ToInt32(this.hid_left.Value);
            this.div_editpro_value.Style[HtmlTextWriterStyle.Top] = offsetTop.ToString() + "px";
            this.div_editpro_value.Style[HtmlTextWriterStyle.Left] = offsetLeft.ToString() + "px";
            PropertyValuesManager pvm = (PropertyValuesManager)Page.LoadControl("UserControl/PropertyValuesManager.ascx");
            pvm.Extend_ID = Convert.ToInt32(Up_multsel_temp["1"]);
            pvm.Property_ID = Convert.ToInt32(Up_multsel_temp["0"]);
            this.PlaceHolder1.Controls.Add(pvm);
        }Up_multsel_temp里存放的CommandArgument值。
问题是,如果调试的话,每点一次编辑多选值,都会执行ShowUpdateMulitValues方法,相应的用户控件数据也会改变为相应的数据。但是不用调试,直接运行,不管怎么点,显示的都是第一次的数据。很纳闷,请有相似经验的指教。

解决方案 »

  1.   

    问题是,如果调试的话,每点一次编辑多选值,都会执行ShowUpdateMulitValues方法,相应的用户控件数据也会改变为相应的数据。但是不用调试,直接运行,不管怎么点,显示的都是第一次的数据。很纳闷,请有相似经验的指教。你调试走完后在页面可以看到效果吗?
      

  2.   

    楼主是要实现局部刷新 ??
    使用ajax ??
      

  3.   

    调试完成页面可以看见效果。不调试数据就不会更新。
    回7楼的,不是要Ajax
      

  4.   

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PropertyValuesManager.ascx.cs" Inherits="ADOnline.Web.FileManager.UserControl.PropertyValuesManager" %>
    <div id="propertyValue" runat="server">
            <asp:GridView ID="GridView2" Width="100%" DataKeyNames="PropertyValues_ID,PV_State,PV_FatherID" runat="server" AutoGenerateColumns="False" OnRowUpdating="GridView2_RowUpdating" DataSourceID="pvm_ObjectDataSource" OnRowCommand="GridView2_RowCommand" OnRowDataBound="GridView2_RowDataBound" >
                <Columns>
                    <asp:BoundField DataField="PV_Value" HeaderText="属性值" />
                    <asp:BoundField HeaderText="父属性名称" DataField="PV_ExtendName" ReadOnly="True" />
                    <asp:TemplateField HeaderText="父属性值">
                        <EditItemTemplate>
                            <asp:DropDownList ID="pvm_ddlValues" runat="server" Width="106px" SelectedValue='<%# Eval("PV_FatherID") %>' DataTextField="PV_Value" DataSource="<%# PV_List %>" DataValueField="PropertyValues_ID">
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("PV_FatherValue") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="操作">
                        <ItemTemplate>
                            <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="edit" Font-Size="14px">编辑</asp:LinkButton>&nbsp;
                            <asp:LinkButton ID="lbtnUse" runat="server" CommandName="use" Font-Size="14px"  CommandArgument='<%# Eval("PropertyValues_ID")+","+((GridViewRow)Container).RowIndex %>'>启用</asp:LinkButton>
                            <asp:LinkButton ID="lbtnStop" runat="server" CommandName="stop" Font-Size="14px" CommandArgument='<%# Eval("PropertyValues_ID")+","+((GridViewRow)Container).RowIndex %>'>禁用</asp:LinkButton>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:LinkButton ID="lbtnSave" runat="server" CommandName="update" Font-Size="14px">更新</asp:LinkButton>&nbsp;
                            <asp:LinkButton ID="lbtnCancel" runat="server" CommandName="cancel" Font-Size="14px">取消</asp:LinkButton>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <RowStyle HorizontalAlign="Center" ForeColor="#0066CC" Font-Size="14px" Height="35px" BackColor="#E9F2FF" VerticalAlign="Middle"/>
                <AlternatingRowStyle BackColor="White" />
                <HeaderStyle ForeColor="White" BackColor="#507CD1" Font-Size="14px" Height="35px" />
            </asp:GridView>
        <asp:ObjectDataSource ID="pvm_ObjectDataSource" runat="server" SelectMethod="GetExtendValues"
            TypeName="ADOnline.Bll.BPropertyValues">
            <SelectParameters>
                <asp:Parameter Name="property_id" Type="Int32" />
                <asp:Parameter DefaultValue="-1" Name="state" Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>
            <asp:TextBox ID="txtValue" runat="server"></asp:TextBox>&nbsp;
            <asp:DropDownList ID="ddlFatherValue" runat="server" Width="148px" DataTextField="PV_Value" DataValueField="PropertyValues_ID">
            </asp:DropDownList>&nbsp;
            <asp:Button ID="btnAdd" runat="server" Text="添 加" OnClick="btnAdd_Click" />
            <input type="hidden" id="hidIsEdit" runat="server" value="0" />
    </div> 
    页面代码
      

  5.   


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using ADOnline.Model;
    using ADOnline.Web.Common;
    using ADOnline.Bll;
    using System.Collections.Generic;
    using System.Reflection;namespace ADOnline.Web.FileManager.UserControl
    {
        public partial class PropertyValuesManager : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (ViewState["property_id"] == null)
                    ViewState.Add("property_id", -1);
                if (ViewState["extend_id"] == null)
                    ViewState.Add("extend_id", -1);
                if (ViewState["pv_list"] == null)
                    ViewState.Add("pv_list", new List<PropertyValues>());
                InitPage();
            }        #region 页面属性
            public int Property_ID//属性编号
            {
                get { return Convert.ToInt32(ViewState["property_id"]); }
                set { ViewState["property_id"] = value; }
            }
            public int Extend_ID//继承属性编号
            {
                get { return Convert.ToInt32(ViewState["extend_id"]); }
                set{ViewState["extend_id"]=value;}
            }
            public List<PropertyValues> PV_List//父属性值集合
            {
                get { return (List<PropertyValues>)ViewState["pv_list"]; }
                set { ViewState["pv_list"] = value; }
            }
            #endregion
            /// <summary>
            /// 初始化界面
            /// </summary>
            protected void InitPage()
            {
                this.pvm_ObjectDataSource.SelectParameters["property_id"].DefaultValue = Property_ID.ToString();
                if (Extend_ID >0)
                {
                    PV_List = BPropertyValues.GetExtendValues(Extend_ID,1);
                    this.ddlFatherValue.DataSource = PV_List;
                    this.ddlFatherValue.DataBind();
                }
                else
                    this.ddlFatherValue.Enabled = false;
            }        /// <summary>
            /// 添加新的属性值
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                PropertyValues pv = new PropertyValues();
                pv.Property_ID = Property_ID;
                pv.PV_FatherID = this.ddlFatherValue.SelectedValue == DBNull.Value.ToString() ? 0 : Convert.ToInt32(this.ddlFatherValue.SelectedValue);
                pv.PV_ProExtendID = Extend_ID < 0 ? 0 : Extend_ID;
                pv.PV_Value = this.txtValue.Text;
                pv.PV_State = 1;
                BPropertyValues.Add(pv);
                Response.Write("<script>alert('添加成功!');</script>");
                this.txtValue.Text = string.Empty;
            }        /// <summary>
            /// 数据保存事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                PropertyValues pv = new PropertyValues();
                pv.Property_ID = Property_ID;
                pv.PropertyValues_ID = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Values[0].ToString());
                pv.PV_ProExtendID = Extend_ID == -1 ? 0 : Extend_ID;
                pv.PV_Value = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[0].Controls[0])).Text;
                string fatherid = ((DropDownList)GridView2.Rows[e.RowIndex].FindControl("pvm_ddlValues")).SelectedValue;
                pv.PV_FatherID = (fatherid == DBNull.Value.ToString() ? 0 : Convert.ToInt32(fatherid));
                pv.PV_State = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Values[1].ToString());            BPropertyValues.Update(pv);
                
                this.GridView2.EditIndex = -1;
                e.Cancel = true;            
            }        /// <summary>
            /// 行命令事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "stop")
                {
                    string[] argvalues = e.CommandArgument.ToString().Split(',');
                    int pro_values_id = Convert.ToInt32(argvalues[0]);
                    BPropertyValues.UpdatePVState(0, pro_values_id);
                }
                else if (e.CommandName == "use")
                {
                    string[] argvalues = e.CommandArgument.ToString().Split(',');
                    int pro_values_id = Convert.ToInt32(argvalues[0]);
                    BPropertyValues.UpdatePVState(1, pro_values_id);
                }
            }        /// <summary>
            /// Gridview行绑定事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.RowIndex != GridView2.EditIndex)
                    { 
                        if (GridView2.DataKeys[e.Row.RowIndex].Values[1].ToString() == "0")
                        {
                            ((LinkButton)e.Row.FindControl("lbtnUse")).Visible = true;
                            ((LinkButton)e.Row.FindControl("lbtnStop")).Visible = false;
                        }
                        else
                        {
                            ((LinkButton)e.Row.FindControl("lbtnUse")).Visible = false;
                            ((LinkButton)e.Row.FindControl("lbtnStop")).Visible = true;
                        }
                    }
                }
            }
        }
    }后台---用户控件
      

  6.   

    Page_Load中,你不防加上if(!IsPostBack)试试我想应该是这里了……
      

  7.   

    问题已解决,
    /// <summary>
            /// 初始化界面
            /// </summary>
            protected void InitPage()
            {
                this.pvm_ObjectDataSource.SelectParameters["property_id"].DefaultValue = Property_ID.ToString();
                GridView2.DataBind();用户控件首次加载会自动更新Gridview2,但是后来都不会了,这里就手动指定加载就可以了。谢谢各位关注
    结贴
      

  8.   

    这里是万不能加IsPostBack的,调试下就明白了。谢谢