![](/img/trans.png)
[英]RadioButtonList set to AutoPostBack = true but still not firing OnSelectedIndexChanged
[英]Asp.net OnSelectedIndexChanged event not firing despite autopostback being true
好的,我有一些要构建的网站的代码。 我有一个数据框绑定到SqlDataSource的列表框。 选中后,它应该会生成一个SQL查询,该查询将更新\\过滤页面中其他列表框的位置。 我已经尝试过测试它,只是为了使其更改Label的文本而已,甚至无法正常工作。 这是我的一些代码:
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<%--register triggers for Partial postback --%>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="showbutton" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="viewapps" EventName="Click" />
</Triggers>
<ContentTemplate>
<%--- the controls in their rows and columns --%>
<%--column 1 --%>
<asp:Label runat="server" ID="TESTER">Text</asp:Label>
<asp:Panel runat="server" CssClass="column1">
<asp:Panel ID="Panel1" runat="server" CssClass="row1">
<%-- Make Panel --%>
<span style="padding:8px; position:relative;">
<asp:Label ID="label1" runat="server" Text="Make" Font-Size="Large" ></asp:Label>
<asp:Listbox ID="MakeList" runat="server" Width="166px" SelectionMode ="Multiple" DataSourceID="MakeSource" DataTextField="MakeName" AutoPostBack="true" DataValueField="MakeID" OnSelectedIndexChanged="UpdateModels">
</asp:Listbox>
</span>
<asp:SqlDataSource ID="MakeSource" runat="server" ConnectionString="<%$ ConnectionStrings:VCDBConnectionString %>" ></asp:SqlDataSource>
</asp:Panel>
现在在我的C#代码背后,我有这个:
public void UpdateModels(object sender, EventArgs e)
{
//build a string for a SQL query for the Models
string newQuery = "SELECT M.[ModelID], M.[ModelName] FROM Model M INNER JOIN BaseVehicle BV ON BV.ModelID = M.ModelID Where BV.MakeID= '";
string test = "";
foreach (ListItem li in MakeList.Items)
{
//add each piece of the selected text to the string
if (li.Selected)
{
test += li.Value;
test += "' AND BV.MakeID= '";
}
int cleanup = test.LastIndexOf("' AND BV.MakeID= '");
test.Remove(cleanup-19,cleanup);
//ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", "alert('" + test + "');", true);
TESTER.Text = test;
}
但是测试器仍未更新。 即使AutoPostBack = true,即使整个内容都包装在更新面板中也是如此。 =(我需要一些帮助来弄清楚这一点。我也将感谢其他建议。我很乐意提供您可能需要的任何其他信息,请在评论中告知我。
String.remove无法按我认为的方式工作。 这导致了运行时异常,直到我进行调试后才显示出来,这就是为什么TESTER的文本没有得到更新的原因。
我能够以其他方式解决问题。
与其立即构建字符串并尝试棘手的字符串操作,不如我只是将每个字符串放入列表中,然后在末尾删除最后一个的最后一项。 然后,当我有了完善的列表后,便将其添加到字符串中。
在代码中,它看起来像这样:
List<string> queryBuilder = new List<string>();
//add the seleted items to items in the list
foreach (ListItem li in MakeList.Items)
{
if (li.Selected)
{
queryBuilder.Add(li.Value);
queryBuilder.Add("' OR BV.MakeID = '");
//build the list of selected makes for later use
selectedMakes.Add(li.Value);
}
}
try
{
//remove the last ' AND BV.MakeID= '
queryBuilder.RemoveAt(queryBuilder.Count-1);
//add back the ' and the orderby
queryBuilder.Add("'");
queryBuilder.Add(" ORDER BY [ModelName]");
//build the string
foreach(string s in queryBuilder){
newQuery+= s;
}
//debug for visibilty
TESTER.Text =newQuery;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.