代码示例:
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(some condition)
{
comboBox.Text = "new string"
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是comboBox文本总是显示所选索引的字符串值而不是新字符串.绕道而行吗?
OnSelectedIndexChanged我的下拉框没有触发该事件.我看过的所有论坛都告诉我添加AutoPostBack="true",但这并没有改变结果.
HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Current Time: " /><br />
<asp:Label ID="lblCurrent" runat="server" Text="Label" /><br /><br />
<asp:DropDownList ID="cboSelectedLocation" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cboSelectedLocation_SelectedIndexChanged" /><br /><br />
<asp:Label ID="lblSelectedTime" runat="server" Text="Label" />
</div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
代码背后:
public partial class _Default : Page
{
string _sLocation = string.Empty;
string _sCurrentLoc = string.Empty;
TimeSpan _tsSelectedTime;
protected void Page_Load(object sender, EventArgs e)
{
AddTimeZones();
cboSelectedLocation.Focus();
lblCurrent.Text = "Currently in " …Run Code Online (Sandbox Code Playgroud) 我的代码:
*的.aspx:
<asp:DropDownList ID="CountryList" CssClass="CountryList" runat="server"
OnSelectedIndexChanged="CountryList_SelectedIndexChanged" />
Run Code Online (Sandbox Code Playgroud)
*.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
CountryList.SelectedIndexChanged +=
new EventHandler(CountryList_SelectedIndexChanged);
...
}
protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadCityList(CountryList, CityList);
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
我正在编写一些代码来检测打开MultiSelect的WindForms ListBox中的选择切换.由于SelectedIndexChanged只让我看到点击后选择的内容,我一直在寻找一种方法来检测单击ListBox之前选择的内容.我实现了MouseDown事件,我可以得到我想要的,但不幸的副作用是我杀死了SelectedIndexChanged事件.它不会开火.
这是已知行为吗?有关在点击之前进入选择列表的想法吗?
谢谢.
编辑以包括所请求的代码片段.
Designer生成的事件:
this.lbPhysicianClinic.SelectedIndexChanged += new System.EventHandler( this.lbPhysicianClinic_SelectedIndexChanged );
this.lbPhysicianClinic.MouseDown += new System.Windows.Forms.MouseEventHandler( this.lbPhysicianClinic_MouseDown );
Run Code Online (Sandbox Code Playgroud)
显示MouseDown事件的代码段:
private void lbPhysicianClinic_MouseDown( object sender, MouseEventArgs e )
{
List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
}
Run Code Online (Sandbox Code Playgroud)
显示SelectedIndexChanged事件的代码段:
private void lbPhysicianClinic_SelectedIndexChanged( object sender, EventArgs e )
{
try
{
if ( this.FormInitComplete && this.RefreshUIComplete )
{
List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
Clinic_List_ByPhysicianResult DroppedClinic = new Clinic_List_ByPhysicianResult();
Run Code Online (Sandbox Code Playgroud)
我在每个事件中设置了一个断点,如果有MouseDown事件,那么SelectedIndexChanged事件永远不会触发.它仅在MouseDown事件消失时触发.
希望这能澄清事情.
上一个错误发生在列表框上的SelectedIndexChanged单击事件上.
在调试中返回的值是"",但是当你查看网页源代码时肯定有值.
这是我的列表框:
<asp:ListBox ID="lstBxMyList" runat="server" CssClass="binorderlst1"
DataTextField="myName"
DataValueField="myID"
OnSelectedIndexChanged ="lstBxMyList_SelectedIndexChanged"
AutoPostBack="true" Rows="10">
</asp:ListBox>
Run Code Online (Sandbox Code Playgroud)
这是活动:
protected void lstBxMyList_SelectedIndexChanged(object sender, EventArgs e)
{
myID = Convert.ToInt32(lstBxSiteList.SelectedValue.ToString());
... rest of code
}
Run Code Online (Sandbox Code Playgroud)
为了完整性,这里是数据绑定:
private void BindLstBxMyList(int myOtherID)
{
DataTable dt = new DataTable();
SqlConnection conn;
SqlCommand comm;
using (conn = new SqlConnection(aSHconns.aconn))
{
comm = new SqlCommand("myStoredProc", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@myOtherID", SqlDbType.Int));
comm.Parameters["@myOtherID"].Value = myOtherID;
SqlDataAdapter sqlDa = new SqlDataAdapter(comm);
try
{
conn.Open();
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
lstBxMyList.DataSource …Run Code Online (Sandbox Code Playgroud) 我有一个需要由目录填充的列表框.我已成功用目录填充列表框但我的问题是我无法获得所选项目的值.我在SelectedIndexChanged事件上放置了一个断点,但我的Web应用程序似乎没有运行该代码.
这是我填充列表框的代码:
lstDirectories.DataSource = dtDirectories;
lstDirectories.DataValueField = "DirectoryID";
lstDirectories.DataTextField = "DirectoryName";
lstDirectories.DataBind();
Run Code Online (Sandbox Code Playgroud)
这就是我从SelectedIndexChanged事件的列表框中获取值的方法:
TextBox1.Text = lstDirectories.SelectedItem.Value.ToString();
Run Code Online (Sandbox Code Playgroud)
我在这里做错了吗?谢谢您的帮助!:)
我发现很多帖子,人们试图解决SelectedIndexChanged不工作的问题EnableViewState='false'.
我有点困惑,为什么控制状态不会启动并允许它工作,但如果有人也可以解释这个奖励积分.
一些'黑客'非常'hacky'.就像在页面本身的viewstate上设置一个对应于下拉值的值,然后将该值与回发期间接收的值进行比较.
我正在寻找最优雅的解决方案(如果有一个好的解决方案).
我不知道这是否是一个聪明的派生控件或更聪明的东西,但我想确保解决方案足够通用,以便"可信任",而不仅仅是一个仅适用于特定页面的黑客(这是常见的这样的黑客!).
我有一个带有下拉列表的转发器.当用户更改其索引时,我希望标签更改其值.(ddlSizes值来自MySQL DB)
Sizes.aspx
<asp:DropDownList ID="ddlSizes" runat="server" AutoPostBack="True" DataSourceID="objdsSizes" DataTextField="SizeName" DataValueField="SizeID" />
<asp:Label ID="lbldummy" runat="server" Text=""></asp:Label>
Run Code Online (Sandbox Code Playgroud)
Sizes.aspx.vb
Protected Sub ddlSizes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSizes.SelectedIndexChanged
lbldummy = ddlSizes.value
End Sub
Run Code Online (Sandbox Code Playgroud)
但是无法识别ddlSizes.SelectedIndexChanged.所以价值lbldummy不会改变.
有什么建议?谢谢.
更改ListView的选定项目(或索引)时会发生哪个事件?
我有一个gridview,其中有两个从db填充的下拉列表.一个是描述性名称,另一个是缩写名称.我需要完成以下任务:
当我在DDL1中选择一个项目时,我需要更改所选的DDL2索引以匹配,反之亦然.
我在这里搜索过,发现了以下内容:
protected void ddlAddLabTest_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlLabTest = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlLabTest.NamingContainer;
DropDownList ddlAddLabTestShortName = (DropDownList)row.FindControl("ddlAddShortname");
ddlAddLabTestShortName.SelectedIndex = intSelectedIndex;
}
Run Code Online (Sandbox Code Playgroud)
只有当它到达"行"的分配时,我才会收到以下内容:
Unable to cast object of type 'System.Web.UI.WebControls.DataGridItem' to type 'System.Web.UI.WebControls.GridViewRow'.
Run Code Online (Sandbox Code Playgroud)
我试过找一个有效的例子,但我做不到.任何帮助是极大的赞赏!