error message is not displayed in validation summary - asp.net

I have a following code on my web page.
table>
<tr>
<td class="style2"> <asp:ValidationSummary
ID="ValidationSummary1"
runat="server"
HeaderText="Following error occurs....."
ShowMessageBox="false"
DisplayMode="BulletList"
ShowSummary="true"
BackColor="Snow"
Width="200"
ForeColor="Red"
Font-Size="X-Large"
Font-Italic="true" EnableClientScript="true" />
</td>
</tr>
<tr>
<td valign="top" class="style2">
Test1 :
<asp:DropDownList ID="ddlTest1" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqTest1" runat="server" ControlToValidate="ddlTest1" InitialValue="Please Select" ErrorMessage="Please Select Test1"></asp:RequiredFieldValidator>
</td>
<td valign="top">
<asp:DropDownList ID="ddlLocation" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqLocation" runat="server" ControlToValidate="ddlLocation" InitialValue="Please Select" ErrorMessage="Please select a Location"></asp:RequiredFieldValidator>
</td>
</tr>
I am not getting any error message when I am clicking on submit button without selecting any dropdowns value. My dropdown code is
Private Sub Test1()
ddlTest.DataSource = ' called the class to bid the drop down
ddlTest.DataTextField = "Test"
ddlTest.DataValueField = "Testid"
ddlTest.DataBind()
ddlTest.Items.Insert(0, New ListItem("Please Select", 0))
End Sub
Public Sub LoadLocation()
' ddlLocation.DataSource = ' called the class to bid the drop down
ddlLocation.DataTextField = "location"
ddlLocation.DataValueField = "id"
ddlLocation.DataBind()
ddlLocation.Items.Insert(0, New ListItem("Please Select", 0))
End Sub
Any help will be appreciated. I cannot see any error message when I click on submit button and the control gets transferred to the code behind of submit button.

Dropdown validation is a little bit different. You need to have InitialValue set in validator to verify and error out for non-selection
<asp:RequiredFieldValidator ID="ReqTest1" runat="server" InitialValue=0 ControlToValidate="ddlTest1" InitialValue="Please Select" ErrorMessage="Please Select Test1"></asp:RequiredFieldValidator>
InitialValue is whatever value of the list item appears by default in the drop down.
It is also important to check Page.IsValid in your button click event, before you process anything.

Related

Using TextBox TextChanged event to enable or disable a button control in asp.net

I am using an asp TextBox control with its TextChanged event and my goal is to capture text as a user enters it. If there are one or more characters entered, I would like a button control to be enabled without the user having to leave the TextBox control.
My source code for the TextBox on the aspx page is
<asp:TextBox ID="NewSpendingCategoryTextBox" MaxLength="12" runat="server"
AutoPostBack="True"
OnTextChanged="NewSpendingCategoryTextBox_TextChanged"
ViewStateMode="Enabled" >
</asp:TextBox>
and my source code on the code behind page is
Protected Sub NewSpendingCategoryTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles NewSpendingCategoryTextBox.TextChanged
Dim strSpendingCategoryTextBox As String = Nothing
strSpendingCategoryTextBox = NewSpendingCategoryTextBox.Text
If strSpendingCategoryTextBox.Length <= 0 Then
Me.NewSpendingCategoryInsertButton.Enabled = False
Else 'strSpendingCategoryTextBox.Length > 0
Me.NewSpendingCategoryInsertButton.Enabled = True
End If 'strSpendingCategoryTextBox.Length <= 0
End Sub
So it appears I have to use javascript to enable or disable the insert button. Can someone guide me on how to get an element within a table? The table sits in a Panel as well.
below is the aspx code...
<asp:Panel ID="AddSpendingCategoryPanel" Visible="false" runat="server">
<table class="AddNewTable">
<tbody>
<tr>
<td>
<asp:Label ID="lblSpend" runat="server"
Text="Spending Category:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="txtSpend" MaxLength="12"
runat="server"
AutoPostBack="True"
OnTextChanged="txtSpend_TextChanged"
OnKeyDown="return CheckSpendTextBoxValue()"
ViewStateMode="Enabled" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button CssClass="frmbtn" ID="btnInsertSpend"
runat="server" Text="Insert" />
</td>
<td>
<asp:Button CssClass="frmbtn" ID="btnCancelSpend"
runat="server" Text="Cancel"
CausesValidation="False" />
</td>
</tr>
</tbody>
</table>
</asp:Panel>
Run this code in the OnKeyPress event or consider JavaScript. The textbox does not fire the Text_Changed event til Tab or Enter are used.
Simplify the boolean check.
Me.NewSpendingCategoryInsertButton.Enabled = (NewSpendingCategoryTextBox.Text.Length <> 0)
I'm not sure exactly how you would do it. But the ASP.NET code is executed on the server that is hosting the web page.
I'd highly recommended doing it on JavaScript which can be run client side. Hopefully this article is of use to you.
How to check if a textbox is empty using javascript

