【发布时间】:2010-10-08 14:48:03
【问题描述】:
我有一些自定义控件。我的控件继承自 CompositControl。在我的页面中,我在 On_Init() 中创建它们。我将这些控件插入占位符。当我单击复选框本身时,它会发送正确的回发,但是当我单击复选框旁边的文本时,我有 2 个回发:一个事件目标等于 null,另一个具有正确的 ID。当它使用正确的 ID 发送回传时,Page.IsPostBack 为 false,这是完全不正确的。我希望单击文本的行为类似于单击复选框。我该怎么做?
提前致谢。
这就是我创建复选框列表的方式:
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>
当我点击复选框工资单时,例如,我有一个 ID 为 214 的回发,这是跨度 ID。当我点击文本“工资单”时,我有 2 次回发,事件目标为 null 和 214。我做错了什么? 谢谢
【问题讨论】:
标签: asp.net