I am having HyperLink on my webfrom in such a way that if proper user login and clicks on the link will navigate him to corresponding page. If not i make that as a non clickable field by using this code
if(isAdmin)
{ // check for admin user
Link1.NavigateUrl = "Reports.aspx";
Link1.NavigateUr2 = "Delete.aspx";
}
else
{
Link1.NavigateUrl = "javascript:return void;";
Link1.NavigateUr2 = "javascript:return void;";
}
In the else part i would like to display an alert box saying that you are not an authorized user.
I also write another code for making that link as non selectable as follows
LinkButton1.Attributes["OnClick"] = “return false;”;
So can any one tell how can i write alert message in both the cases
Link1.NavigateUrl = "javascript:alert('hello'); return false;";
OR
Link1.Attributes["onclick"] = "alert('hello'); return false";
In your else part add:
string prompt =
"<script type=\"text/javascript\">alert('You are not an authorized user'); </script>";
ClientScript.RegisterStartupScript(typeof (Page), "alert", prompt);
Regards
Related
I am working on website that redirect the user to the same page when he log out and I am trying to hide login button and the username and password text boxes when the user is logging in and show the user name and the logout button only and vise versa,
I tried but when I click log in button it still show log in button with username and password text boxes, and it should hide them and show user name and log out button only, I used .visible = false as shown below:
if session["userName"] == NULL)
{
login_btn.visible = true;
logout_btn.visible = false;
}
login_btn.visible = false;
logout_btn.visible = true;
I solved this issue I just needed to add else in this code:
if session["userName"] == NULL)
{
login_btn.visible = true;
logout_btn.visible = false;
}
else // this solved the issue
{
login_btn.visible = false;
logout_btn.visible = true;
}
On button click event after inserting data I want to show message that data is inserted successfully.
Here is my code
if (com2.ExecuteNonQuery() >= 1)
{
Response.Write("<script LANGUAGE='JavaScript' >alert('Request Submitted Successfully!')</script>");
}
else
{
Response.Write("<script LANGUAGE='JavaScript' >alert('There is some error. Please enter again')</script>");
}
It was working fine until I put code for empty textboxs after insertion in same event soon after above code. here is code
foreach (var control in this.Controls)
{
TextBox tb = control as TextBox;
if (tb != null)
{
tb.Text = string.Empty;
}
}
Response.Redirect("Default.aspx");
now textbox empty after data insertion, data is also inserting but no popup message is showing. Where is problem
I'm not 100% sure on your logic, but instead of having the server perform the redirect, do it on the client side after the call to alert:
if (com2.ExecuteNonQuery() >= 1) {
Response.Write("<script LANGUAGE='JavaScript' >alert('Request Submitted Successfully!');window.location='Default.aspx';</script>");
} else {
Response.Write("<script LANGUAGE='JavaScript' >alert('There is some error. Please enter again');window.location='Default.aspx';</script>");
}
Your Response.Redirect("Default.aspx") is run at the server level so you'll never give the client side a chance to render the javascript and therefore you won't ever see what your Response.Write is outputting.
Why don't you have an <asp:Label> control that will show the message you're looking for.
So, instead of a javascript message you could do:
tb.Text = String.Empty; //clear the textbox
label.Text = "Success Message."; //show the message
It's been awhile since I've used WebForms but this should work if you have Viewstate enabled. You might also want to look into UpdatePanels for this.
I've read a few articles regarding getting values back from a modal popup in an ASP .NET page, but they all seem to use JavaScript to accomplish this which isn't really want I want to do if possible.
I have a web user control which has a repeater that I populate from a list into a table. Each row has a link button which has a redirect url with a value as a query string.
Then I have another web user control which is just a link button with a panel and the repeater web user control that once clicked shows the actual modal popup.
Is it possible to get a value from the web user control once the link button on the repeater is clicked without having to redirect to the same page? I basically want to click on the link, show the modal and once closed, want to access the value.
I'm populating the repeater with the links as follows:
string linkUrl = "";
string action = "";
if (Request.QueryString["action"] != null)
{
action = Request.QueryString["action"];
switch (action)
{
case "SetupCompany":
{
linkUrl = "<a href=CreateCompanies.aspx?companyId=";
break;
}
case "ViewCompany":
{
linkUrl = "<a href=ViewCompany.aspx?companyId=";
break;
}
}
}
CompaniesBusinessManager mgr = new CompaniesBusinessManager();
var companies = mgr.GetCompanies(txtCompanyName.Text, txtRegistrationNumber.Text);
if (linkUrl != "")
{
foreach (var c in companies)
{
c.Name = linkUrl + c.Id.ToString() + "&action=" + action + ">" + c.Name + "</a>";
}
}
rptrCompanies.DataSource = companies;
rptrCompanies.DataBind();
if you don't want the page to be redirected, you will need to use javascript.
There is now way you can pass values from different controls without going back to the server.
In case you keep it without the javascript:
I think you need to pass values from one user control to another. I used to accomplish this by firing reachable events between them.
For example:
in your parent view:
<uc:YourUserControl runat="server" ID="UserControl_Test"
OnYourCustomAction="UserControl_YourUserControl_YourCustomAction" />
In your user control:
public event EventHandler<CustomActionEventArgs> YourCustomAction;
also in the same user control create a public trigger method to be access from others usercontrols
public void TriggerCustomActoinEvent(CustomActionEventArgs EventArgs)
{
if (this.YourCustomAction!= null)
{
this.YourCustomAction(this, EventArgs);
}
}
Hope this help, in on my way to home this was from my mind!
Without a page postback or JavaScript it not really possible. If you are using modal popups you are already using JS, so why not just get the value in JS? You could setup an event handler for all repeater buttons and if they are loaded via ajax use something like this to attach the event handler:
$(document).on('click', '.repeaterButton', function(e) {
var valueOfBtnClicked = $(this).val();
// Do something else
});
I have 3 text box's and a submit button when i enter the values and submit then values are entered in database .
but when i enter the same values and enter then those values are also entered.......This should not happen...I need a popup window showing there are duplicate values that you have entered.Please give the code for aspx and aspx.cs and data base. Please explain in breif
try
{
int result = Timesheet_BI.InsertCompanyInformation(txtCompanyName.Text, txtAddress.Text);
if (result == -1)
{
txtCompanyName.Focus();
txtCompanyName.Attributes["onfocus"] = "this.select();";
string jv = "<script>alert('Error Details: Duplicate Entry of Company Name ');</script>";
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", jv, false);
return;
//Page.ClientScript.RegisterStartupScript(typeof(string), "My Script", "Duplicate Enteries");
}
}
catch (Exception ex)
{
throw ex;
}
Why don;t you use a simple java script to check this.
or
use JQuery /getElementById to get the controls and do a simple comparison.
If you have a bunch of textboxes to compage, give something similar to all the textbox
Eg Give a common prefix to all textbox controls and use JQuery to get all the textboxes starting with that prefix and do a regular comparison.
http://api.jquery.com/attribute-contains-selector/#
Eg;
$(:contains(<prefix>,textbox').each
(
function()
{
// do the comparison
}
)
if (TextBox1.Text == row["UserName"].ToString()
&& TextBox2.Text == row["Password"].ToString())
{
//here i have to reload the page
Button1.Attributes.Add("onclick", "alert('Login SucessFull')");
break;
}
Maybe this is more what you want? It will show an alert and then redirect you to another page.
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LoggedInScript", "alert('Login SucessFull'); window.location = 'MyLoggedInPage.aspx';", true);
When you say you need to "reload the page" do you mean redirect to another page becuase the user is now logged in? If you you use:
Response.Redirect("MyLoggedInPage.aspx")