我遇到了麻烦,我无法弄清楚。我有一个数据绑定的 asp:gridview (sqldatasource),在 itemtemplate 中有一个 asp:dropdownlist。下拉列表有一个 onSelectedIndexChanged 事件侦听器,但它不会触发。
这是标记:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
EmptyDataText="There are no data records to display." EnableViewState="True" >
<Columns>
<asp:TemplateField HeaderText="Delete user">
<ItemTemplate>
<asp:Button runat="server" ID="btnDelete" CommandName="Delete" CommandArgument='<%# Eval("UserId") %>'
Text="Delete" OnCommand="DeleteUser" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Block users">
<ItemTemplate>
<asp:Button runat="server" ID="btnBlock" CommandName="Block" CommandArgument='<%# Eval("UserId") %>'
Text="Block" OnClick="btnBlock_Click" Visible='<%# !Convert.ToBoolean(Eval("IsLockedOut")) %>' />
<asp:Button runat="server" ID="btnDeblock" CommandName="Deblock" CommandArgument='<%# Eval("UserId") %>'
Text="Deblock" OnClick="btnBlock_Click" Visible='<%# Convert.ToBoolean(Eval("IsLockedOut")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Change role">
<ItemTemplate>
<asp:DropDownList ID="ChangeRole" runat="server" EnableViewState="false"
OnSelectedIndexChanged="ChangeRole_SelectedIndexChanged" AutoPostBack="true"
ToolTip='<%# Bind("UserName") %>' >
<asp:ListItem Text="Choose a role" Value="" Selected="True" />
<asp:ListItem Text="Admin" Value="" />
<asp:ListItem Text="Member" Value="" />
<asp:ListItem Text="Visitor" Value="" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RoleName" HeaderText="Current role" ReadOnly="true" SortExpression="RoleName" />
<asp:BoundField DataField="UserName" HeaderText="Username" ReadOnly="True" SortExpression="UserName" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="LastLoginDate" HeaderText="Last login"
SortExpression="LastLoginDate" />
<asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked"
SortExpression="IsLockedOut" />
<asp:BoundField DataField="FailedPasswordAttemptCount"
HeaderText="Failed logins"
SortExpression="FailedPasswordAttemptCount" />
<asp:BoundField DataField="Comment" HeaderText="Comments"
SortExpression="Comment" />
</Columns>
</asp:GridView>
<asp:Label ID="lblSuccess" runat="server" Text="Database updated successfully." Visible="false" meta:resourcekey="success" />
<asp:Label ID="lblError" runat="server" Text="An error occured, database was not updated." />
</ContentTemplate>
</asp:UpdatePanel>
我添加了更新面板代码,因为我认为它可能是相关的。在后面的代码中,我确保仅在 !Page.IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataKeyNames = new String[] {"UserId"};
GridView1.DataBind();
}
}
然后我为下拉列表制作了一个动作监听器
protected void ChangeRole_SelectedIndexChanged(object sender, EventArgs e)
{
Logger.Info("it's alive!", "Chaning role");
}
我已经在两个方向上尝试了 enableViewState="True || False" (对于 gridview 和下拉列表)和 autoPostBack="True || False" 但日志文件没有显示“它还活着”消息。更改索引时未触发该事件。
有任何想法吗?