Ajax in UserControl - asp.net

i have a signup page and i want to check username exists or not with ajax
<%# Control Language="C#" AutoEventWireup="true" CodeFile="Simple.ascx.cs" Inherits="UserControls_Simple" %>
<div style="direction:rtl;">
<table style="font-family:Tahoma;font-size:12px;width:200px;">
<tr>
<td>نام کاربری:</td>
<td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Label ID="lblMessage" runat="server" Text="Username Already Exists" Visible="False"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>کلمه عبور</td>
<td><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
</tr>
</table>
</div>
i want to show label if user exists
how can i do that?

Using Ajax to check if a user exists in your DB via a User Control (or whatever method you are using) is the same method you would use in a Web Page.
What kind of Ajax do you want to use? MS Ajax, jQuery, what?
Regardless, you will need to Call Web Services from Client Script to perform an AJAX request.

Related

How to make asp .net to forget entered text in fields?

Everywhere I read questions about how to remember it, but I want to reset it!
I use this code:
<table>
<tr>
<td>Your name:</td>
<td><asp:TextBox runat="server" ID="user_name" MaxLength="20" AutoCompleteType="None" /></td>
</tr>
<tr>
<td><table><tr><td>Captcha:</td><td><img src="turing.aspx" alt="Enable images to see captcha image" /></td></tr></table></td>
<td><asp:TextBox runat="server" ID="txt_captcha" MaxLength="5" AutoCompleteType="None" /></td>
</tr>
<tr>
<td>Email for new messages reports:</td>
<td><asp:TextBox runat="server" ID="txt_eml" MaxLength="100" AutoCompleteType="None" /> (*this is not required)</td>
</tr>
<tr>
<td>Message text:</td>
<td><asp:TextBox runat="server" ID="comment_text" Columns = "30" Rows = "5" Wrap = "true" TextMode="MultiLine" AutoCompleteType="None"/></td>
</tr>
<tr>
<td colspan = "2">
<asp:Button runat="server" ID="Button1" Text="Add message" OnClick="HandleAddMessage" CausesValidation="true" ValidationGroup="second_group" />
</td>
</tr>
</table>
At HandleAddMessage I even reset those manually that way: user_name.Text = txt_eml.Text = txt_captcha.Text = comment_text.Text = string.Empty;
But after message added, if I refresh the page old values comes back.
What I can do to prevent it?
Once the user has submitted the form the first time, just response.redirect to the same page again. It effectively resets the page, so if they do refresh it, they won't post back the same data. It would also mean that your fields will all be reset since the page has effectively reloaded.

Cannot use CheckBox in UserControl missing FORM Tag (ASPX)

how do I use a within an UserControl?
I always get the error, that the CheckBox should be placed within a FormTag (with runat=server), which is still existing on the MasterPage and it is not allowed to have two of them ...
Code of UserControl:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ctrlOrderEmailsDialog.ascx.cs" Inherits="WebClient.Controls.ctrlOrderEmailsDialog" %>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<div id="OrderEmailDialog" runat="server">
<table id="OrderEmailDialogTable">
<tbody>
<tr>
<th>Nr.</th>
<td>
<asp:Label ID="labelId" runat="server"></asp:Label></td>
</tr>
<tr>
<th>ReturnReceipt:</th>
<td>
<%--<telerik:RadButton ID="checkboxReturnReceipt" runat="server" ToggleType="CustomToggle" ButtonType="ToggleButton" AutoPostBack="false">
<ToggleStates>
<telerik:RadButtonToggleState Text="UnChecked" PrimaryIconCssClass="rbToggleCheckbox"></telerik:RadButtonToggleState>
<telerik:RadButtonToggleState Text="Filled" PrimaryIconCssClass="rbToggleCheckboxFilled"
Selected="true"></telerik:RadButtonToggleState>
<telerik:RadButtonToggleState Text="Checked" PrimaryIconCssClass="rbToggleCheckboxChecked"></telerik:RadButtonToggleState>
</ToggleStates>
</telerik:RadButton>--%>
<asp:CheckBox ID="checkboxReturnReceipt" runat="server" />
</td>
</tr>
<tr>
<th>Mailtext:</th>
<td>
<asp:Label ID="OrderEmailText" runat="server">
</asp:Label>
</td>
</tr>
</tbody>
</table>
</div>
Form Tag (already existing within the SiteMaster page):
<form id="MainForm" runat="server">
System.Web.HttpException:
"The control ctrlOrderEmailsDialog_checkboxReturnReceipt type CheckBox must be positioned in a form tag with runat = server." which is existing ...
Can you guys give me a small hint?
Thank you.
//SiL

