ASP复合控件的子控件(单选)失去选定值 [英] Asp Composite control child control (radiobutton) losing checked value

查看:173
本文介绍了ASP复合控件的子控件(单选)失去选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在asp.net测验与控制动态创建的问题和选项。
主要的控制基本上容纳所有的问题的容器。
在设计视图用户可以通过自定义集合编辑器中添加的问题。
每次我加一个问题,它会产生一个问题标记为我收集编辑器列表。
在每个问题对象是一个标签和继承的单选框控制选对象的 N 的数量。反过来每个选项的对象重新present一个选项,用户可以对每个问题的选择。

I am working on a quiz control in asp.net with dynamically created questions and options. The main control is basically a container to hold all of the questions. In design view users can add questions through a custom Collection Editor. Everytime i add a question to the collection editor list it generates a question tag for me. Inside each question object is a label and a n amount of Option objects that inherit the Radiobutton Control. Each of these Option objects in turn represent a option the user can select for each question.

但现在我在这里我希望能够读取每个单选按钮的选定值的一部分。这所有的作品。当我想实现一个页面内本测验,检查我想提出一个按钮,该页面中的问题,并调用下面的函数,它是控制内:

This all works except i am now at the part where i want to be able to read the Checked value of each radiobutton. When i want to implement this quiz inside a page and check the questions i want to put a button in this page and call the following function that is inside the control:

$

    public String checkQuestions()
    {


        if (questions != null)
        {

            foreach (Question question in questions)
            {

                options = question.readOptions();

                int i = 0;
                foreach (Option option in options)
                {
                    testLabel.Text = option.Checked.ToString(); // test purposes only
                }

            }



        }
       return errors;

    }

然而,一旦我选择一个单选按钮,然后单击提交按钮选中的值总是会变成假的所有选项。
基本上,它是回发后失去其选中的值,我正好卡在试图解决它。
将AP preciate它,如果任何人都可以点我在正确的方向。

However once i select a radiobutton and click on the submit button the Checked value will always turn out false for all of the options. Basically it is losing its checked value after a Postback and i am just stuck in trying to solve it. Would appreciate it if anyone could point me in the right direction.

推荐答案

乍一看,有两件事情我会检查。首先,确保你实施 IPostBackDataHandler 。这需要你实现两个方法,的LoadPostData RaisePostDataChangedEvent 。在我的第一个猜测,第一种可能是你的问题的根源。

At a first glance, there are two things I'd check. Firstly, make sure you're implementing IPostBackDataHandler. this requires you to implement two methods, LoadPostData and RaisePostDataChangedEvent. At my first guess, the first one is probably the source of your problem.

的LoadPostData 需要一个字符串 postDataKey 和<一个href=\"http://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx\"相对=nofollow> 的NameValueCollection postCollection 并返回 BOOL 指示的值是否已改变回发的结果。你不会的需求的这一实施净原本打算的方式,比如我创建了多次召开单选按钮(即对于在这里并不重要不能仅仅是一个原因,控制单选按钮列表控制)等确保他们都是由一个属性名为串组名并视察了 postCollection 组名

LoadPostData takes a string postDataKey and a NameValueCollection postCollection and returns a bool indicating whether or not the value has changed as a result of the postback. You don't need to implement this the way .Net originally intends, for example I created a control that held several radio buttons (that for reasons that aren't important here couldn't simply be a RadioButtonList control) and so made sure they were all named by a property string GroupName and inspected the postCollection for that GroupName:

public bool LoadPostData(string postDataKey,
    System.Collections.Specialized.NameValueCollection postCollection)
{
    bool oldValue = _isChecked;

    postCollection = HttpContext.Current.Request.Form; // See note below

    _isChecked = (postCollection[this.GroupName] == this.Text);

    return oldValue == _isChecked;
}

您会注意到,我重新定义了 postCollection 在这里;这是因为 postCollection 仅包含的Htt prequest.Form 对应什么ASP.Net认为的一个子集你的控制应该关心。正如你也在这里建设一个复合控件,你可能想这样做。

You'll notice that I'm redefining the postCollection here; this is because postCollection only contains a subset of the HttpRequest.Form corresponding to what ASP.Net thinks your control should care about. As you're also building a composite control here, you probably want to do the same.

如果这不起作用第一次轮不要担心;它的价值是通过什么被传递到这个方法在调试模式步进(或输出的东西到 HttpContext.Trace ,我经常发现更容易),看看为什么你的code是你需要的并不完全符合。

Don't worry if this doesn't work first time round; it's worth stepping through what gets passed into this method in debug mode (or outputting things to the HttpContext.Trace, which I often find easier) to see why your code isn't quite what you need.

最后一件事:的LoadPostData 如果提交的表单包含与相匹配的一个名称字段只叫的UniqueID 你的控制。当你的控制是一个复合控件,你可能想牛仔小幅,像这样:

