Telerik Radcombobox.SelectedValue not working - c#

I am trying to populate my telerik dropdown on page load. I am using the following code for this:
ddIntervention.SelectedValue=3
The RadComboBox is inside two update panels.
aspx page:
<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>

I think your problem will be EnableAutomaticLoadOnDemand="True" reason because the RadComboBox will be empty until you click on it. That's why you set SelectedValue won't take effect on Page_Load
Update Code
.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();
}
}
Result On Page Load

I had a similar issue with RadcomboBox. I got a workaround using clear selection method before setting the Selected Value of the combo box. This worked for me wherever I implemented.
radComboBox1.ClearSelection();
radComboBox1.SelectedValue = value;

Related

how to extract asp .net control (Textbox) from .ascx.cs to another class and change the control attribute <ENABLE> to true

my scenario: I have more dropdown lists, when you click the dropdown list including with "Other" this word. my textbox enable attribute will be true otherwise it will be false (default). currently, I ran into a problem is I want to use one method to do this. when all dropdown lists was selected, it will call the same function and do the same thing. but I can't (1) Get the current dropdown list ID and even I got the drop-down list id, I cannot change the textbox ENABLE attribute to true. Thanks
.ascx.cs
<asp:Panel ID="Panel1" runat="server">
<div class="formGreyBg">
<div>
<asp:Label ID="panellblS2Heading" runat="server" Font-Size="Larger" Font-Bold="True" CssClass="formHeading"></asp:Label><br />
<asp:Label ID="panellblS2Description" runat="server" Font-Size="Small" CssClass="formDescription"></asp:Label>
</div>
<br />
<br />
<div class="formRow__underline formRow">
<div style="vertical-align: top" class="formRowLabelWrapper">
<asp:Label ID="panellblListedEntity" runat="server" Font-Bold="True" CssClass="formLabel"></asp:Label>
</div>
<div style="vertical-align: top" class="formRowInputWrapper">
<asp:UpdatePanel ID="upPanelddlListedEntity" runat="server" UpdateMode="Conditional" Visible="true"
RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList ID="panelddlListedEntity" runat="server" Width="510" CssClass="formInput"
OnSelectedIndexChanged="panelddlListedEntity_OnSelectedIndexChanged" AutoPostBack="True"></asp:DropDownList><br/><br/>
<asp:TextBox ID="paneltbListedEntity" runat="server" Enabled="false"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="panelddlListedEntity" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
Another class:
protected void panelddlListedEntity_OnSelectedIndexChanged(object sender, EventArgs e)
{
//DropDownList dropDownList = (DropDownList)sender;
if (panelddlListedEntity.SelectedValue.Contains("Other"))
{
TextBox textBox = new TextBox();
textBox.ID = dropDownList.ID.ToString().Replace("ddl", "tb");
textBox.Enabled = true;
TextBox control = (TextBox)this.Page.LoadControl(#"~/_CONTROLTEMPLATES/15/FRC.WB/Report/ReportUserControl.ascx").FindControl(dropDownList.ID.ToString().Replace("ddl", "tb"));
//control.Enabled = true;
}
else
paneltbListedEntity.Enabled = false;
}

SelectedIndexChanged only fires after button click

SelectedIndexChanged only works after form submit.If i select the data from drpdown list that it selects the index and gets data from database to texboxes.But it doesnot do that instead if i click the submit button it shows the data
I tried using postback but it didn't work.Is there any alternative to solve this problem.
Html
<div class="col-md-4">
<asp:Label ID="Label10" runat="server" Text="Related Word"></asp:Label>
<asp:UpdatePanel ID="update" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtRelatedWord" runat="server" class="form-control" AutoPostBack="true" OnTextChanged="txtRelatedWord_TextChanged" placeholder="Word" ></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtRelatedWord" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
<div class="col-md-4" style="margin-top:37px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="cmbRelatedWord" CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="cmbRelatedWord_SelectedIndexChanged" runat="server" >
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cmbRelatedWord" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
Code Behind
private static void InsertRequest(ref RequestModel model, string connection)
{
using(var con = new SqlConnection(connection))
{
using(var cmd = new SqlCommand("Insert into infromationRequests (Gender, Age, IsMedicine, IsTransplant, Operations, ResponseJson) values (#Gender, #Age, #IsMedicine, #IsTransplant, #Operations, NULL); SELECT SCOPE_IDENTITY();", con))
{
cmd.Parameters.AddWithValue("#Gender", model.Gender);
cmd.Parameters.AddWithValue("#Age", model.Age);
cmd.Parameters.AddWithValue("#IsMedicine", model.Medicine);
cmd.Parameters.AddWithValue("#IsTransplant", model.Transplant);
cmd.Parameters.AddWithValue("#Operations", model.Operations);
con.Open();
model.Id = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
}
Well, I managed to do a simple test which worked fine :
Back :
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Console.WriteLine("Load triggered");
}
protected void ddlist_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("event triggered");
tb.Text = (sender as DropDownList).SelectedValue;
}
}
And front :
<asp:ScriptManager runat="server" ID="smanager"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="upanel">
<ContentTemplate>
<asp:DropDownList ID="ddlist" runat="server" OnSelectedIndexChanged="ddlist_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox runat="server" ID="tb" Text="Default"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlist" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
According to this I can give you those clues :
Did you try to put both component in the same Update panel ?
Are you sure your event isn't triggered ? Or mayeb you just didn't used any Postback check in your page ? If you don't check if your page is a postback on page load, then your component contents will be reloaded before the even is triggered.