Hide tr or td in a table using vb.net in codebehind

In my masterpage I have links for users also the authorization is different like admin and regular user.
links under each other and I can hide the hyperlinks depending on authorization status but the problem is i.e when I have 3 links the second link for the admin the link will hide when the user is regular and the link place empty like 123 1 3.
So I have an idea using table each link in one tr but I can`t hide td or tr because Visible is not in properties.
any help?
thank you
According to how to hide a having asp.net control:
you can give ID either to the TD or TR to which you want to hide/show
with the runat="server" and also you can take that tr/td inside the
div tag and give id to that div tag and also runat=server attribute
and after that you can pro grammatically hide/show that div.
like
<pre>
<tr id="trhide" runat="server"> </tr>
</pre>
in code behind write
trhide.visible=true/false
In the master page VB code behind add a public procedure: Then call the public set from your aspx page.
'======================================================================================================
'Set Tab No invisible
'======================================================================================================
Public Sub setTabNumberLabel(visible As Int16)
If visible = 0 Then
td_page.Visible = False
Else
td_page.Visible = True
End If
End Sub
The master aspx would be:
<table style="width:100%">
<!--<tr style="background-color:#565656;">-->
<tr>
<td style="width:15%;text-align:left;vertical-align:bottom;padding-left:20px;">Stategic Energy Assessment ( <asp:Label ID="lbl_year_ended" runat="server" /> )</td>
<td style="text-align:center;vertical-align:bottom;"><asp:Label ID="lbl_utility_name_and_id" runat="server" /></td>
<td id="td_page" runat="server" style="width:15%;text-align:right;vertical-align:bottom;padding-right:20px;">Tab No: <asp:Label ID="lbl_page" runat="server" /></td>
</tr>
<tr><td colspan="3" style="vertical-align:central"><hr /></td></tr>
<tr>
<td style="width:15%;text-align:left;vertical-align:central">
<asp:Label ID="lbl_print_version" runat="server" Text="View Printable Vision" Visible="false" />
</td>
<td style="font-size:larger; font-weight:bold; text-align:center; text-transform:capitalize;vertical-align:central">
<asp:Label ID="lbl_schedule_name" runat="server" />
</td>
<td style="width:15%;text-align:right;vertical-align:central;padding-right:20px;">
<asp:LinkButton ID="btn_footnotes" runat="server" Visible="false">Footnotes</asp:LinkButton>
</td>
</tr>
<%--<tr><td colspan="3" style="vertical-align:central" class="auto-style1"></td></tr>--%>
<tr><td colspan="3" style="vertical-align:central; padding-right:20%;padding-left:20%; ">
<i><asp:Label ID="lbl_headnotes" runat="server" Text="" /></i></td></tr>
<tr><td colspan="3" style="vertical-align:central"><hr /></td></tr>
</table>
The other answer is correct and works fine. Just adding complete piece of code.
It's quite amusing that you don't need to add runat=server for a table but you can still hide tr for that table using runat attribute.
<table>
<tr>
<td>aa</td><td>bb</td>
</tr>
<tr id="trHide1" runat="server">
<td>aa</td><td>bb</td>
</tr>
<tr id="trHide2" runat="server">
<td>aa</td><td>bb</td>
</tr>
<tr>
<td>aa</td><td>bb</td>
</tr>
</table>
Now just set properties in codebehind (hiding the tr)
trHide1.Visible = false;
trHide2.Visible = false;

How to a use a web user control more than one time on the same page?

I have created web user control for calendar. I.e it has a textbox and a calendar image. By clicking on calendar popup the calendar opens. Then by selecting any date it stores the year of that date. I use this code:
<script language="javaScript" type="text/javascript" src="Scripts/Calendar.js"> </script>
<link href="Styles/Calendar.css" rel="stylesheet" type="text/css" />
<table>
<tr>
<td>
<input type="text" name="datum1"/>
</td>
<td>
<img id="Img1" src="images/calFinal.jpg" alt="" runat="server" onclick="setYears(1947, 2040);
showCalender(this, 'datum1');" />
</td>
</tr>
</table>
<div>
<!-- Calender Script -->
<table id="calenderTable">
<tbody id="calenderTableHead">
<tr>
<td colspan="4" align="center">
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear').value,
this.selectedIndex, false));" id="selectMonth">
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>
</td>
<td colspan="2" align="center">
<select onchange="showCalenderBody(createCalender(this.value,
document.getElementById('selectMonth').selectedIndex, false));" id="selectYear">
</select>
</td>
<td align="center">
<font color="#003333" size="+1">X</font>
</td>
</tr>
</tbody>
<tbody id="calenderTableDays">
<tr style="">
<td>Sun</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thu</td>
<td>Fri</td>
<td>Sat</td>
</tr>
</tbody>
<tbody id="calender">
</tbody>
</table>
<!-- End Calender Script -->
</div>
The problem occurs when I use the user control twice on the same web form. I have registered the control. When I use it once, it works proper. But I want it at two places. So, please give some solution on how to use the user control more than one time on the same web form. I have given different IDs for the user control, but it still does not work.
Code for user control:
<tr>
<td class="directorytdWidth directorylabel">
School Name:
</td>
<td class="directoryTdPadding">
<asp:TextBox ID="txtSchoolName" runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
School Passout Year:
</td>
<td class="directoryTdPadding">
<uc1:calendar ID="schoolPassout" runat="server" />
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
College Name:
</td>
<td class="directoryTdPadding">
<asp:TextBox ID="txtCollegeName" runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
College Passout Year:
</td>
<td class="directoryTdPadding">
<uc1:calendar ID="collegePassout" runat="server" />
</td>
</tr>
Use the Same User control but With diferent ID. For example (See code below)
Reg Your User control in you Web page
<%# Register Src="~/Controls/SearchAffiliated.ascx" TagPrefix="uc" TagName="SearchAffiliated" %>
First User Control call
<uc:SearchAffiliated runat="server" ID="ucSearchAffiliated" />
Second User Control Call
<uc:SearchAffiliated runat="server" ID="SearchAffiliated1" />
See The Users Controls have diferents IDs, That Works For call The same UC in one web form.
Cheers.
Use the loop of how many times that u want the usercontrol to display:
for(int i=0;i < n; i++) //Define the number of time that u want to display
{
usercontrolname.id="UC"+i;//Assign the id for that usercontrol
//add line of code,that controls in placeholder or panel
}
You can use same user control in same page in N number of times. either it dynamically or statically. Only the thing is we need to give the different ID for the usercontrol
This need to add in your Reg
<%# Register Src="~/UserControls/Hello.ascx" TagPrefix="uc" TagName="Hello" %>
calling the user control 1
<uc:Hello runat="server" ID="ucHello1" />
calling the user control 2
<uc:Hello runat="server" ID="ucHello2" />
I faced same problem but the problem was not related with the ID you prefer to use inside the asp.net page. Because I guess asp.net automatically generates ID for each element in page if it doesn't have one.
The problem was I copied ascx files and forget to change their class names. Make sure you use different class names for each user control.
<%# Control Language="C#" ClassName="ucSchema2" %>
I know this i late to the game but maybe someone can use this answer anyway.
Using a static clientside id for a html element in a user control and then using that user control multiple times will results in invalid html.
You are only allowed to use an id once on a html page otherwise getElementById (which returns a single element) will not know which element to get and then you will probably not get any element whatsoever.
You need to make those clientside ids inside the user control truely unique, i.e. selectMonth, selectYear.
This might be done by appending the ClientID of the user control to the element id. Something like this (not sure if this approach really works but you get the idea):
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear_<%=this.ClientID%>').value,
this.selectedIndex, false));" id="selectMonth_<%=this.ClientID%>">
This way the clientside id should be a combination of locally unique ids and the page unique control ids.

Recaptcha ErrorMessage Property in ASP.NET

I am using recaptcha in an ASP.NET 3.5 application and for some odd reason the ErrorMessage property is not working. Below is my code
<recaptcha:RecaptchaControl ID="recaptcha" runat="server" PublicKey="*******************************" PrivateKey="*******************" Theme="white" ErrorMessage="This is an typo error" />
When the typed text doesn't match with the recaptcha image text it still shows the default error message "Incorrect. Try again." instead of my custom error message. What could be the reason for this strange behavior
You put the message in a Literal tag. Here is my code snippet:
<tr>
<td align="center" colspan="2" >
<recaptcha:RecaptchaControl Theme="white" ID="recaptcha" runat="server" PrivateKey="*************"
PublicKey="*************" />
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color: red">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False" Text="You have not entered the verification words correctly. Please try again."></asp:Literal>
</td>
</tr>
Mike

Resources