One last thing: LoadPostData is only called if the posted form contains a field with a name which matches the UniqueID of your control. As your control is a composite control, you might want to cowboy this slightly, like so:

protected override void Render(HtmlTextWriter writer)
{
    base.Render(writer);

    writer.WriteBeginTag("input");
    writer.WriteAttribute("type", "hidden");
    writer.WriteAttribute("name", this.UniqueID);
    writer.WriteAttribute("value", "post");
    writer.Write(" />");
}

这是一个肮脏的黑客,但它会工作; O)

It's a dirty hack, but it'll work ;o)

如果处理回发手动不能解决你的问题,它可能是你需要惹控件的视图状态。不要担心,这是隔靴搔痒,因为它似乎可怕,只要你遵循一些简单的规则。

If handling the postback manually doesn't solve your problem, it might be that you need to mess with the viewstate of your control. Don't worry, this is nowhere near as scary as it seems, provided you follow a few simple rules.

要手动处理您的视图状态,你只需要重写两个方法调用,很明显的是, LoadViewState SaveViewState 。首先需要视图状态膨胀和其他回报同样对象结构的对象。如果您包含您需要保存所有需要坚持的重要特性的结构,你的 SaveViewState 覆盖一些回报,那么你只需要再次夸大它的 LoadViewState 方法。

To handle your viewstate manually, you just need to override two methods called, obviously enough, LoadViewState and SaveViewState. The first takes an object of viewstate to inflate and the other returns that same object structure. If you make your SaveViewState override return something containing the structure you need to save all the important properties that need persisting, then you just inflate it again in your LoadViewState method.

下面是其中第一个狡猾的伎俩来了。还有,你应该保存视图状态使用特定的数据类型,你永远不应该使用任何其他类型的(因为其他类型存储真正低效)。这可能是最有用的你是 <$类型C $ C> System.Web.UI.Pair <​​/ code> ,的 System.Web.UI.Triplet 以及我们的老朋友的 System.Collections.ArrayList 和的 System.Collections.Hashtable 。对,三胞胎简单地存储类型的两个或三个值对象;的ArrayList是一个有效的列表与LT;对象&gt;

Here's where the first of the cunning tricks comes up. There are certain datatypes that you should use for saving viewstate and you should never use any other type (because other types are stored really inefficiently). The types that will probably be most useful to you are System.Web.UI.Pair, System.Web.UI.Triplet and our old friends System.Collections.ArrayList and System.Collections.Hashtable. Pairs and Triplets simply store two or three values of type object; ArrayLists are effectively a List<object>.

我猜,在你的情况,你可能想将任一(1)布尔标志的ArrayList,存储你的单选按钮或(2)字符串或整数的ArrayList的checkedness,存储的ID被检查的单选按钮或索引。

I'd guess that, in your circumstance, you probably want to store either (1) an ArrayList of boolean flags, storing the "checkedness" of your radiobuttons or (2) an ArrayList of strings or ints, storing the IDs or index of the checked radiobuttons.

在我前面提到的控制,我只需要存储checkedness和文本属性,所以我的 LoadViewState SaveViewState 方法是这样的:

In the control I mentioned earlier, I just needed to store the checkedness and the Text property, so my LoadViewState and SaveViewState methods looked like this:

protected override void LoadViewState(object savedState)
{
    Pair state = savedState as Pair;

    if (state != null)
    {
        _isChecked = state.First as Nullable<bool> ?? false;
        this.Text = state.Second as string;
    }
}


protected override object SaveViewState()
{
    return new Pair(_isChecked, this.Text);
}

此外,如果这不起作用第一次,你几乎可以肯定想一步通过code或扔东西到跟踪。重要的是,你可能想避免这些方法抛出异常,如果你的视图状态已损坏或不存在或什么的。

Again, if this doesn't work first time, you almost certainly want to step through the code or throw things into the Trace. Importantly, you probably want to avoid throwing Exceptions from these methods, in case your viewstate is corrupt or non-existent or something.

有几个非常有用的文章我一直书签时,我与视图状态搞乱了。第一个解释了为什么你应该只(存储某些类型的视图状态就像使用的ArrayList 的Hashtable ,而不是列表&LT; T&GT; 词典&LT; TKEY的,TValue&GT; ),第二个是如何一个良好的深入说明这一切的视图状态的东西实际工作。

There are a couple of very useful articles I keep bookmarked for when I'm messing with viewstate. The first one explains about why you should only store certain types in the viewstate (like using ArrayList and Hashtable, rather than List<T> and Dictionary<TKey, TValue>) and the second is a good in-depth explanation of how all this viewstate stuff actually works.


  1. 不要让BinaryFormatter的得到它!

  2. 真正了解的ViewState

  1. Don't let the BinaryFormatter get at it!
  2. Truly understanding ViewState

我希望这一切可以帮助解决你的问题。

I hope all this helps resolve your problem.

这篇关于ASP复合控件的子控件(单选)失去选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