Why won't my function execute (ASP.NET)? - asp.net

Basically I have a aspx page function that will not seem to activate. I've created a function that's supposed to run on selectedindexchange of a dropdownmenu.
<asp:TableCell>Credit Card Type: <asp:DropDownList
id="DropDownCredit"
OnSelectedIndexChanged="creditType"
Runat="server">
<asp:ListItem Text="Select" Value="Empty" />
<asp:ListItem Text="Visa" Value="Visa" />
<asp:ListItem Text="Mastercard" Value="Mastercard" />
<asp:ListItem Text="Discover" Value="Discover" />
<asp:ListItem Text="American Express" Value="American Express" />
</asp:DropDownList>
There as you can see I added my function name to run when they select something. Now my function itself is extremely simple.
Protected Sub creditType()
If DropDownCredit.SelectedIndex.ToString.StartsWith("A") Then
TextBoxCardNumberAmerican.Enabled = True
Else
TextBoxCardNumberOthers.Enabled = True
End If
End Sub
By the way I have set the two textboxes mentioned in the above function to disabled inititally, so only if one is correct then it is enabled. I am going to guess that the string isn't being interpreted properly. Any help would be great thankyou.

Two things: Your creditYpe method has the wrong signature it should be:
Protected Sub creditType(Object o, EventArgs e)
..and add the AutoPostBack="true" property to your asp control
Credit Card Type: <asp:DropDownList
id="DropDownCredit"
OnSelectedIndexChanged="creditType"
AutoPostBack="true"
Runat="server">
<asp:ListItem Text="Select" Value="Empty" />
<asp:ListItem Text="Visa" Value="Visa" />
<asp:ListItem Text="Mastercard" Value="Mastercard" />
<asp:ListItem Text="Discover" Value="Discover" />
<asp:ListItem Text="American Express" Value="American Express" />
</asp:DropDownList>

you should add the AutoPostBack="True" property to the dropdown
only then the server side sub will be executed

Please set the
AutoPostBack="True"
On the dropdown. Like this:
<asp:DropDownList id="DropDownCredit"
OnSelectedIndexChanged="creditType" AutoPostBack="True" Runat="server">
The the event will fire

Related

UpdatePanel posting back entire page for the RadioButtonList control only

I have a webpage with 3 UpdatePanels on. They all work perfectly and have used UpdatePanels for years now and never come across this issue.
The RadioButtonList is posting back the entire page rather than the UpdatePanel and can't see any errors in the output. I have never seen this behaviour before and copied the code into another project, and it worked as expected. All other controls on the page are working as expected in the same format.
Is there any other reasons that this behaviour could occur? My code below:
ASPX Code:
<asp:RadioButtonList ID="rbSelectWeek" runat="server" ClientIDMode="Static" CssClass="rb-select" OnSelectedIndexChanged="rbSelectWeek_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Text="Previous" Value="1" />
<asp:ListItem Text="Current" Value="2" Selected="True" />
<asp:ListItem Text="Next" Value="3" />
<asp:ListItem Text="All" Value="4" />
</asp:RadioButtonList>
<asp:UpdatePanel ID="upCaseViewer" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:HiddenField ID="dtFrom" runat="server" ClientIDMode="Static" Value="1900-01-01" />
<asp:HiddenField ID="dtEnd" runat="server" ClientIDMode="Static" Value="9999-12-31" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cmbTeam" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="rbSelectWeek" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
VB.Net / C#
Protected Sub rbSelectWeek_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rbSelectWeek.SelectedIndexChanged
Dim sVal As Integer = rbSelectWeek.SelectedValue
rbSelectWeek.ClearSelection()
rbSelectWeek.SelectedValue = sVal
Select Case sVal
Case 1
dtFrom.Value = getFunctions.getPrevWeek("Start")
dtEnd.Value = getFunctions.getPrevWeek("End")
Case 2
dtFrom.Value = getFunctions.getCurrWeek("Start")
dtEnd.Value = getFunctions.getCurrWeek("End")
Case 3
dtFrom.Value = getFunctions.getNextWeek("Start")
dtEnd.Value = getFunctions.getNextWeek("End")
Case Else
dtFrom.Value = getFunctions.getAll("Start")
dtEnd.Value = getFunctions.getAll("End")
End Select
End Sub
AsyncPostBackTriggers do not work when an referenced element uses ClientIDMode="Static". So change it to
<asp:RadioButtonList ID="rbSelectWeek" runat="server" CssClass="rb-select" OnSelectedIndexChanged="rbSelectWeek_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Text="Previous" Value="1" />
<asp:ListItem Text="Current" Value="2" Selected="True" />
<asp:ListItem Text="Next" Value="3" />
<asp:ListItem Text="All" Value="4" />
</asp:RadioButtonList>

