Telerik Radcombobox.SelectedValue 不工作
Telerik Radcombobox.SelectedValue not working
我正在尝试在页面加载时填充我的 telerik 下拉列表。我为此使用以下代码:
ddIntervention.SelectedValue=3
RadComboBox 在两个更新面板内。
aspx 页面:
<telerik:RadComboBox ID="ddIntervention" runat="server" DataSourceID="SqlDataSource3"
DataTextField="ProcedureDesc" MaxHeight="180px" Filter="Contains"
DataValueField="ProcedureID" HighlightTemplatedItems="True" MarkFirstMatch="True"
Width="350px" EmptyMessage="Search By Intervention Name" EnableAutomaticLoadOnDemand="True"
ShowMoreResultsBox="True" EnableVirtualScrolling="True" ItemsPerRequest="10"
DropDownWidth="350px" AutoPostBack="True" CausesValidation="False"
ResolvedRenderMode="Classic" AllowCustomText="true">
</telerik:RadComboBox>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ProcedureID], [ProcedureDesc], [DateDeleted] FROM
[Vw_UploadRef_Procedures] WHERE ([DateDeleted] IS NULL)
</asp:SqlDataSource>
我认为您的问题将是 EnableAutomaticLoadOnDemand="True"
原因,因为 RadComboBox
在您单击它之前将是空的。这就是为什么你设置 SelectedValue
不会对 Page_Load
生效
更新代码
.aspx
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btn" runat="server" Text="Dummy Button" />
<br /><br />
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<telerik:RadComboBox ID="rcb" runat="server" AutoPostBack="true"
AllowCustomText="true" HighlightTemplatedItems="true"
EmptyMessage="Search By Name" MarkFirstMatch="true"
ShowMoreResultsBox="true" EnableVirtualScrolling="true"
DropDownWidth="350px" CausesValidation="false"
ItemsPerRequest="10" RenderMode="Classic"
OnSelectedIndexChanged="rcb_SelectedIndexChanged"></telerik:RadComboBox>
<br />
<asp:Label ID="lbl" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rcb" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn" />
</Triggers>
</asp:UpdatePanel>
.cs
protected void Page_Load(object sender, EventArgs e)
{
// Check
if (!IsPostBack)
{
// Variable
string[] text = { "A","B","C", "D", "E", "F" };
DataTable dt = new DataTable();
dt.Columns.Add("Text");
dt.Columns.Add("Value");
// Add Rows
for (int i = 0; i < text.Length; i++)
dt.Rows.Add(text[i], i + "");
// Bind to Drop Down
rcb.DataSource = dt;
rcb.DataTextField = "Text";
rcb.DataValueField = "Value";
rcb.DataBind();
// Check
if (rcb.Items.Count > 0)
{
rcb.SelectedValue = "3";
rcb_SelectedIndexChanged(rcb, new RadComboBoxSelectedIndexChangedEventArgs
(rcb.SelectedItem.Text.Trim(), "", rcb.SelectedValue, ""));
// Trigger Selected Index Changed
}
}
}
protected void rcb_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Check if the dropdown is selectedIndex is greater or equal first item
// if you have "Please select" on first item just change ">=" to ">"
if (rcb.SelectedIndex >= 0)
{
lbl.Text = rcb.SelectedItem.Text.Trim();
}
}
页面加载结果
我在使用 RadcomboBox 时遇到了类似的问题。在设置组合框的选定值之前,我使用清除选择方法获得了解决方法。无论我在哪里实施,这对我都有效。
radComboBox1.ClearSelection();
radComboBox1.SelectedValue = value;
我正在尝试在页面加载时填充我的 telerik 下拉列表。我为此使用以下代码:
ddIntervention.SelectedValue=3
RadComboBox 在两个更新面板内。
aspx 页面:
<telerik:RadComboBox ID="ddIntervention" runat="server" DataSourceID="SqlDataSource3"
DataTextField="ProcedureDesc" MaxHeight="180px" Filter="Contains"
DataValueField="ProcedureID" HighlightTemplatedItems="True" MarkFirstMatch="True"
Width="350px" EmptyMessage="Search By Intervention Name" EnableAutomaticLoadOnDemand="True"
ShowMoreResultsBox="True" EnableVirtualScrolling="True" ItemsPerRequest="10"
DropDownWidth="350px" AutoPostBack="True" CausesValidation="False"
ResolvedRenderMode="Classic" AllowCustomText="true">
</telerik:RadComboBox>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ProcedureID], [ProcedureDesc], [DateDeleted] FROM
[Vw_UploadRef_Procedures] WHERE ([DateDeleted] IS NULL)
</asp:SqlDataSource>
我认为您的问题将是 EnableAutomaticLoadOnDemand="True"
原因,因为 RadComboBox
在您单击它之前将是空的。这就是为什么你设置 SelectedValue
不会对 Page_Load
更新代码
.aspx
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btn" runat="server" Text="Dummy Button" />
<br /><br />
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<telerik:RadComboBox ID="rcb" runat="server" AutoPostBack="true"
AllowCustomText="true" HighlightTemplatedItems="true"
EmptyMessage="Search By Name" MarkFirstMatch="true"
ShowMoreResultsBox="true" EnableVirtualScrolling="true"
DropDownWidth="350px" CausesValidation="false"
ItemsPerRequest="10" RenderMode="Classic"
OnSelectedIndexChanged="rcb_SelectedIndexChanged"></telerik:RadComboBox>
<br />
<asp:Label ID="lbl" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rcb" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn" />
</Triggers>
</asp:UpdatePanel>
.cs
protected void Page_Load(object sender, EventArgs e)
{
// Check
if (!IsPostBack)
{
// Variable
string[] text = { "A","B","C", "D", "E", "F" };
DataTable dt = new DataTable();
dt.Columns.Add("Text");
dt.Columns.Add("Value");
// Add Rows
for (int i = 0; i < text.Length; i++)
dt.Rows.Add(text[i], i + "");
// Bind to Drop Down
rcb.DataSource = dt;
rcb.DataTextField = "Text";
rcb.DataValueField = "Value";
rcb.DataBind();
// Check
if (rcb.Items.Count > 0)
{
rcb.SelectedValue = "3";
rcb_SelectedIndexChanged(rcb, new RadComboBoxSelectedIndexChangedEventArgs
(rcb.SelectedItem.Text.Trim(), "", rcb.SelectedValue, ""));
// Trigger Selected Index Changed
}
}
}
protected void rcb_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Check if the dropdown is selectedIndex is greater or equal first item
// if you have "Please select" on first item just change ">=" to ">"
if (rcb.SelectedIndex >= 0)
{
lbl.Text = rcb.SelectedItem.Text.Trim();
}
}
页面加载结果
我在使用 RadcomboBox 时遇到了类似的问题。在设置组合框的选定值之前,我使用清除选择方法获得了解决方法。无论我在哪里实施,这对我都有效。
radComboBox1.ClearSelection();
radComboBox1.SelectedValue = value;