开发者

clicking on text of custom control checkboxlist sends null event target

I have some custom controls. My control is inherit from CompositControl. In my page, I create them in On_Init(). I insert these controls to a place holder. When I click on the checkbox itself, it sends correct postback, but when I click on the text next to the checkbox, I have 2 postbacks: one with event target equal null and the other with correct ID. And when it sends the postback with correct ID, Page.IsPostBack is false, which is totally incorrect. I want the click on the text behave like the click on the check box. How do I do that?

Thanks in advance.

This is how I creat the checkboxlist:

protected override void CreateChildControls() 
{
    Controls.Clear();

    _lQuestion = new Label();
    _lQuestion.ID = "quest"+QuestionID;
    _lQuestion.Text = Question
        + QuestionID //for debugging
        ;
    _lQuestion.CssClass = EddQuestionareStyle.EddQuestionStyle.ToString();
    _cbl = new CheckBoxList();
    _cbl.ID = "ans"+QuestionID;
    for( int i=0; i<_PossibleAnswers.Count();i++)
    {
        AnswerOption a = _PossibleAnswers[i];
        _cbl.Items.Add(a.Value.ToString());
    }
    _cbl.CssClass = EddQuestionareStyle.EddAnswerStyle.ToString();
    _cbl.SelectedIndexChanged += new EventHandler(cbl_SelectedIndexChanged);
    Page.RegisterRequiresPostBack(_cbl);//will call LoadPostData

    Controls.Add(new LiteralControl("<br>"));
    Controls.Add(_lQuestion);
    Controls.Add(new LiteralControl("<br>"));
    Controls.Add(_cbl);

    ChildControlsCreated = true;
}

This is my AddAttributesToRender:

    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {

        writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
        writer.AddAttribute(HtmlTextWriterAttribute.Type, this.GetType().Name);
        writer.AddAttribute(Html开发者_如何学CTextWriterAttribute.Onclick,
            Page.ClientScript.GetPostBackEventReference(this, this.UniqueID));
        base.AddAttributesToRender(writer);
    }

It will render html like this:

    <br />
<span id="214" onclick="__doPostBack('214','214')" type="MyCheckBoxList" name="214">
    <br />
    <span class="EddQuestionStyle" id="214_quest21">Where will the money deposited in your
        account(s) come from? Select all that apply.21</span><br />
    <table class="EddAnswerStyle" id="214_ans21" border="0">
        <tbody>
            <tr>
                <td>
                    <input id="214_ans21_0" type="checkbox" checked name="214$ans21$0" value="on" /><label
                        for="214_ans21_0">Payroll</label>
                </td>
                <td>
                    <input id="214_ans21_4" type="checkbox" checked name="214$ans21$4" value="on" /><label
                        for="214_ans21_4">Cash</label>
                </td>
                <td>
                    <input id="214_ans21_7" type="checkbox" name="214$ans21$7" value="on" /><label for="214_ans21_7">Gifts</label>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="214_ans21_1" type="checkbox" checked name="214$ans21$1" value="on" /><label
                        for="214_ans21_1">Payments other than Payroll</label>
                </td>
                <td>
                    <input id="214_ans21_5" type="checkbox" name="214$ans21$5" value="on" /><label for="214_ans21_5">Account
                        Transfers</label>
                </td>
                <td>
                    <input id="214_ans21_8" type="checkbox" name="214$ans21$8" value="on" /><label for="214_ans21_8">Loans</label>
                </td>
            </tr>

        </tbody>
    </table>

when I click on the checkbox Payroll, for example, I have a postback with ID 214, which is the span ID. When I click on the text "Payroll", I have 2 postback with event target null and 214. What did I do wrong? Thanks


I resolved my own issue by remove this line of code:

writer.AddAttribute(HtmlTextWriterAttribute.Onclick, 
        Page.ClientScript.GetPostBackEventReference(this, this.UniqueID));

it caused an extra postback. And I realized that I didn't have to write that code for RadioButtonList either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