How to bind dropdownlist using asp.net c#?

<asp:DropDownList id="ddlSampleSize" runat="server">
<asp:ListItem Text="10" Value="10">
<asp:ListItem Text="100" Value="100">
<asp:ListItem Text="1000" Value="1000">
<asp:DropDownList/>
I have bind from codebehind file but I can't see same.
Please help me.
You need to use some sort of datasource to bind. You are just showing static bound data here.
In markup:
<asp:DropDownList id="ddlSampleSize" runat="server">
<asp:ListItem Text="10" Value="10" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="1000" Value="1000" />
</asp:DropDownList>
In code-behind:
ddlSampleSize.Items.Add(new ListItem("10", "10"));
ddlSampleSize.Items.Add(new ListItem("100", "100"));
ddlSampleSize.Items.Add(new ListItem("1000", "1000"));

How to display a message below the dropdownlist not an alert box when specific dropdownlist item is chosen?

To provide consistency, I cannot use an alert box to display a message in my code when someone chooses a specific item in a dropdownlist. It needs to display a red message below the dropdown field only if someone has chosen a specific item.
So in this case if someone chooses Alabama from a state dropdownlist for example, a red message needs to display below the dropdownlist box immediately after the choice is made. The old version of this form works accurately, but the new version of the form with many changes in it is not displaying this message.
In the code behind file (the aspx.vb file) I have these code snippets:
Protected Sub StateDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StateDropDownList.SelectedIndexChanged
If StateDropDownList.Text = "AK" Then
AlabamaPanel.Visible = True
Session("Alabama") = "Yes"
Else
AlabamaPanel.Visible = False
Session("Alabama") = "No"
End If
StateDropDownList.Focus()
End Sub
I'm using previous code with certain edits and another part of the problem is that I don't understand the Session("Alabama") part - I don't find anywhere else in the code where Session("Alabama") is listed or rather to understand what Session() is and how it even would know what an Alabama Session is... So if you know something about Session() that might be helpful - I see this (https://msdn.microsoft.com/en-us/library/ms178581%28v=vs.140%29.aspx) but that's not helping me understand. The only place in the code that Alabama by itself exists is in the items of the dropdownlist. And maybe that is part of the problem since in the old code the Alabama was specifically noted in a long list like (in the aspx file, not the aspx.vb file) such:
<asp:DropDownList class="DropDowns" ID="StateDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="AL">Alabama</asp:ListItem>
<asp:ListItem Value="AK">Alaska</asp:ListItem>
<asp:ListItem Value="AZ">Arizona</asp:ListItem>
<asp:ListItem Value="AR">Arkansas</asp:ListItem>
<asp:ListItem Value="CA">California</asp:ListItem>
<asp:ListItem Value="CO">Colorado</asp:ListItem>
<asp:ListItem Value="CT">Connecticut</asp:ListItem>
<asp:ListItem Value="DE">Delaware</asp:ListItem>
<asp:ListItem Value="DC">District of Columbia</asp:ListItem>
<asp:ListItem Value="FL">Florida</asp:ListItem>
<asp:ListItem Value="GA">Georgia</asp:ListItem>
<asp:ListItem Value="HI">Hawai'i</asp:ListItem>
<asp:ListItem Value="ID">Idaho</asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
<asp:ListItem Value="KS">Kansas</asp:ListItem>
<asp:ListItem Value="KY">Kentucky</asp:ListItem>
<asp:ListItem Value="LA">Louisiana</asp:ListItem>
<asp:ListItem Value="ME">Maine</asp:ListItem>
<asp:ListItem Value="MD">Maryland</asp:ListItem>
<asp:ListItem Value="MA">Massachusetts</asp:ListItem>
<asp:ListItem Value="MI">Michigan</asp:ListItem>
<asp:ListItem Value="MN">Minnesota</asp:ListItem>
<asp:ListItem Value="MS">Mississippi</asp:ListItem>
<asp:ListItem Value="MO">Missouri</asp:ListItem>
<asp:ListItem Value="MT">Montana</asp:ListItem>
<asp:ListItem Value="NE">Nebraska</asp:ListItem>
<asp:ListItem Value="NV">Nevada</asp:ListItem>
<asp:ListItem Value="NH">New Hampshire</asp:ListItem>
<asp:ListItem Value="NJ">New Jersey</asp:ListItem>
<asp:ListItem Value="NM">New Mexico</asp:ListItem>
<asp:ListItem Value="NY">New York</asp:ListItem>
<asp:ListItem Value="NC">North Carolina</asp:ListItem>
<asp:ListItem Value="ND">North Dakota</asp:ListItem>
<asp:ListItem Value="OH">Ohio</asp:ListItem>
<asp:ListItem Value="OK">Oklahoma</asp:ListItem>
<asp:ListItem Value="OR">Oregon</asp:ListItem>
<asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
<asp:ListItem Value="RI">Rhode Island</asp:ListItem>
<asp:ListItem Value="SC">South Carolina</asp:ListItem>
<asp:ListItem Value="SD">South Dakota</asp:ListItem>
<asp:ListItem Value="TN">Tennessee</asp:ListItem>
<asp:ListItem Value="TX">Texas</asp:ListItem>
<asp:ListItem Value="UT">Utah</asp:ListItem>
<asp:ListItem Value="VT">Vermont</asp:ListItem>
<asp:ListItem Value="VA">Virgina</asp:ListItem>
<asp:ListItem Value="WA">Washington</asp:ListItem>
<asp:ListItem Value="WV">West Virgina</asp:ListItem>
<asp:ListItem Value="WI">Wisconsin</asp:ListItem>
<asp:ListItem Value="WY">Wyoming</asp:ListItem>
</asp:DropDownList>
But now we are calling to another VB file to grab the state dropdown list and the full set as shown above is no longer in the code.
Since there is nowhere besides this list that no longer exists or this list that does exist in the separate vb file we are calling to which is successfully populating the dropdownlist, then that could perhaps be blocking the Session() from working?
In the new code (in the aspx file, not the aspx.vb file), all of the above is replaced with:
<label for="StateDropDownList">State <span class="Required">*</span></label>
<asp:DropDownList class="form-control" ID="StateDropDownList" runat="server">
</asp:DropDownList>
I'm trying to use (in the aspx file) :
<asp:Panel ID="AlabamaPanel" runat="server" Visible="false">
<tr>
<td align="center" colspan="3">
<br />
<asp:Label ID="AlabamaLabel" runat="server" visibile="false" Text="Alabama Text that I want to appear right below the statedropdownlist field if they choose alabama."
Font-Bold="True" ForeColor="#e00022" />
</td>
</tr>
</asp:Panel>
to get the text to show up on the page below the dropdown field if the user chooses Alabama and it's simply not displaying. I have also tried putting the long list of states back in instead of calling out to the separate dropdowns vb file.
I'm also trying to use Update Panel, wrapping the dropdown list with an
<asp:UpdatePanel ID="UpdatePanel1"
UpdateMode="Conditional"
runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
And that has benefited me because before that I wasn't able to include the autopostback="true" without it taking me to a blank screen whenever someone chose Alabama from the dropdown list. Now at least the entire screen isn't blanking out... But still I'm not getting the message to show on the screen.
Also I've tried this, but still no message appearing:
<asp:DropDownList class="form-control" ID="StateDropDownList" runat="server" AutoPostBack="True" CausesValidation="False" OnSelectedIndexChanged="StateDropDownList_SelectedIndexChanged">
Any ideas or is anything glaringly obvious as to why my message isn't showing up when someone chooses Alabama?
Try this:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="updtPnl">
<ContentTemplate>
<asp:DropDownList class="form-control" ID="StateDropDownList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="StateDropDownList_SelectedIndexChanged">
<asp:ListItem Value="Al">Alabama</asp:ListItem>
<asp:ListItem>Other value</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="AlabamaLabel" runat="server" visibile="false" Text=""
Font-Bold="True" ForeColor="#e00022" />
</ContentTemplate>
</asp:UpdatePanel>
And change the code-behind as:
Protected Sub StateDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StateDropDownList.SelectedIndexChanged
If StateDropDownList.SelectedValue = "Al" Then
AlabamaLabel.Text = StateDropDownList.SelectedItem.Text + " is selected!"
AlabamaLabel.Visible = True
Else
AlabamaLabel.Visible = False
End If
End Sub
In the original code that you provided you have the following issues:
1) you are looking at the text of the Dropdown for an abbreviation, which is not the text in the dropdown that is the value so you would need to use something like StateDropDownList.SelectedValue = "AK"
2) The AlabamaPanel gets set to visible however; you do not set the visibility of the label to true, so the panel is visible, but has no controls in it that are visible.

