Initial text is not populating in textbox - asp.net

I have put an initial text in the text box of a asp.net web page. In the text property box.
I want to load the text in the load time of the webpage.
While i debug and run text box is not populating with that text.
Please help me.
<td class="textFieldColumn" style="height:16px!important;">
<asp:TextBox ID="txtUserID" text="Enter User id.." runat="server" AutoPostBack="true"
CssClass="text_box_3"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUserID" CssClass="requiredField" runat="server" ControlToValidate="txtUserID"
ErrorMessage="* Required field" ValidationGroup="Form2"></asp:RequiredFieldValidator><div class="smallText" nowrap>
(This would be the ID you would normally log on with.)</div></td>
Private Sub mvRequestorForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles mvRequestorForm.Load
txtUserID.Text = "Enter User ID..."
End Sub
<input name="txtUserID" type="text" onchange="javascript:setTimeout('__doPostBack(\'txtUserID\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="txtUserID" class="text_box_3" />
tHE RENDERED CODE

I would use a AJAX TextBoxWatermark
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/TextBoxWatermark/TextBoxWatermark.aspx
<ajaxToolkit:TextBoxWatermarkExtender ID="txtExtender1" runat="server"
TargetControlID="txt"
WatermarkText="Type First Name Here"
WatermarkCssClass="watermarked" />
<asp:Textbox id="txt" ruant="server" />

Related

Is the TextChanged Event not triggered by a TextBox in TextMode="Password"?

I am creating a password text field that shows/hides password requirements as the user types their password into a TextBox with TextMode="Password". I added a TextChanged event so I could check the TextBox text against the requirements each time a new character is typed/removed.
The problem I am having is that the TextChanged event is seemingly not being triggered ever. Here is the relevant code:
<tr>
<td style="text-align: right">
<asp:Label ID="Label1" runat="server" CssClass="BoldLabel14px" Font-Bold="True"
Text="New Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="NewPassword" runat="server" Font-Names="calibri"
Font-Size="14px" TextMode="Password"></asp:TextBox>
</td>
</tr>
Protected Sub NewPassword_TextChanged(sender As Object, e As EventArgs) Handles NewPassword.TextChanged
System.Diagnostics.Debug.WriteLine("NewPassword Text = " & Me.NewPassword.Text)
CheckPasswordRequirements(Me.NewPassword.Text)
Me.PasswordRequirementsCapital.Visible = False
End Sub
From this code, the output never shows the debug statement as the TextBox is modified. Is different way to grab the text as it is written? I am using Visual Studio, vb.net, and asp.net.
The main reason the event isn't fired is because you don't have an event tied up to even be fired.
It should be something like:
<asp:TextBox ID="NewPassword" runat="server" Font-Names="calibri" Font-Size="14px" TextMode="Password" ontextchanged="NewPassword_TextChanged" AutoPostBack="True"></asp:TextBox>
The event itself will not fire until you leave though, it's a server side process and only happens between posts to the server. If you want to handle this as the text is changing look into JavaScript and or JQuery.

Set focus on selected checkbox list item

I am trying to achieve a search function for a checkbox list that i have. Upon typing in the search text, it will set focus on the item in the checkbox list (preferably not through JavaScript).
Below is what i have for the design source.
<asp:Panel runat="server" DefaultButton="Button1">
<br />
<asp:TextBox ID="txtSearchCountry" runat="server" class="form-control" placeholder="Search for a country"/>
<asp:Button ID="Button1" runat="server" Style="display: none" OnClick="Button1_Click1" />
<br />
<div style="OVERFLOW-Y:scroll; WIDTH:100%; HEIGHT:115px">
<asp:CheckBoxList ID="chkCountry" runat="server" RepeatLayout="Table" Height="35px" class="form-control"></asp:CheckBoxList>
</div>
Followed by my vb code.
Protected Sub Button1_Click1(sender As Object, e As EventArgs)
Dim i As Integer
For i = 0 To chkCountry.Items.Count - 1
If InStr(chkCountry.Items(i).Value.Trim(), txtSearchCountry.Text.Trim()) = True Then
End If
Next
End Sub
With the code, i am able to find and match the search text. However, how am i able to set the focus to the item in the checkbox list?

