2010-10-08 29 views
0

我有一些自定义控件。我的控件继承自CompositControl。在我的页面中,我在On_Init()中创建它们。我将这些控件插入占位符。当我单击复选框本身时,它会发送正确的回发,但是当我单击复选框旁边的文本时,我有2个回发:一个事件目标等于null,另一个具有正确的ID。当它发送正确的回传ID时,Page.IsPostBack为false,这完全不正确。我希望点击文本的行为就像点击复选框。我怎么做?点击自定义控件checkboxlist的文本发送null事件目标

在此先感谢。

这是我创造的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; 
} 

这是我的AddAttributesToRender:

protected override void AddAttributesToRender(HtmlTextWriter writer) 
    { 

     writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID); 
     writer.AddAttribute(HtmlTextWriterAttribute.Type, this.GetType().Name); 
     writer.AddAttribute(HtmlTextWriterAttribute.Onclick, 
      Page.ClientScript.GetPostBackEventReference(this, this.UniqueID)); 
     base.AddAttributesToRender(writer); 
    } 

,它将使HTML这样的:

<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> 

当我点击复选框Payroll,例如,我有一个ID为214的回传,它是范围ID。当我点击文本“工资单”时,我有2个回复,事件目标为null,214。我做错了什么? 感谢

回答

0

我解决我自己的问题,通过删除此行的代码:

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

它造成额外的回传。我意识到我不必为RadioButtonList写代码。

相关问题