ASP.NET RequiredFieldValidator : How can i bind value to " InitialValue " property?

I have the following code
<div>
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue="Choose" ControlToValidate="testDropDownList"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
<br />
</div>
in which i validate Dropdownlist by RequiredFieldValidator
If changed the value of initialvalue property to read from static property in stactic classs .. but it always give me emtpy string in runtime unless this property have the value "Choose" ...
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue='<%# Util.ChooseValue %>' ControlToValidate="testDropDownList">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
Could Any one help me to know what's the issue in my code ??
Please call
testRequiredFieldValidator.databind()
in page load event and let me know if this is still an issue.
Set InitialValue of RequiredFieldValidator on page_load event in aspx.cs page.

Keep file name in FileUpload control after postBack

I have got problem with FileUpload control. I have this one, two drop down list, text box and button. If I select in first dropDownList "Yes" second one become disable and set value on NO (In second ddl I have two option YES or NO and in first one as well) however if I select NO in first dropDownList I posible to choose both option in second dropDownList. First ddl change second one on postBack using selectedIndexChanged evet and when it happends I loose file name in UploadFile control which I set before.
Code sample:
<asp:FileUpload ID="fuUploadGeometry" runat="server" Width="100%" />
<asp:DropDownListID="ddlSymmetry"runat="server" AutoPostBack="true"
onselectedindexchanged="ddlSymmetry_SelectedIndexChanged">
<asp:ListItem Value="0">-- Select --</asp:ListItem>
<asp:ListItem Value="true">Yes</asp:ListItem>
<asp:ListItem Value="false">No</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlModule" runat="server" Enabled="True">
<asp:ListItem Text="-- Select --" Value="0"/>
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="2"/>
</asp:DropDownList>
<asp:TextBox ID="txtTopic" runat="server"></asp:TextBox>
What should I do to keep file name in UploadFile control during changes selected options in drop down lists?
Try this, I added the label so you can see that the postback of the onselectedindexchange only affects the dropdown and not the file upload control, hope this helps.
<asp:FileUpload ID="fuUploadGeometry" runat="server" Width="100%" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlSymmetry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSymmetry_SelectedIndexChanged">
<asp:ListItem Value="0">-- Select --</asp:ListItem>
<asp:ListItem Value="true">Yes</asp:ListItem>
<asp:ListItem Value="false">No</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlModule" runat="server" Enabled="True">
<asp:ListItem Text="-- Select --" Value="0" />
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="2" />
</asp:DropDownList>
<asp:TextBox ID="txtTopic" runat="server"></asp:TextBox>
<asp:Label runat="server" ID="msgFromList" />
</ContentTemplate>
</asp:UpdatePanel>
protected void ddlSymmetry_SelectedIndexChanged(Object sender, EventArgs e)
{
msgFromList.Text = ddlSymmetry.SelectedItem.Value.ToString();
}
First thing keep in ur mind that is FileUpload Control Will became empty if any post back event occur on ur web page. So best solution is to put ur file upload control after all controll that can cause a post back like drop down list.

Resources