我正在加载Datalist以为网格创建必要的过滤器(文本框,下拉列表,复选框等)。到目前为止一直很好,问题出在回发之间。 我知道每次回发发生时我都需要用项目重新创建Datalist,但是如何在每个控件上存储用户输入的内容?
想象一下,我创建了一个文本框,用于过滤列和用户输入" test"在文本框上,单击触发事件的按钮以搜索gridview中的值。
我如何使用(如果是这样)" viewstate"什么时候?
这是代码
客户代码:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="imgHelp" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnlFilter" runat="server" Width="95%">
<asp:DataList ID="dlFilter" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" OnItemDataBound="dlFilter_ItemDataBound" OnItemCreated="dlFilter_ItemCreated" EnableViewState="true">
<ItemTemplate>
<asp:HiddenField ID="hfFilterName" runat="server" Value='<%# Bind("szFilterName") %>' />
<asp:HiddenField ID="hfFilterType" runat="server" Value='<%# Bind("szFilterObjType") %>' />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("szFilterFiller") %>' />
</ItemTemplate>
<FooterTemplate>
<table>
<tr>
<td valign="middle" style="width: 120px; text-align: right;">
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Font-Names="Verdana" Font-Size="7pt" OnClick="btnSubmit_Click"
Text="Search" />
</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
</asp:Panel>
<br />
<div id="div1" style="overflow: auto; height: auto; width: 97%;">
<asp:GridView ID="gv" runat="server" CssClass="tablecloth-theme" Width="97%" GridLines="Vertical"
AllowSorting="True" AllowPaging="True" PageSize="20" OnPageIndexChanging="gv_PageIndexChanging"
OnSorting="gv_Sorting" SelectedRowStyle-BackColor="Beige" OnSelectedIndexChanged="gv_SelectedIndexChanged">
<HeaderStyle BackColor="#7799AF" Font-Bold="True" Height="20px" Font-Names="Verdana"
Font-Size="7pt" ForeColor="White" HorizontalAlign="Left" />
<Columns>
<asp:CommandField SelectText="Details" ShowSelectButton="True" />
</Columns>
<AlternatingRowStyle BackColor="Gainsboro" />
<SelectedRowStyle BackColor="Beige" />
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
服务器代码:
protected void Page_Load(object sender, EventArgs e)
{
try
{
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
if (!scriptManager.IsInAsyncPostBack)
{
DataInfo_Load();
}
//Page_Prepare();
}
catch (Exception ex)
{
ShowError(ex);
}
}
protected void DataInfo_Load()
{
try
{
DataSet_Load();
Filters_Load();
Grid_Load();
if ((int)ViewState["PageStatus"] >= 2)
{
if (ViewState["Saveable"] != null)
btnNew.Visible = (bool)ViewState["Saveable"];
else
btnNew.Visible = true;
}
else
btnNew.Visible = false;
}
catch (Exception ex)
{
throw ex;
}
}
protected void DataSet_Load()
{
try
{
Connection cn = new Connection();
DataSet ds = null;
string Query = hfSelStore.Value;
SqlCommand SQLcmd = new SqlCommand(Query);
SQLcmd.CommandType = System.Data.CommandType.StoredProcedure;
ds = cn.ExecuteSqlCmd(cn.SqlLocalConn, SQLcmd);
ViewState["DataSet"] = ds;
}
catch (Exception ex)
{
throw ex;
}
}
protected void Filters_Load()
{
try
{
if (ViewState["DataSet"] == null)
{
throw new Exception("No DataSet!");
}
DataSet ds = (DataSet)ViewState["DataSet"];
if (ds == null)
{
throw new Exception("DataSet is empty!");
}
if (ds.Tables.Count == 5)
{
//contains filter
pnlFilter.Visible = true;
DataView view = new DataView(ds.Tables[4]);
view.Sort = "lId";
dlFilter.DataSource = view;
dlFilter.DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void dlFilter_ItemDataBound(object sender, DataListItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve the Label control in the current DataListItem.
HiddenField hfFilterName = (HiddenField)e.Item.FindControl("hfFilterName");
HiddenField filtertype = (HiddenField)e.Item.FindControl("hfFilterType");
if (filtertype != null)
{
DataListItem dlt = ((filtertype.Parent) as DataListItem);
int itemIndex = dlt.ItemIndex;
Label lbl = new Label();
lbl.ID = "lbl";
lbl.Text = hfFilterName.Value;
lbl.Attributes.Add("style", "font-size:7pt; color:#005780; font-weight:bold;");
Control ctr = Control_Create(filtertype.Value);
ctr.ID = "val";
dlt.Controls.Add(new LiteralControl("<table><tr><td valign=\"middle\" style=\"width: 120px; text-align: right;\">"));
dlt.Controls.Add(lbl);
dlt.Controls.Add(new LiteralControl("</td><td>"));
dlt.Controls.Add(ctr);
dlt.Controls.Add(new LiteralControl("</td></tr></table>"));
}
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
protected Control Control_Create(string controlType)
{
Control c = new TextBox();
try
{
if (controlType == "TextBox")
c = new TextBox();
else if (controlType == "CheckBox")
c = new CheckBox();
else if (controlType == "DropDownList")
c = new DropDownList();
else if (controlType == "RadioButton")
c = new RadioButton();
//else if (controlType == "Checkbox")
// c = new TextBox();
//else if (controlType == "Checkbox")
// c = new TextBox();
}
catch (Exception ex)
{
throw ex;
}
return c;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
gv.SelectedIndex = -1;
gv.PageIndex = 0;
Grid_Load();
}
catch (Exception ex)
{
ShowError(ex);
}
}
你可以帮帮我吗?
提前致谢!
答案 0 :(得分:0)
首先,您不需要在每个回发上加载listview。 ListView能够在PostBack期间保存状态信息。但是,您不应该在每个回发上调用DataBind()函数,如果这样做,它将丢失状态信息。检查你绑定ListView的位置
关于添加到ListView的动态控件,如果你现在正在将它们添加到ItemDataBound事件中,如果在第一页加载期间调用DataBind()函数,则它们在回发期间保持状态,即 if(!IsPostBack)代码块。
另一点是尽可能避免在代码隐藏中使用html创建控件。您可以在ItemTemplate中添加这些控件,并在后面的代码中访问它们。如果要在列表视图下创建多个控件(如表),可以在listview的ItemTemplate中添加转发器控件,并在ListView的OnItemDataBound事件中绑定转发器。
由于我没有看到像Control_Create函数这样的完整代码,因此我无法协助或显示有关如何在ListView中使用Repeater控件的代码。