How to Apply Globalization On Web User Control in asp.net c# - asp.net

i have applied globalization on default page but it is not applied on Web User Control.
I have created Header.ascx and put two linkButton one for English and second for spanish.
i am using code on header.ascx form code is below ... .
this code for english
protected void lbtnEng_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = "en";
Response.Cookies.Add(cookie);
Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
Server.Transfer(Request.Path);
}
this code for Spanish language
protected void lbtnSpan_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = "es-mx";
Response.Cookies.Add(cookie);
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-mx");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-mx");
Server.Transfer(Request.Path);
}
this code change language on default page label text but not on web user control label
declare control on default page
<asp:Label ID="lbl_test" runat="server" Text="<%$ Resources:Resource, Test %>"></asp:Label>
and on web control
<asp:Label ID="lbl_test" runat="server" Text="<%$ Resources:Resource, Test %>"></asp:Label>

you can set only one language at a time. In order to achieve your task .Use static strings or Take one resource file which consist of key value pairs. This is an example for submit key.
ResourceFileForStaticStrings.resx
Key Value
submitEnglish Submit
submitSpanish Presentar
Store it in a session or a cookie: Try the below one
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(Session("es").ToString)
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(Session("es").ToString)

Related

How to prevent a user from resubmitting a form?

I am developping a Single Page Application.
At the end of the application, the user gets to submit his contact information (name, phone number, etc). This sends an Email and modifies the page to a "Thanks for submitting [...]" page.
The problem is, the client can press the Back button and REsend the Email.
Is there a way to prevent this sort of.. spam?
Code
Sub BT_Send(sender As Object, e As EventArgs) Handles BT_Send.Click
Try
'Creating the Email Message
Dim mailMessage As New MailMessage()
mailMessage.To.Add("SomeOne#a.com")
mailMessage.From = New MailAddress("Robot#a.com", "Robot")
mailMessage.Subject = "Test"
mailMessage.IsBodyHtml = True
mailMessage.Body = LBL_Emailbody.Text & _
"<br><br><br><div style=""font-size: 0.7em;"">Robot speaking, I will not answer if you send me a message.</div>"
Dim smtpClient As New SmtpClient("Something.com")
smtpClient.Send(mailMessage)
PNL_Before.Visible = False
PNL_After.Visible = True
Catch ex As Exception
LBL_errorEmail.Visible = True
'Should never happen...
End Try
End sub
Here is a very simple example, where I use a static variable on page and avoid database.
the asp.net page is
<asp:Literal runat="server" ID="txtInfos"></asp:Literal><br />
<asp:TextBox runat="server" ID="txtEmail"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />/>
and the code behind.
static Dictionary<string, DateTime> cLastSubmits = new Dictionary<string, DateTime>();
private static readonly object syncLock = new object();
protected void Button1_Click(object sender, EventArgs e)
{
DateTime cWhenLast;
lock (syncLock)
{
var cNowIs = DateTime.UtcNow;
if (cLastSubmits.TryGetValue(txtEmail.Text, out cWhenLast))
{
if (cWhenLast > cNowIs )
{
txtInfos.Text = "Please contact us again after 10 seconds";
return;
}
else
{
// ok I let him submit the form, but note the last date time.
cLastSubmits.Remove(txtEmail.Text);
}
}
foreach(var DelMe in cLastSubmits.Where(x => cNowIs > x.Value).ToList())
cLastSubmits.Remove(DelMe.Key);
// if reach here, note the last datetime of submit
cLastSubmits.Add(txtEmail.Text, cNowIs.AddSeconds(10));
}
// and submit the form.
txtInfos.Text = "thank you for submit the form";
}
Some notes.
If you have many pools (web garden), then this may left user to
submit at the same time up to the pool you have.
Of course if a user submit fake data this can not protect you, and thats why we can use:
The honey pot trick.
captcha
Other control thats understands the bots
Code that understand the re-submit.

Showing RDLC report in Asp.Net with report viewer