Required field validator doesn´t work

I have this code in my ASPX view:
<asp:TextBox ID="txtDate" runat="server" class="form-control" type="text"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvDate" runat="server" ControlToValidate="txtDate" ErrorMessage="Obligatory field" ViewStateMode="Enabled" CssClass="alert-danger"></asp:RequiredFieldValidator>
</div>
the error message appears correctly when I leave the field empty, but I also want to show an error message when the date is not correctly, I do this in my code behind but it doesn´t work:
Private Sub Mybutton_Click(sender As Object, e As EventArgs) Handles Mybutton.Click
If IsDate(txtDate.Text.ToString) = False Then
rfvDate.IsValid = False
rfvDate.Visible = True
rfvDate.ErrorMessage = "Check that it is a valid date"
Exit Sub
Else
'DO THE REST OF THE CODE
End Sub
What am I doing wrong? thanks
you can use both RequiredField Validator along with RegularExpressionValidator
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtDate" ValidationExpression="(((0|1)[1-9]|2[1-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$"
ErrorMessage="Invalid date format." CssClass="alert-danger" ValidationGroup="Group1" />
Add the ValidationGroup="Group1" to the Button aspx markup

txtAddress is not declared. I cannot reference TextBox inside of AJAX .NET VB Update Panel

Trying to fill a few textboxes using AJAX, VB .NET:
<div align="left">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="pnlAddresses" runat="server">
<ContentTemplate runat="server">
<asp:dropdownlist id="ddlVenueAddresses" Width="264px" Height="24px" runat="server" AutoPostBack="true" OnSelectedIndexChanged ="ddlVenueAddresses_SelectedIndexChanged" CssClass="admin" BackColor="Transparent" DataTextField="VenueName" DataValueField="VenueAddress"></asp:dropdownlist> (optional)
<br />
<input name="txtAddress" id="txtAddress" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtAddress")%>'/>
<br />
<input name="txtGoogleAddress" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtGoogleAddress")%>'/>
<br />
<input name="txtGoogleCity" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtGoogleCity")%>'/>
<br />
<input name="txtGoogleState" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtGoogleState")%>'/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
dropdown list gets populated and when item is picked, code stops on breakpoint where it should. But stepping through I get error when trying to just add simple "xxxx"s to textbox as a test:
Public Sub ddlVenueAddresses_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim txtAddressFound As TextBox
txtAddressFound = pnlAddresses.FindControl("txtAddress")
txtAddressFound.Text = "xxxxx"
End Sub
txtAddressFound is Null and when trying to set it to "xxxxx" I get this error:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
If I just trying to set txtAddress.Text = "xxxx" with no FindControl method, it says it doesn't exist:
? txtAddress
'txtAddress' is not declared. It may be inaccessible due to its protection level.
Totally lost. This application has been working for years, I just wanted to use Ajax to populate some address stuff.
Does anyone have any ideas?
I have tons of textboxes on this form (not using AJAX on them). I also put this at the top under Inherits System.Web.UI.Page with no luck.
Protected WithEvents txtAddress As System.Web.UI.WebControls.TextBox
Because you are trying to access client side control txtAddress from Server side script.
In ASP.NET, control should be
<asp:TextBox ID="txtAddress" runat="Server" />
This way you can access it from Server Side.
<input name="txtAddress" id="txtAddress" runat="server" type="text" size="70" maxlength="100" value='<%= Request.Form.Get("txtAddress")%>'/>
protected void ddlVenueAddresses_SelectedIndexChanged(object sender, EventArgs e)
{
txtAddress.Value = "XXXX";
}
I tested the above it works, please check ones. I kept the input html control as is but added runat="server" attribute, I am not sure why are you instantiating a new TextBox to access your input control.

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!

Resources