ASP.NET DropDownList.SelectedValue 在 i!nPostBack 中丢失
ASP.NET DropDownList.SelectedValue gets lost i!nPostBack
此问题已解决,请参阅我在 post
下方的评论
我的代码有一个问题让我很困惑:
我有一个 ASP.NET DropDownList(简单,不在 gridview 左右...),它是静态数据绑定到数据库查询。此外,我在标记中添加了一个列表项:
<asp:DropDownList ID="ddDeviceSelector" runat="server" DataSourceID="CryringDB_InventoryAssets" DataTextField="Identifier" DataValueField="RowID" AutoPostBack="True" OnTextChanged="ddDeviceSelector_IndexChanged" ToolTip="Select a device by name" AppendDataBoundItems="True" Width="300px">
<asp:ListItem>-- Select Device OR Enter Data of New Device --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="CryringDB_InventoryAssets" runat="server" connectionString="<%$ ConnectionStrings:CMSValuesExplicitUser %>" SelectCommand="SELECT valueColumn, textColumns FROM myTable"></asp:SqlDataSource>
我可以使用 URL 查询参数“?rid=...”调用该页面,其中 rid 包含列表中某个特定项目的值。当我尝试按值解决 rid 和 select 列表中的项目时,DropDownList.SelectedValue 未设置。
Page_Load代码:
protected void Page_Load(object sender, EventArgs e)
{
// Code to interpret the parameter rid (RowID) in the URL
// provides functionality to open formAssetDetails of an individual object.
if (!IsPostBack)
{
ddDeviceSelector.DataBind();
setUiPropertiesByMode(classConstants.displayMode);
String rowID = null;
rowID = Request.QueryString["rid"];
if (rowID != null)
{
setUiPropertiesByMode(classConstants.editMode);
ddDeviceSelector.Enabled = true;
ddDeviceSelector.Enabled = true;
ddDeviceSelector.SelectedValue = rowID;
}
}
代码执行没有问题,但我在调试器中看到一条错误消息(断点在第 "ddDeviceSelector.SelectedValue = rowID;" 行之后)
error CS0103: The name 'SelectedValue' does not exist in the current context
(此消息是从德文翻译而来,英文可能略有不同)
感谢任何帮助。
尝试在下拉列表的 DataBound 事件中设置所选值,这将确保您的下拉列表在尝试设置所选值之前完全与数据数据绑定,请记住也委托此方法:
//In your designer file
ddDeviceSelector.DataBound += new System.EventHandler(ddDeviceSelector_DataBound);
protected void ddDeviceSelector_DataBound(object sender, EventArgs e)
{
String rowID = null;
rowID = Request.QueryString["rid"];
if (rowID != null)
{
setUiPropertiesByMode(classConstants.editMode);
ddDeviceSelector.Enabled = true;
ddDeviceSelector.Enabled = true;
ddDeviceSelector.SelectedValue = rowID;
}
}
此问题已解决,请参阅我在 post
下方的评论我的代码有一个问题让我很困惑: 我有一个 ASP.NET DropDownList(简单,不在 gridview 左右...),它是静态数据绑定到数据库查询。此外,我在标记中添加了一个列表项:
<asp:DropDownList ID="ddDeviceSelector" runat="server" DataSourceID="CryringDB_InventoryAssets" DataTextField="Identifier" DataValueField="RowID" AutoPostBack="True" OnTextChanged="ddDeviceSelector_IndexChanged" ToolTip="Select a device by name" AppendDataBoundItems="True" Width="300px">
<asp:ListItem>-- Select Device OR Enter Data of New Device --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="CryringDB_InventoryAssets" runat="server" connectionString="<%$ ConnectionStrings:CMSValuesExplicitUser %>" SelectCommand="SELECT valueColumn, textColumns FROM myTable"></asp:SqlDataSource>
我可以使用 URL 查询参数“?rid=...”调用该页面,其中 rid 包含列表中某个特定项目的值。当我尝试按值解决 rid 和 select 列表中的项目时,DropDownList.SelectedValue 未设置。 Page_Load代码:
protected void Page_Load(object sender, EventArgs e)
{
// Code to interpret the parameter rid (RowID) in the URL
// provides functionality to open formAssetDetails of an individual object.
if (!IsPostBack)
{
ddDeviceSelector.DataBind();
setUiPropertiesByMode(classConstants.displayMode);
String rowID = null;
rowID = Request.QueryString["rid"];
if (rowID != null)
{
setUiPropertiesByMode(classConstants.editMode);
ddDeviceSelector.Enabled = true;
ddDeviceSelector.Enabled = true;
ddDeviceSelector.SelectedValue = rowID;
}
}
代码执行没有问题,但我在调试器中看到一条错误消息(断点在第 "ddDeviceSelector.SelectedValue = rowID;" 行之后)
error CS0103: The name 'SelectedValue' does not exist in the current context
(此消息是从德文翻译而来,英文可能略有不同)
感谢任何帮助。
尝试在下拉列表的 DataBound 事件中设置所选值,这将确保您的下拉列表在尝试设置所选值之前完全与数据数据绑定,请记住也委托此方法:
//In your designer file
ddDeviceSelector.DataBound += new System.EventHandler(ddDeviceSelector_DataBound);
protected void ddDeviceSelector_DataBound(object sender, EventArgs e)
{
String rowID = null;
rowID = Request.QueryString["rid"];
if (rowID != null)
{
setUiPropertiesByMode(classConstants.editMode);
ddDeviceSelector.Enabled = true;
ddDeviceSelector.Enabled = true;
ddDeviceSelector.SelectedValue = rowID;
}
}