ASP.net下拉动态样式,然后记住中止提交的样式

时间:2022-11-19 22:11:43

So, I've got an ASP drop down list (this is .net 2.0). I'm binding it with data. Basically, when the page loads and it's not a post back we'll fetch record data, bind all the drop downs, and set them to their appropriate values (strictly speaking we: initialize page with basic set of data from DB, bind drop downs from DB, fetch actual record data from DB, set drown downs to appropriate settings at this time). What I want to do is selectively style the list options. So the database returns 3 items: ID, Text, and a flag indicating whether I the record is "active" (and I'll style appropriately). It's easy enough to do and I've done it.

所以,我有一个ASP下拉列表(这是.net 2.0)。我用数据绑定它。基本上,当页面加载并且它不是回发时我们将获取记录数据,绑定所有下拉列表,并将它们设置为适当的值(严格来说,我们:使用来自DB的基本数据集初始化页面,绑定下拉列表从DB,从DB获取实际记录数据,此时将淹没设置为适当的设置)。我想要做的是选择性地设置列表选项的样式。因此,数据库返回3个项目:ID,文本和一个标志,指示我的记录是否“活动”(并且我将适当地设置样式)。这很容易做到,我已经做到了。

My problem is what happens when a form submission is halted. We have slightly extended the Page class and created an AddError() method, which will create a list of errors from failed business rule checks and then display them in a ValidationSummary. It works something like this, in the submit button's click event:

我的问题是表单提交停止时会发生什么。我们稍微扩展了Page类并创建了一个AddError()方法,该方法将从失败的业务规则检查中创建错误列表,然后在ValidationSummary中显示它们。在提交按钮的单击事件中,它的工作方式如下:

CheckBizRules();

if(Page.IsValid)
{
    SaveData();
}

If any business rule check fails, the Page will not be valid. The problem is, when the page re-renders (viewsate is enabled, but no data is rebound) my beautiful conditional styling is now sadly gone, off to live in the land of the missing socks. I need to preserve it.

如果任何业务规则检查失败,则页面将无效。问题是,当页面重新渲染(视图已启用,但没有数据反弹)时,我美丽的条件样式现在可悲地消失了,生活在失踪袜子的土地上。我需要保留它。

I was hoping to avoid another DB call here (e.g. getting the list data back from the DB again if the page isn't valid, just for purposes of re-styling the list). But it's not the end of the world if that's my course of action. I was hoping someone might have an alternative suggestion.

我希望在这里避免另一个数据库调用(例如,如果页面无效,则再次从数据库中获取列表数据,仅用于重新设置列表样式)。但如果这是我的行动方针,那不是世界末日。我希望有人可能有其他建议。

I couldn't think of how to phrase this question better, if anyone has any suggestions or needs clarification don't hesitate to get it, by force if need be. ;)

我想不出如何更好地表达这个问题,如果有人有任何建议或需要澄清,请不要犹豫,如果需要,可以通过武力。 ;)

2 个解决方案

#1


I'm not sure I completelly understand what kind of styling you apply to your drop-down items, but it seems this style is something that the control does not preserve accross postbacks. Usually this kind of info will therefore need to be saved in the ViewState. I see two options (other than re-loading from DB):

我不确定我是否完全理解你应用于你的下拉项目的样式,但似乎这种风格是控件不会保留回发的东西。因此,通常需要将此类信息保存在ViewState中。我看到两个选项(除了从DB重新加载):

First method: Create your own drop-down control that inherits from DropDownList. Then save styling data in the control's ViewState bag when styling the items:

第一种方法:创建自己的DropDownList继承的下拉控件。然后在设置项目样式时将样式数据保存在控件的ViewState包中:

public void SetItemActive(ListItem item)
       {
         ViewState[item.Value] = "active";           
        }

then override the OnRender

然后覆盖OnRender

protected override void Render(HtmlTextWriter writer)
{

   ....

   foreach(ListItem item in Items)
   {
    if ( ViewState[item.Value] == "active")
    { 
            ** RenderActiveItem **
    }
    else 
    {
            ** RenderNormalItem **

    }

}

Second method: is to save the active ID's in the Page's ViewState then re-style the dropdown on each postback using the data from the ViewState rather than from the DB

第二种方法:将活动ID保存在Page的ViewState中,然后使用ViewState中的数据而不是数据库中的数据重新设置每个回发的下拉样式

#2


Well, I couldn't come up with anything except to go to the database to re-retrieve my list data when the Page was not valid, and re-style the control.

好吧,除了在页面无效时去数据库重新检索我的列表数据,我无法想出任何东西,并重新设置控件的样式。

#1


I'm not sure I completelly understand what kind of styling you apply to your drop-down items, but it seems this style is something that the control does not preserve accross postbacks. Usually this kind of info will therefore need to be saved in the ViewState. I see two options (other than re-loading from DB):

我不确定我是否完全理解你应用于你的下拉项目的样式,但似乎这种风格是控件不会保留回发的东西。因此,通常需要将此类信息保存在ViewState中。我看到两个选项(除了从DB重新加载):

First method: Create your own drop-down control that inherits from DropDownList. Then save styling data in the control's ViewState bag when styling the items:

第一种方法:创建自己的DropDownList继承的下拉控件。然后在设置项目样式时将样式数据保存在控件的ViewState包中:

public void SetItemActive(ListItem item)
       {
         ViewState[item.Value] = "active";           
        }

then override the OnRender

然后覆盖OnRender

protected override void Render(HtmlTextWriter writer)
{

   ....

   foreach(ListItem item in Items)
   {
    if ( ViewState[item.Value] == "active")
    { 
            ** RenderActiveItem **
    }
    else 
    {
            ** RenderNormalItem **

    }

}

Second method: is to save the active ID's in the Page's ViewState then re-style the dropdown on each postback using the data from the ViewState rather than from the DB

第二种方法:将活动ID保存在Page的ViewState中,然后使用ViewState中的数据而不是数据库中的数据重新设置每个回发的下拉样式

#2


Well, I couldn't come up with anything except to go to the database to re-retrieve my list data when the Page was not valid, and re-style the control.

好吧,除了在页面无效时去数据库重新检索我的列表数据,我无法想出任何东西,并重新设置控件的样式。