我有一个网页,其中包含一个动态构建的数据网格,具体取决于所选的日期和过滤器。在itemdatabound上,为每个单元格创建了一个linkbutton,因此单击时将打开一个模态弹出窗口,其中包含可以修改的详细信息。我遇到的问题是,当我更改过滤器(datepicker,btnShow)时,页面绑定两次并复制网格中的列。如果我放了一个!= page.ispostback,当我点击一个单元格链接按钮时,数据网格就会消失。 请帮忙
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Frm.aspx.cs" Inherits="web.Frm" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript" language="javascript" src="Scripts/script.js"> </script>
<script src="Scripts/jquery-1.8.3.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.9.2.custom.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('#<%= datepicker.ClientID %>').datepicker({ changeMonth: true, changeYear: true });
});
</script>
<asp:ScriptManager runat="server" ID="scriptManager" EnablePageMethods="true">
</asp:ScriptManager>
<asp:Button ID="btnShowPopupAmend" Style="display: none" runat="server" />
<!--Start:Amend Details-->
<asp:ModalPopupExtender ID="mpeAmend" runat="server" TargetControlID="btnShowPopupAmend"
PopupControlID="pnlpopupAmend" CancelControlID="imgCancel" BackgroundCssClass="modalBackground" />
<asp:Panel ID="pnlpopupAmend" runat="server" Width="700px" Style="display: none;"
class="ModalPanel">
<asp:Panel ID="pnlInner" runat="server" BackColor="white" Width="700px">
<table style="width: 700px;" class="BG">
<tr>
<td width="10px">
</td>
<td align="left">
<asp:Label ID="lbl" runat="server" Text="Amend" CssClass="norm_w" Font-Bold="True"></asp:Label>
</td>
<td align="right">
<asp:Button ID="btnAmendCloseConfirm" runat="server" Text="x" OnClick="imgCancel_Click">
</asp:Button>
</td>
</tr>
</tr>
<table style="width: 700px;">
<tr height="25">
<td width="10px">
</td>
<td>
<asp:Label ID="Label1" runat="server" CssClass="norm"> Name:</asp:Label>
</td>
<td>
<asp:Label ID="LblName" runat="server" CssClass="norm"></asp:Label>
</td>
</tr>
<tr height="25">
<td width="10px">
</td>
<td valign="top">
<asp:Label ID="Label4" runat="server" CssClass="norm"> Address:</asp:Label>
</td>
<td>
<asp:TextBox ID="TxtAddress" runat="server" CssClass="norm_no_border" TextMode="MultiLine"
Rows="4" Width="300px" Style="overflow: hidden"></asp:TextBox>
</td>
</tr>
</table>
<br />
<asp:Button ID="imgCancel" Text="cancel" Height="25px" Width="80px" runat="server"
OnClick="imgCancel_Click" />
</asp:Panel>
</asp:Panel>
<!--End:Amend Details-->
<!--Start:Filters-->
<div>
<asp:Label ID="Lbl1" runat="server" CssClass="norm">Show Details:</asp:Label>
<asp:CheckBox ID="ChkDetails" runat="server"></asp:CheckBox>
<asp:Label ID="Lbl2" runat="server" CssClass="norm"> Show Names:</asp:Label>
<asp:CheckBox ID="ChkNames" runat="server"></asp:CheckBox>
<asp:Button ID="btnShow" Text="Go" runat="server" OnClick="btnShow_Click">
</asp:Button>
</div>
<!--End:Filters-->
<br />
<!--Start:Grid-->
<div>
<asp:Label ID="lblBeginning" runat="server" CssClass="head2"> Week:</asp:Label>
<asp:TextBox ID="datepicker" runat="server" AutoPostBack="true" CssClass="norm"
OnTextChanged="datepicker_TextChanged"></asp:TextBox>
<br />
</div>
<asp:DataGrid ID="dgData" runat="server" CssClass="DG" Width="100%" CellPadding="4"
AutoGenerateColumns="False" GridLines="Vertical" OnItemDataBound="dgData_ItemDataBound">
<AlternatingItemStyle CssClass="Alternating"></AlternatingItemStyle>
<ItemStyle CssClass="Item"></ItemStyle>
<Columns>
<asp:TemplateColumn>
<HeaderStyle CssClass="head"></HeaderStyle>
<ItemStyle CssClass="bdr"></ItemStyle>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<!--End:Grid-->
背后的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Week"] != null)
{
DateTime dDate = DateTime.Parse(Session["Week"].ToString());
datepicker.Text = Utils.GetFirstDateOfWeek(dDate, DayOfWeek.Monday).ToString("dd MMM yyyy");
}
else
{
datepicker.Text = Utils.GetFirstDateOfWeek(DateTime.Now, DayOfWeek.Monday).ToString("dd MMM yyyy");
Session["Week"] = datepicker.Text.ToString();
}
}
BindData(datepicker.Text, ChkDetails.Checked, ChkNames.Checked);
}
protected void datepicker_TextChanged(object sender, EventArgs e)
{
BindData(datepicker.Text, ChkDetails.Checked, ChkNames.Checked);
Session["Week"] = datepicker.Text.ToString();
}
protected void BindData(string sWkDate, Boolean bDetails, Boolean bNames)
{
DataSet ds = null;
ds = ListGridDetails(sWkDate);
// .................
}
protected void dgData_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem | e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.SelectedItem)
{
LinkButton lnk = default(LinkButton);
lnk = new LinkButton();
lnk.Text = "Amend";
lnk.CommandName = "Amend";
lnk.CommandArgument = id;
lnk.Command += new CommandEventHandler(AmendButtonClick);
e.Item.Cells[i].Controls.Add(lnk);
}
}
public void AmendButtonClick(object sender, CommandEventArgs e)
{
//.............
this.mpeAmend.Show();
}
答案 0 :(得分:0)
在回调Page_Load
时也会调用你的绑定逻辑,因为它不在!Page.IsPostBack
下。试试这个
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Week"] != null)
{
DateTime dDate = DateTime.Parse(Session["Week"].ToString());
datepicker.Text = Utils.GetFirstDateOfWeek(dDate, DayOfWeek.Monday).ToString("dd MMM yyyy");
}
else
{
datepicker.Text = Utils.GetFirstDateOfWeek(DateTime.Now, DayOfWeek.Monday).ToString("dd MMM yyyy");
Session["Week"] = datepicker.Text.ToString();
}
BindData(datepicker.Text, ChkDetails.Checked, ChkNames.Checked);
}
}