Create controls dynamically on button click

I am trying to create dynamic controls on button click.
HTML:
<asp:LinkButton ID="lnkSrt" runat="server" Text="Multi Sort" ForeColor="Black" Font-Names="Calibri" Font-Size="10pt" />
<ajax:ModalPopupExtender runat="server" ID="mpeSrt"
TargetControlID="lnkSrt" PopupControlID="pnlSrt" CancelControlID="btnClose">
</ajax:ModalPopupExtender>
<asp:UpdatePanel ID="upPanel" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlSort" runat="server" align="center" Style="display: none" BackColor="LightGray">
<asp:Label runat="server" Text="Sort By Column" />
<asp:DropDownList ID="ddlColumnSort" runat="server" AutoPostBack="true" />
<asp:Label runat="server" Text="Order By" />
<asp:DropDownList ID="ddlOrder" runat="server" AutoPostBack="true">
<asp:ListItem Text="Ascending" Value="0"></asp:ListItem>
<asp:ListItem Text="Descending" Value="1"></asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="lnkAdd" runat="server" Text="Add Sorting" Font-Underline="true" OnClick="lnkAddOrder_Click" Font-Names="Calibri" Font-Size="10pt" ForeColor="Black" />
<asp:Button ID="btnSorting" runat="server" Text="Sort" OnClick="btnSorting_Click" />
<asp:Button ID="btnClose" runat="server" Text="Close" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
CODE:
When clicked on AddSorting I am trying to create dropdown control using below code and the controls doesn't show.
protected void lnkAddSort_Click(object sender, EventArgs e)
{
int index = pnlSort.Controls.OfType<DropDownList>().ToList().Count + 1;
this.Createddl("ddldyn" + index);
}
public void Createddl(string id)
{
DropDownList ddl = new DropDownList();
ddl.ID = id;
pnlSort.Controls.Add(ddl);
Literal lt = new Literal();
lt.Text = "<br />";
pnlSort.Controls.Add(lt);
}
Image:
pnlSrt is set to display: none so the dynamically added child controls will not show either.
As per the #Yoshi answer. You could set visible true for the pnlSrt control by using pnlSort.Style["display"] = "block";// or pnlSort.Attributes.Add("style", "display:block");
Try this
public void Createddl(string id)
{
DropDownList ddl = new DropDownList();
ddl.ID = id;
pnlSort.Controls.Add(ddl);
Literal lt = new Literal();
lt.Text = "<br />";
pnlSort.Controls.Add(lt);
pnlSort.Attributes.Add("style", "display:block");
}

ASP dropdownlist selecedvalue is blank on button press