how to pass ID value from one page to another

I have three hyperlinks in my table.
These hyperlinks are used for the print preview of the crystal reports.
The problem is the selected content of the print preview of the crystal report should be equal to the ID from the other page.
here is the code behind, this page will be responsible for the passing and redirecting of the ID
Sub OnFinishButtonClick(ByVal sender As Object, ByVal e As WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
If Wizard1.ActiveStepIndex = 6 Then
''au1.regau1()
ElseIf Wizard1.ActiveStepIndex = 7 Then
'au2.regau2()
End If
Response.Redirect("~/operator/renewInstruction.aspx")
End Sub
and this is the other page.
Here is my hyperlink in table:
<table class="req">
<tr class="field_value3">
<td><asp:Label ID="req3" runat="server" Text="A. Affidavit of Undertaking(FVO shall comply with the requirements of R.A. 8550)" ></asp:Label></td><td>
<asp:HyperLink ID="au8550_h4" runat="server" ForeColor ="blue" Target="_Blank"><asp:Image ID="Image3" runat="server" ImageUrl ="~/images/preview.png" Height="25px" Width="30px" ImageAlign="AbsMiddle"/></asp:HyperLink></td>
</tr>
<tr class="field_value4">
<td><asp:Label ID="req4" runat="server" Text="B. Affidavit of Undertaking to the effect that the FVO shall comply with FAQ No. 232 (applicable only to fishing vessels which intend to operate in Manila Bay)" ></asp:Label></td><td>
<asp:Hyperlink ID="au232_h3" runat="server" Target="_blank" ForeColor ="Blue" ><asp:Image ID="Image4" runat="server" ImageUrl ="~/images/preview.png" Height="25px" Width="30px" ImageAlign="AbsMiddle"/></asp:Hyperlink></td>
</tr>
<tr class="field_value5">
<td><asp:Label ID="req5" runat="server" Text="C. Certification to the effect that the fishing vessel is not involved in any administrative or judicial case" ></asp:Label></td><td>
<asp:HyperLink ID="Cert_h5" runat="server" Target ="_blank" ForeColor ="blue"><asp:Image ID="Image5" runat="server" ImageUrl ="~/images/preview.png" Height="25px" Width="30px" ImageAlign="AbsMiddle"/></asp:HyperLink></td>
</tr>
</table>
here is the code behind for the hyperlink:
au232_h3.NavigateUrl = "~/operator/printAU232.aspx?Au232VesselID=" & Session("vid")
au8550_h4.NavigateUrl = "~/operator/printAu8550.aspx?Au8550VesselID=" & Session("vid")
Cert_h5.NavigateUrl = "~/operator/printCERT.aspx?CertVesselID=" & Session("vid")
how will i able to pass the value of vid to the other page so that it will be the unique basis for the selection of the print preview content of the crystal report.

Do a Button Click from Code behide

I have a gridview which lists Tools and Access values. To edit I have an edit imagebutton on each row. I have an OnRowBound method which assigns an OnClick attribute to each button so that I will know which record I need to edit.
The code is
Protected Sub ChangeFirstRowIcon(ByVal Sender As Object, ByVal e As GridViewRowEventArgs) Handles gv_AccessRights.RowDataBound
'This sub fires on each gridview row created...
'It first checks that the row is a data row (as opposed to Header, Footer etc.)
'If ib_Edit is true then change add an attribut to button with aid, tid and ac values attached.
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ib_Edit As ImageButton = e.Row.FindControl("ib_Edit")
Dim lb_AccessID As Label = e.Row.FindControl("lb_AccessID")
Dim hd_ToolID As HiddenField = e.Row.FindControl("hd_ToolID")
Dim hd_AccessCode As HiddenField = e.Row.FindControl("hd_AccessCode")
If ib_Edit IsNot Nothing Then
ib_Edit.Attributes.Add("onClick", "proxyClick('" & lb_AccessID.Text & "', '" & hd_ToolID.Value & "', '" & hd_AccessCode.Value & "')")
End If
End If
End Sub
I'm using a hidden proxy button to show a modal popup which the user will use to edit a record... (the same popup will be used to add a new access record... but that will come later). So having passed my details to proxyClick I set values to controls within the modal popup. The javascript is....
<script type="text/javascript">
function proxyClick(aid, tid, ac) {
document.getElementById('hd_AccessID').value = aid;
document.getElementById('hd_ToolIDMod').value = tid;
document.getElementById('hd_AccessCodeMod').value = ac;
document.getElementById('but_SetModalDetails').click();
}
</script>
For reference the main bits of the markup are....
<table class="border">
<tr>
<td>
<asp:Button ID="but_SetModalDetails" runat="server" Style="display: none" Text="Set modal details" ClientIDMode="Static" UseSubmitBehavior="true" />
<asp:Button ID="but_HiddenProxy" runat="server" Style="display: none" Text="Hidden Proxy Button for Modal Popup" ClientIDMode="Static" />
</td>
<td class="rt">
<asp:Button ID="but_AddTool" runat="server" AccessKey="A" CssClass="butGreen" Text="Add Tool" ToolTip="Add Tool - Alt A" />
</td>
</tr>
</table>
<asp:ModalPopupExtender ID="mpx_AddEditAccess" runat="server" CancelControlID="but_Cancel"
BehaviorID="pn_AddEditAccess" PopupControlID="pn_AddEditAccess" TargetControlID="but_HiddenProxy"
BackgroundCssClass="modalBackground" />
<asp:Panel ID="pn_AddEditAccess" runat="server" Width="500px" CssClass="modalPopup"
Style="display: block">
<div class="box">
<h2>
<asp:Label ID="lb_ModTitle" runat="server"></asp:Label>
</h2>
<asp:HiddenField ID="hd_AccessID" runat="server" ClientIDMode="Static"></asp:HiddenField>
<div class="block">
<asp:UpdatePanel ID="up_Access" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_ToolName" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<table>
<tr>
<th class="p66 rt">
Tool Name:
</th>
<td class="p66">
<asp:HiddenField ID="hd_ToolIDMod" runat="server" ClientIDMode="Static" />
<asp:DropDownList ID="ddl_ToolName" runat="server" AutoPostBack="true" AppendDataBoundItems="True"
DataSourceID="SqlDS_Tools" DataTextField="ToolName" DataValueField="ToolID" OnSelectedIndexChanged="ddl_ToolName_SIC">
<asp:ListItem Text="Please Select..." Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDS_Tools" runat="server" ConnectionString="<%$ ConnectionStrings:ToolsConnString %>"
SelectCommand="SELECT [ToolID], [ToolName] FROM [tbl_Tools] WHERE ([Redundant] = #Redundant)">
<SelectParameters>
<asp:Parameter DefaultValue="False" Name="Redundant" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>
<asp:RequiredFieldValidator ID="rfv_ddl_ToolName" runat="server" ControlToValidate="ddl_ToolName"
CssClass="error" Display="Dynamic" ErrorMessage="Please Select Tool Name" InitialValue="0">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<th class="p66 rt">
Access Rights:
</th>
<td class="p66">
<asp:HiddenField ID="hd_AccessCodeMod" runat="server" ClientIDMode="Static" />
<asp:DropDownList ID="ddl_AccessCode" runat="server" Enabled="false">
<asp:ListItem Text="No Access" Value="0"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="p66">
<asp:Button ID="but_Cancel" runat="server" Text="Cancel" />
</td>
<td class="p66 rt">
<asp:Button ID="but_Save" runat="server" Text="Save" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</asp:Panel>
As you can see I have implemented two hidden buttons but_SetModalDetails and but_HiddenProxy. but_SetModalDetails has some codebehind which sets a couple of dropdown lists (one populated from a datasource, the other is populated dynamically based on the value of the first. The codebehind is...
Protected Sub but_SetModalDetails_Click(ByVal sender As Object, ByVal e As EventArgs) Handles but_SetModalDetails.Click
If hd_AccessID.Value = "0" Then
lb_ModTitle.Text = "Assigning Access Rights to:"
ddl_ToolName.SelectedIndex = 0
ddl_AccessCode.SelectedIndex = 0
ddl_AccessCode.Enabled = False
Else
lb_ModTitle.Text = "Edit Access Rights to:"
ddl_ToolName.SelectedValue = hd_ToolIDMod.Value
ddl_ToolName.Enabled = False
SqlStr = "SELECT AccessID AS ddlValue, AccessText as ddlText FROM tbl_AccessCodes WHERE ToolID = " & hd_ToolIDMod.Value
PopulateDDLvalue(ddl_AccessCode, SqlStr)
ddl_AccessCode.SelectedValue = hd_AccessCodeMod.Value
ddl_AccessCode.Enabled = True
End If
'NOW I NEED TO SIMULATE but_HiddenProxy Click
End Sub
As you can see at the end I need to simulate a click of but_HiddenProxy so that the modalPopup is shown populated with the correct data.
Any Ideas? Thanks
After all that... I was able to do everything in codebehind...
I only needed one hidden button but_HiddenProxy.
In the gridview instead of setting an onClick attribute for each edit image button I just set a commandname of 'AccessEdit' (don't use 'Edit'). I then had a method that handled the gridview.RowCommand event. This found the various info I needed by using findControl on the selected row. These values were then used to populate the dropdowns in the popup and then use the show command to make the popup visible.
One bit that did stump me for a while was why my RowCommand was not triggering when an imagebutton was clicked. I'd forgotten that I had validation in the modal which stopped the RowCommand being executed. I stuck a CausesValidation="false" in the imagebutton and all was OK.
Talk about using a hammer to crack a nut!

CustomValidator handler not being called after user clicks the Update link on the GridView

On the DropDownList SelectedIndexChanged, the page posts back and records the name of the developer in the radiobutton. When the user clicks the Update link on the GridView, I want the customValidator to check if the Radiobutton is empty.
<EditItemTemplate>
<table>
<tr>
<td>
<asp:DropDownList ID="_ddlAllDev" runat="server" AutoPostBack = "true" DataTextField = "Text"
DataValueField = "Value" OnSelectedIndexChanged = "ddlAllDev_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:RadioButtonList ID="_rblAllDev" runat="server" AutoPostBack = "true" DataTextField = "Text"
DataValueField = "Value" OnSelectedIndexChanged = "rblAllDev_SelectedIndexChanged">
</asp:RadioButtonList>
<asp:CustomValidator ID="_valDev" runat="server" ControlToValidate = "_rblAllDev" Text = "*"
ErrorMessage="Please choose at leat one developer for this task" OnServerValidate = "AllDev_ServerValidate" />
</td>
</tr>
</table>
</EditItemTemplate>
The problem is that it looks like the CustomerValidator handler is not even being called after the user click the UPPDATE link on the GridView. I have put a break point inside the method but the method is not even being called.
protected void AllDev_ServerValidate(object sender, ServerValidateEventArgs args)
{
}
Am I missing something?
Thanks for helping.

How to get Selected Value in FormView Control?

I'm working with an application develop by ASP.NET, the problem I face is using FormView control, the FormView Control has ItemTemplate, InsertItemTemplate and EditItemTemplate.
Below is the code fragment of InsertItemTemplate:
<asp:FormView ID="FormView1" runat="server" DefaultMode="ReadOnly">
<InsertItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Label id="lblPS" runat="server" Text="Process Status"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlPS" runat="server"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label id="lblAP" runat="server" Text="Action Plan"></asp:Label>
</td>
<td>
<asp:TextBox id="txtAP" runat="server" Width="230px" TextMode="MultiLine" Rows="5"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FormView>
In Page_Load event, I do DataSource Binding into the DropDownList as below:
FormView1.ChangeMode(FormViewMode.Insert);
DropDownList ddlPS = FormView1.FindControl("ddlPS") as DropDownList;
ddlPS.DataSource=GetProcessStatus();
ddlPS.DataBind();
ddlPS.Items.Insert(0, new System.Web.UI.WebControls.ListItem("- Please Select -", "- Please Select -"));
The data binding into DropDownList and the "- Please Select -" was ok.
Here the problem comes, when Submit Button click, I wanted to get user selected DropDownList Value, but the DropDownList.SelectedItem.Text always return me "- Please Select -".
Please advise how can I get the user selected value in InsertItemTemplate.
The problem is with your DataBind on Page Load event.
When you DataBind you clear the existing values and hence loose the selected value.
A drop down list remembers the items in it, so you don't need to DataBind on every postback.
Your could should be like this.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList ddlPS = FormView1.FindControl("ddlPS") as DropDownList;
ddlPS.DataSource=GetProcessStatus();
ddlPS.DataBind();
ddlPS.Items.Insert(0, new System.Web.UI.WebControls.ListItem("- Please Select -", "- Please Select -"));
}
}

Resources