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.
Related
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;
}
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");
}
I want to create a C# WebForm with a ASP.NET FileUpload Control and some Textboxes and a submit button. Idea: user selects a file, enters some data, on submit the form checks the data and if valid, it saves the file on the server otherwise an error message is displayed. There are so many posts about UpdatePanel Triggers etc. but not a working solution.
Here my code behind:
protected void Page_Load(object sender, EventArgs e) {
// for FileUpload-Control outside UpdatePanel
Page.Form.Attributes.Add("enctype", "multipart/form-data");
}
protected void Button1_Click(object sender, EventArgs e) {
bool valid = true;
string errorMessage = DateTime.Now.ToLongTimeString() + ": ";
if (this.TextBox1.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox1<br/>";
}
if (this.TextBox2.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox2<br/>";
}
if (this.TextBox3.Text.Equals("")) {
valid = false;
errorMessage += "Missing Textbox3<br/>";
}
if (!this.FileUpload3.HasFile) {
// is alway false!
errorMessage += "Missing FileUpload3<br/>";
}
if (valid) {
// never fires, because .HasFile is always false
this.Label1.Text = "valid!";
// do upload stuff
this.FileUpload3.SaveAs("foobar");
} else {
this.Label1.Text = errorMessage;
}
}
And here is my sample ASPX:
<form id="Upload" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:FileUpload ID="FileUpload3" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Does anyone see, why my FileUpload is always empty, although it's outside the UpdatePanel and I do have the required line in the Page_Load() event? If, could you adjust the code?
Thank you
SiS
<form id="Upload" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:FileUpload ID="FileUpload3" runat="server" />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
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;
Is there a way to update a textbox without using postback? I'd like to populate the txtDFS field with the value of (100 - txtStocked) after the user enters data in the txtStocked field.
The following code works, but it uses postback so the page refreshes. Is there a way to accomplish this without refreshing the page?
<asp:TextBox ID="txtStocked" runat="server" Width="75px" AutoPostBack="true" OnTextChanged="txtStocked_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px"></asp:TextBox>
protected void txtStocked_TextChanged(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
txtDFS.Text = txtStocked.Text;
}
Solved this by using an UpdatePanel. Here is the updated code:
<asp:ScriptManager ID="SCManager" runat="server" />
<asp:UpdatePanel ID="SCPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txtStocked" runat="server" Width="75px" autopostback="true" OnTextChanged="Update_Text"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
protected void Update_Text(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
SCPanel.Update();
}