在一个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方法,相应的用户控件数据也会改变为相应的数据。但是不用调试,直接运行,不管怎么点,显示的都是第一次的数据。很纳闷,请有相似经验的指教。
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方法,相应的用户控件数据也会改变为相应的数据。但是不用调试,直接运行,不管怎么点,显示的都是第一次的数据。很纳闷,请有相似经验的指教。
解决方案 »
- asp.net 页面加载问题
- Forms身份验证情况下,能否自定义实现IIdentity接口,在Cookie中保存用户正确登录后的更多信息?
- 一个如何从sqlsever中取出图像的问题?????急
- .net2005中文正式版哪天发布?
- 100分解决上传服务器后出现的问题!
- 祝贺女排夺灌,顺便问两个笨问题,怎么判断一个数能不能被另一个数整除?还有一个,谢谢
- 用C#写的ASPX网页怎么在代码里面控制页面刷新?
- 太奇怪了,DataList的模板列里放个HyperLink,在ItemDataBound和ItemCreated事件里对它的NavigateUrl属性附了值,但是运行后这个属性的值
- datagrid中的templatecolumn
- 请高手解答一下这个怎么用
- MVC结构中的
- 做个信息网站,每条信息一个唯一编号。如何实现比较好?
使用ajax ??
回7楼的,不是要Ajax
<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>
<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>
<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>
<asp:DropDownList ID="ddlFatherValue" runat="server" Width="148px" DataTextField="PV_Value" DataValueField="PropertyValues_ID">
</asp:DropDownList>
<asp:Button ID="btnAdd" runat="server" Text="添 加" OnClick="btnAdd_Click" />
<input type="hidden" id="hidIsEdit" runat="server" value="0" />
</div>
页面代码
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;
}
}
}
}
}
}后台---用户控件
/// <summary>
/// 初始化界面
/// </summary>
protected void InitPage()
{
this.pvm_ObjectDataSource.SelectParameters["property_id"].DefaultValue = Property_ID.ToString();
GridView2.DataBind();用户控件首次加载会自动更新Gridview2,但是后来都不会了,这里就手动指定加载就可以了。谢谢各位关注
结贴