我目前正在构建一个webform,它使用下拉列表和radiobuttonList等。有一些小的验证,如果你在这些元素中的任何一个上选择一个值,就必须有一个回发,并且可能必须重新加载某些元素,这取决于业务逻辑。 现在我为RBL和DDL创建了eventHandlers,但是我遇到了问题。
如果我更改DDL上的值,则会触发回发并且程序循环通过我的方法,没问题。如果我更改RBL上的值,则不会触发回发,也不会触发。奇怪的是,如果我首先在RBL中更改某些内容然后在DDL中更改某些内容,它会以正确的顺序循环遍历两个事件处理程序。
但显然这对我来说并不理想。如果用户首先在RBL中更改了DDL中的某些内容,那么该程序将无法正常运行。
我将在下面发布我的代码,是否有人知道如何使RadioButtonList触发回发/事件处理程序?
C#
protected void SchoolTypeID_Index_Changed(object sender, EventArgs e)
{
//code
}
protected void ddlStudentenStad_IndexChanged(object sender, EventArgs e)
{
//code
}
ASP.net
<asp:DropDownList ID="ddlStudentenStad" DataTextField="stad" DataValueField="id" AutoPostBack="true"
OnSelectedIndexChanged="ddlStudentenStad_IndexChanged" runat="server" class="SelectDropDown"></asp:DropDownList>
<asp:RadioButtonList ID="rblSchoolType" runat="server" AutoPostBack="true" DataTextField="type"
DataValueField="id" OnSelectedIndexChanged="SchoolTypeID_Index_Changed" RepeatDirection="Horizontal"/>
编辑:我们还制作了一个RadioButtonListAdapter类,因为我们过去在这个元素上遇到了更多问题。代码粘贴在下面,如果有人碰巧看到错误。
public class RadioButtonListControlAdapter:ControlAdapter {
public RadioButtonListControlAdapter()
{
}
protected override void Render(HtmlTextWriter writer)
{
RadioButtonList ButtonList = Control as RadioButtonList;
if (null != ButtonList)
{
int i = 0;
if (ButtonList.RepeatDirection == RepeatDirection.Horizontal)
{
writer.WriteFullBeginTag("div class=\"row collapse\"");
int aantalCols = (int)Math.Floor((double)12 / (double)ButtonList.Items.Count);
foreach (ListItem li in ButtonList.Items)
{
string itemClientID = Helpers.GetListItemClientID(ButtonList, li);
writer.WriteFullBeginTag("div class=\"large-" + aantalCols + " columns\" style=\"width:" + 100/ButtonList.Items.Count + "% !important; \"");
writer.WriteFullBeginTag("label for=\"" + ButtonList.UniqueID + "\"");
if (ButtonList.TextAlign == TextAlign.Right)
{
RenderRadioButtonListInput(writer, ButtonList, li);
RenderRadioButtonListLabel(writer, ButtonList, li);
}
else
{
RenderRadioButtonListLabel(writer, ButtonList, li);
RenderRadioButtonListInput(writer, ButtonList, li);
}
writer.WriteEndTag("label");
writer.WriteEndTag("div");
if (this.Page != null)
Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID, li.Value);
//if (ButtonList.RepeatDirection == RepeatDirection.Vertical)
// writer.Write("<br />");
writer.WriteLine();
}
writer.WriteEndTag("div");
if (this.Page != null)
{
Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID);
}
}
else
{
foreach (ListItem li in ButtonList.Items)
{
string itemClientID = Helpers.GetListItemClientID(ButtonList, li);
//writer.WriteFullBeginTag("div class=\"row\"");
writer.WriteFullBeginTag("label for=\"" + ButtonList.UniqueID + "\"");
if (ButtonList.TextAlign == TextAlign.Right)
{
RenderRadioButtonListInput(writer, ButtonList, li);
RenderRadioButtonListLabel(writer, ButtonList, li);
}
else
{
RenderRadioButtonListLabel(writer, ButtonList, li);
RenderRadioButtonListInput(writer, ButtonList, li);
}
writer.WriteEndTag("label");
//writer.WriteEndTag("div");
if (this.Page != null)
Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID, li.Value);
//if (ButtonList.RepeatDirection == RepeatDirection.Vertical)
// writer.Write("<br />");
writer.WriteLine();
}
}
if (this.Page != null)
Page.ClientScript.RegisterForEventValidation(ButtonList.UniqueID);
}
}
private void RenderRadioButtonListInput(HtmlTextWriter writer, RadioButtonList ButtonList, ListItem li)
{
writer.AddAttribute(HtmlTextWriterAttribute.Id, Helpers.GetListItemClientID(ButtonList, li));
writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
writer.AddAttribute(HtmlTextWriterAttribute.Name, ButtonList.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Value, li.Value);
writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
writer.AddAttribute(HtmlTextWriterAttribute.Class, "hidden-field");
if (li.Selected)
writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
if (li.Enabled == false || ButtonList.Enabled == false)
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
if (li.Enabled == true && ButtonList.Enabled == true && ButtonList.AutoPostBack)
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, String.Format(@"javascript:setTimeout('__doPostBack(\'{0}\',\'\')', 0)", Helpers.GetListItemUniqueID(ButtonList, li)));
}
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
private void RenderRadioButtonListLabel(HtmlTextWriter writer, RadioButtonList ButtonList, ListItem li)
{
writer.WriteFullBeginTag("span class=\"custom radio\"");
writer.WriteEndTag("span");
writer.Write(" " + li.Text);
}
}
答案 0 :(得分:0)
尝试使用更新面板包装控件。
您还可以浏览代码或发布更多详细信息。事件未触发的原因可能是一些额外的逻辑,未在代码清单中显示。我无法重现这个问题。