我的网页中有一个DropDownList对象.当我点击它并选择一个不同的值时,即使我有一个功能连接到SelectedIndexChanged
事件,也没有任何反应.我会尝试尽可能有序地发布我的代码:
首先,实际对象的HTML代码:
<asp:DropDownList ID="logList" runat="server"
onselectedindexchanged="itemSelected">
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
这就是这个功能,itemSelected
:
protected void itemSelected(object sender, EventArgs e)
{
Response.Write("Getting clicked; " + sender.GetType().ToString());
FileInfo selectedfile;
Response.Write("<script>alert('Hello')</script>");
foreach (FileInfo file in logs)
{
if (file.Name == logList.Items[logList.SelectedIndex].Text)
{
Response.Write("<script>alert('Hello')</script>");
}
}
}
Run Code Online (Sandbox Code Playgroud)
没有出现任何响应,并且JavaScript的那部分永远不会运行.我在最新的3.6版本的Firefox以及Internet Explorer 8上试过这个.这是从Windows Server 2003 R2机器上运行的,运行ASP.Net和.NET Framework版本4.
如果有人可以提供帮助,那就太好了.
public class ComboboxItem {
public string Text { get; set; }
public string Value { get; set; }
public override string ToString() { return Text; }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = comboBox1.SelectedIndex;
int selecteVal = (int)comboBox1.SelectedValue;
ComboboxItem selectedCar = (ComboboxItem)comboBox1.SelectedItem;
MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
}
Run Code Online (Sandbox Code Playgroud)
我正在添加它们:
ComboboxItem item = new ComboboxItem();
item.Text = cd.Name;
item.Value = cd.ID;
this.comboBox1.Items.Add(item);
Run Code Online (Sandbox Code Playgroud)
我一直得到一个NullReferenceExeption,不知道为什么.文本似乎显示得很好.
我正在实现一个功能,当用户按下GridView中行中的任何一点时,将选择该行而不是选择按钮.
为了实现这一点,我使用以下代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Set the hand mouse cursor for the selected row.
e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");
// The seelctButton exists for ensuring the selection functionality
// and bind it with the appropriate event hanlder.
LinkButton selectButton = new LinkButton()
{
CommandName = "Select",
Text = e.Row.Cells[0].Text
};
e.Row.Cells[0].Controls.Add(selectButton);
e.Row.Attributes["OnClick"] =
Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,存在以下问题:
EnableEventValidation
将页面设置为时,这才能正常工作false
.SelectedIndexChanged
仅触发公正的情况下Grid.DataBind()
被调用Page_Load
的页面(在每个回发).难道我做错了什么?有更好的实施吗? …
如果用户选择.NET 2.0 ListView中的所有项目,ListView将为每个项目触发SelectedIndexChanged事件,而不是触发事件以指示选择已更改.
如果用户然后单击以选择列表中的一个项目,则ListView将为未选中的每个项目触发SelectedIndexChanged事件,然后为单个新选择的项目触发SelectedIndexChanged事件,而不是触发事件以指示选择已经改变.
如果您在SelectedIndexChanged事件处理程序中有代码,当您开始在列表中有几百/千个项目时,程序将变得非常无响应.
我考虑过停留计时器等.
但有没有人有一个很好的解决方案,以避免成千上万的不必要的ListView.SelectedIndexChange事件,什么时候真的会有一个事件呢?
我正在创建一个服务器控件,基本上绑定两个下拉列表,一个用于国家,一个用于州,并更新国家/地区的selectedindexchanged事件的状态下拉列表.但是,它没有回发.有什么想法吗?将它们包装在UpdatePanel中的加分点(有渲染问题;也许是因为我没有要引用的页面?)
这就是我所拥有的(一些额外的数据访问内容被剥离):
public class StateProv : WebControl
{
public string SelectedCountry;
public string SelectedState;
private DropDownList ddlCountries = new DropDownList();
private DropDownList ddlStates = new DropDownList();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
IList<Country> countries = GetCountryList();
IList<State> states = new List<State>();
if (SelectedCountry != null && SelectedCountry != "")
{
states = GetStateList(GetCountryByShortName(SelectedCountry).CountryShortName);
}
else
{
states.Add(new State { CountryId = 0, Id = 0, StateLabelName = "No states available", StateLongName = "No states available", StateShortName = "" }); …
Run Code Online (Sandbox Code Playgroud) c# asp.net custom-server-controls selectedindexchanged drop-down-menu
我们获得了一个Windows窗体应用程序,它是从Microsoft Visual Studio的模板(PasteBin 1 2 3 4上的设计器代码)创建的,带有默认的ListBox exampleListBox
和Button exampleButton
.
我们使用1到10之间的数字填充ListBox.
for (int i = 0; i < 10; ++i)
{
exampleListBox.Items.Add(i);
}
Run Code Online (Sandbox Code Playgroud)
然后我们添加两个事件处理程序.
exampleListBox_SelectedIndexChanged
将只是向控制台写入当前选定的索引.
private void exampleListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine(exampleListBox.SelectedIndex);
}
Run Code Online (Sandbox Code Playgroud)
exampleButton_Click
将当前所选索引的项目设置为自身.如此有效,这应该绝对没有改变.
private void exampleButton_Click(object sender, EventArgs e)
{
exampleListBox.Items[exampleListBox.SelectedIndex] = exampleListBox.Items[exampleListBox.SelectedIndex];
}
Run Code Online (Sandbox Code Playgroud)
单击按钮时,我预计不会发生任何事情.然而,这种情况并非如此.exampleListBox_SelectedIndexChanged
即使SelectedIndex
没有更改,单击该按钮也会触发事件.
例如,如果我单击索引2中的项目exampleListBox
,那么exampleListBox.SelectedIndex
将变为2.如果我按exampleButton
,则exampleListBox.SelectedIndex
仍然是2.然而,exampleListBox_SelectedIndexChanged
事件触发.
为什么即使所选索引未更改,事件也会触发?
此外,还有什么可以防止这种行为发生?
对于ListBox(选择模式设置为One),我希望跟踪是否选择了所选项目.为此,我订阅了一个方法到SelectedIndexChanged并检查SelectedIndex是否为-1.但是,我注意到在调用Items.Clear()之后事件不会触发,即使SelectedIndex更改为-1(如果它还不是-1).
为什么不开火?我知道我可以通过在清除列表之前为SelectedIndex分配-1来解决这个问题.但有更好的方法吗?
这是一个复制它的简单代码:
using System;
using System.Windows.Forms;
namespace ns
{
class Program
{
static ListBox lst = new ListBox();
public static void Main()
{
lst.SelectedIndexChanged += new EventHandler(lst_SelectedIndexChanged);
lst.Items.Add(1);
Console.WriteLine("Setting selected index to 0...");
lst.SelectedIndex = 0; //event fire here
Console.WriteLine("(Selected Index == {0})", lst.SelectedIndex);
Console.WriteLine("Clearing all items...");
lst.Items.Clear(); //event *should* fire here?!
//proof that the selected index has changed
Console.WriteLine("(Selected Index == {0})", lst.SelectedIndex);
}
static void lst_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("[!] Selected Index Changed:{0}", lst.SelectedIndex);
}
} …
Run Code Online (Sandbox Code Playgroud) 我正在使用Vb.net在asp.net网站上工作,我有一个autopostback = true的下拉列表,我需要在更改项目时获取所选值,或者我想获取触发selectedindexchanged事件的项目.
任何帮助请..
我<base href="http://localhost:80/">
在我的主页中使用基本网址,
现在,当我在更新面板中的dropdownlist
内容页面(位于localhost:80/directory1/directory2
)上使用控件时,selectedindexchanged
事件无法正常工作.
我试图搞清楚,但在firefox
控制台的网络选项卡中,我发现请求正在寻找url
仅在基础上的内容页面localhost:80/contenpage.aspx
而不是localhost:80/directory1/directory2/contenpage.aspx
并且给出错误
无法找到该资源.
我有一个小的C#3.5的WinForms应用程序,我对工作抓起,从一个服务器的事件日志名称为列表视图.当选择这些项目中的一个,另一个是列表视图填充来自如下所示,抓住了第一个项目的Text属性SelectedItems集合中使用SelectedIndexChanged事件选择的事件日志中的事件日志条目.
string logToGet = listView1.SelectedItems[0].Text;
Run Code Online (Sandbox Code Playgroud)
这在第一次工作正常,但第一次列表视图中的第二个事件日志名称选择失败.正在发生的事情是SelectedItems集合SelectedIndexChanged事件越来越是空的,所以我得到一个ArgumentOutOfRangeException.
我很茫然.关于我做错了什么或更好的方法做任何想法?
c# ×7
asp.net ×5
winforms ×4
listbox ×2
listview ×2
.net ×1
.net-4.5.2 ×1
combobox ×1
gridview ×1
selecteditem ×1
updatepanel ×1