Hi I'm having a problem with my dropdownlist. I have goolged but nobody seems to have had the same problem. I have an updatepanel with a panel of controls within it. On button click I want to get the values from the controls the problem is on button click the selectedvalue is blank always.
here is the html
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
<ContentTemplate>
<asp:Panel runat="server" ID="pnlLSP" CssClass="textBox" Width="85%" Visible="False">
<div>
<asp:DropDownList ID="ddlLSPHours" runat="server" ValidationGroup="LSP"
EnableViewState="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="ddlLSPHours" ErrorMessage="RequiredFieldValidator"
ValidationGroup="LSP">*</asp:RequiredFieldValidator>
<asp:DropDownList ID="ddlLSPMins" runat="server" ValidationGroup="LSP" EnableViewState="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="ddlLSPMins" ErrorMessage="RequiredFieldValidator"
ValidationGroup="LSP">*</asp:RequiredFieldValidator>
</div>
<div style="text-align: center">
<asp:Button ID="btnLSPDone" runat="server" CssClass="button" Text="Done" Style="margin-top: 10px"
Width="100px" ValidationGroup="LSP" OnClick="btnLSPDone_Click" />
</div>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnLSPDone"></asp:PostBackTrigger>
</Triggers>
</asp:UpdatePanel>
c# code for populating dropdownlists
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//populate hours ddl
for (int i = 0; i < 25; i++)
{
string number = i.ToString();
if (i < 10)
{
number = "0" + i.ToString();
}
ddlLSPHours.Items.Add(number)
}
//populate mins ddl
for (int i = 0; i < 61; i++)
{
string number = i.ToString();
if (i < 10)
{
number = "0" + i.ToString();
}
ddlLSPMins.Items.Add(number);
}
}
}
protected void btnLSPDone_Click(object sender, EventArgs e)
{
string timeSelected = ddlLSPHours.SelectedValue + ":" + ddlLSPMins.SelectedValue;
}
The only problem is with your
EnableViewState="False"
in
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
Any reason why you are using it?
If you remove it, will give you the selected values for the dropdowns. Something like this :
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1">
Hope this helps. Happy Coding..!!
just make EnableViewState="False" of your UpdatePanel
<asp:UpdatePanel runat="server" ChildrenAsTriggers="true" ID="updpnl1" EnableViewState="False">
it will work.

ImageButton is not being enabled when using AsyncPostBackTrigger in ajax update panel in asp.net

i am using asp.net with c#. i have two image buttons : open and delete.
By default they are disabled i.e. imgOpen.Enabled = false; imgDelete.Enabled = false;.
I have a GridView which shows search results from table. GridView contains a radio button which when selected, should enable Open and delete image button.
if I won't use ajax update panel, then every time i select a radio button my page reloads and it's quite disturbing and not friendly.
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="updateRadioButton" runat="server">
<ContentTemplate>
<asp:RadioButton ID="rdoBtnFileOption" runat="server" OnCheckedChanged="rdoBtnFileOption_CheckedChanged" AutoPostBack="true" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoBtnFileOption" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
protected void rdoBtnFileOption_CheckedChanged(object sender, EventArgs e)
{
imgOpen.Enabled = true;
imgDelete.Enabled = true;
RadioButton curretnRdo = sender as RadioButton;
GridViewRow currentRow = (GridViewRow)curretnRdo.NamingContainer;
int index = currentRow.RowIndex;
try
{
foreach (GridViewRow grv in grdSearchResults.Rows)
{
if (grv.RowType == DataControlRowType.DataRow && grv.RowIndex != index)
{
RadioButton rdo = new RadioButton();
rdo = (RadioButton)(grv.FindControl("rdoBtnFileOption"));
rdo.Checked = false;
}
}
}
catch (Exception ex)
{
form.MessageBox.Show(ex.Message, "Error", form.MessageBoxButtons.OK, form.MessageBoxIcon.Error);
}
}
These two line are not working for me.
imgOpen.Enabled = true;
imgDelete.Enabled = true;
Please Suggest any approach.
I hope i am quite clear.
Try the following
Wrap your imgDelete with another UpdatePanel and set UpdateMode=Conditional and set Trigger
<asp:UpdatePanel ID="updateRadioButton" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Image ID="imgOpen" runat="server"/>
<asp:Image ID="imgDelete" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoBtnFileOption"
EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
or set UpdateMode="Always" and No Triggers
For using AsyncPostBack, the contents should be in Update panel also.
you can use another Asp:UpdatePanel with UpdateMode="Always" having your images in it.
<asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:ImageButton ID="imgOpen" runat="server" ImageUrl="" Visible="false" AlternateText="this is Open Image" />
<asp:ImageButton ID="imgDelete" runat="server" ImageUrl="" Visible="false" AlternateText="this is Delete Image" />
</ContentTemplate>
</asp:UpdatePanel>
May this Help You.

Categories