Am using VS 2010 and created a sample web application. Added a dataset and report and created an SP which just return one row. No variables included as of now. Just simple SP w/o any parameters. In the web app, added an aspx page and in that page added a report viewer control.
My Aspx page goes as below:
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"
Font-Size="8pt" InteractiveDeviceInfos="(Collection)"
WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt">
</rsweb:ReportViewer>
Code behind goes as below:
protected void Page_Load(object sender, EventArgs e)
{
ReportViewer1.Visible = true;
ReportDataSource dataSource = new ReportDataSource("DataSet1", LoadData().Tables[0]);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(dataSource);
ReportViewer1.LocalReport.Refresh();
}
private DataSet LoadData()
{
var dataset = new DataSet();
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["CString"].ConnectionString))
{
var command = new SqlCommand("[spSampleReport]")
{
Connection = con,
CommandType = CommandType.StoredProcedure
};
var adap = new SqlDataAdapter(command);
con.Open();
adap.Fill(dataset, "DataSet1");
}
return dataset;
}
In my web config, have added following in httphandlers section:
When Running, there is nothing shown on the page. Even the report viewer toolbars are not shown.
I am no ReportViewer expert but I think you need an .rdlc to tell the report viewer what to render.
ReportViewer.LocalReport.ReportPath = Server.MapPath(string.Format(#"~\Reports\Definitions\{0}.rdlc", reportName));
Make sure the DataSet name in the .rdlc is the same as the DataSource specified when you create you ReportDataSource
I found a good example to generate rdlc reports in asp.net with code example
http://www.dotnetsharepoint.com/2013/09/how-to-create-rdlc-reports-in-aspnet.html#.UjhvAsafh88

How to do ASP.net website in english and Arabic

I can do the multilingual website in asp.net when language is similar ( dir=ltr for example English, Spanish, French). I would like to know how to do the same when one language dir=ltr (English) and other language dir=rtl (Arabic).
I would appreciate if some one can link to a resource which can show how to do this step by step as along with themes one for English and other for Arabic..
I am using ASP.Net 4.0 .
I would appreciate any help in this area and if someone can provide me with a two page example that would be great.
you can try like this ...
It is easy to develop multi language supported web site using ASP.NET . Just follw that step by step.
1.Take a new web site
2.Add “App_GlobalResources” from ASP.NET folders
3.Take a *.resx file (Strings.resx)
4.Enter Name and values
5.Make different *.resx file for different languages and name like that Strings.en-US.resx (for US english), Strings.fr-FR.resx (for
French). Make as many language file you needed
6.Now time for calling and using language from web page You website Solution Explorer will look like below image...
Default.aspx file will look like that
<asp:Label ID=”lblName” runat=”server” Text=”Label”></asp:Label>
<asp:Label ID=”lblDesc” runat=”server” Text=”Label”></asp:Label>
<asp:Label ID=”lblComments” runat=”server” Text=”Label”></asp:Label>
<asp:LinkButton ID=”lnkEnglish” runat=”server” OnClick=”lnkEnglish_Click”>English</asp:LinkButton>
<asp:LinkButton ID=”lnkFrench” runat=”server” OnClick=”lnkFrench_Click”>French</asp:LinkButton>
Codes of Default.aspx.cs
private ResourceManager rm;
protected void Page_Load(object sender, EventArgs e)
{
CultureInfo ci;
if (!IsPostBack)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(“en-US”);
rm = new ResourceManager(“Resources.Strings”, Assembly.Load(“App_GlobalResources”));
ci = Thread.CurrentThread.CurrentCulture;LoadData(ci);
}
else
{
rm = new ResourceManager(“Resources.Strings”,Assembly.Load(“App_GlobalResources”));
ci = Thread.CurrentThread.CurrentCulture;LoadData(ci);
}
}
protected void lnkEnglish_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(“en-US”);
LoadData(Thread.CurrentThread.CurrentCulture);
}
protected void lnkFrench_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(“fr-FR”);
LoadData(Thread.CurrentThread.CurrentCulture);
}
public void LoadData(CultureInfo ci)
{
lblName.Text = rm.GetString(“EventName”, ci);
lblDesc.Text = rm.GetString(“EventDescription”, ci);
lblComments.Text = rm.GetString(“EventComments”,ci);
}

How to indicate create user control if user already exists

I am using Create User control with my own sql server database, I am not sure what event is there which can indicate the create user control to show its Duplicate UserName Error Message if there is an existing Username or email.
Please let me know.
thanks,
If you were using aspnet membership then CreateUser Control would have done the work itself. But from your question I guess you are not using so. In this case you will have to use logic to check for existing username. You can use Custom Validator and create a server side validation with it.
Example:
.aspx
<asp:TextBox ID="txtUserName" runat="server" MaxLength="150"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="CustomValidator1_ServerValidate"
ControlToValidate="txtUserName" Display="Dynamic"
ErrorMessage="UserName Already Exists"
ToolTip="Please select a different UserName" ValidationGroup="Register">
</asp:CustomValidator>
The code behind can have:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
SqlCommand objcmd = new SqlCommand("Select * from Login_Table where UserName='" + args.Value + "'",con);
SqlDataReader objReader;
con.Open();
objReader = objcmd.ExecuteReader();
if (objReader.HasRows)
{
args.IsValid = false;
}
else {
args.IsValid = true;
}
con.Close();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}
// your registration code
}
Remember to keep the submit button under the same Validation Group as the Custom Validator i.e. "Register" in this case. You can have the control inside ajax update panel further. Hope this helps :)

cannot modify asp login in page_load or page_init

So I have an asp:Login field on my login page.
However, I want to use a path for the create account url and the forgot password url. So I have to do it in Page_Load or maybe Page_Init. Regardless, neither option works, it simply refuses to modify the login form.
protected void Page_Load(object sender, EventArgs e)
{
string accountpath = Request.Url.AbsoluteUri + "/user/RequestAccount.aspx";
string forgotpath = Request.Url.AbsoluteUri + "/user/ForgotPassword.aspx";
lgnMain.CreateUserUrl = accountpath;
lgnMain.PasswordRecoveryUrl = forgotpath;
lgnMain.InstructionText = "test";
lgnMain.Focus();
}
protected void Page_Init(object sender, EventArgs e)
{
string accountpath = Request.Url.AbsoluteUri + "/user/RequestAccount.aspx";
string forgotpath = Request.Url.AbsoluteUri + "/user/ForgotPassword.aspx";
lgnMain.CreateUserUrl = accountpath;
lgnMain.UserName = "test";
lgnMain.InstructionText = "test";
lgnMain.PasswordRecoveryUrl = forgotpath;
}
The CreateUserUrl and the PasswordRecoveryUrl are ignored if you haven't set the CreateUserText and PasswordRecoveryText properties respectively. Since the Text properties probably don't need to be dynamic, just set them in the ASPX (although you could still set them in the code behind if required), and then the dynamic setting of the URL properties (in the Page_Load event) should work without problem.
Documentation here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login_members(v=vs.85).aspx
From the Documentation above:
If the CreateUserText property is
empty, the link to the registration
page is unavailable to the user.
If the PasswordRecoveryText property
is empty, the link to the password
recovery page is not available to the
user.
have you tried setting it in the markup?
<asp:Login id="lgnMain" runat="server"
CreateUserText="Register"
CreateUserUrl="~/user/RequestAccount.aspx"
PasswordRecoveryText = "Forgot Password"
PasswordRecoveryUrl = "~/user/ForgotPassword.aspx" >
</asp:Login>

Resources