Click here to Skip to main content
15,068,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have almost every piece of code, I just need it to be optimized using correct IF_ELSE conditions and arranging the right flow.

I have a webform (UserProfile.aspx), which contains FormView (to display user profile).

In FormView ItemTemplate, I have put simple HTML and ASP.Net Labels to display Info using Label's Text property :
C#
Eval("col_name");

In EditItemTemplate, I have same table but with textboxes instead of Labels.

I have a button "Edit", on its Click event, I'm changing FormViewMode
C#
FormView1.changeMode(FormViewMode.Edit); //WORKING


I have another button "Cancel", on its Click event, I'm changing FormViewMode
C#
FormView1.changeMode(FormViewMode.ReadOnly); //NOT WORKING

To display user profile, I'm passing user-id from querystring
So the valid URL looks like :
C#
/UserProfile.aspx?user=121

What I want to achieve is :
C#
If (user_IS_Logged_In)
{
//Some basic tasks like setting welcome message label with username/email etc.
}

If (querystring_is_EMPTY OR querystring_field_is_incorrect OR user_IS_NOT_Logged_IN)
{
//Remove the Edit and Cancel buttons
}

If (querystring_field_is_exist)
{
//initialize user_id (this will be passed to ShowUserDetails(user_id))
}

If (user_IS_Logged_In AND querystring_is_exist)
{
    If (querystringvalue != logged_in_user_id)
    {
     //Again Remove the Edit and Cancel buttons
    }
}


I'm thinking to add an UpdatePanel around the FormView Once I achieve above tasks, for consistent look.

I know the problem is quite difficult to understand so feel free to ask details. I will provide the code as needed.
Posted
Updated 2-Feb-15 22:58pm
v2
Comments
CHill60 3-Feb-15 5:21am
   
The problem isn't so much difficult to understand as you haven't actually said what it is. Optimisation isn't really relevant.
Are you expecting us to convert that pseudo-code of what you want to achieve, into real code?

1 solution

To verify a logged in user, use session and check if the label where the session variable is stored is empty.
didn't really get your question,
are you trying to open some controls if a user is logged in? if yes, why not set the controls to be open, then ensure the user is logged in before its being viewed.
this is what i am saying
if i have a page say login.aspx and i have some controls in webform1.aspx that i only need to show users that re logged in, i would use these lines of code in the login_Click event of the login button assuming the user's login information is stored in a mysql database;
protected void login_Click(object sender, EventArgs e)
{
try
{
MySqlConnection con = new MySqlConnection(connection);
string query1 = "select Name, Username, Password from usoft.fedadmin where Username='"+untxt.Text+"' && Password='"+pwtxt.Text+"'";
MySqlCommand lol = new MySqlCommand(query1, con);
MySqlDataReader read;
con.Open();
read = lol.ExecuteReader();
while (read.Read())
{
unch.Text = read.GetString("Name");
pwch.Text = read.GetString("Password");
adminName.Text = read.GetString("Username");
}
if ((untxt.Text.Equals(unch.Text)) && (pwtxt.Text.Equals(pwch.Text)))
{
Session["name"] = unch.Text;
Response.Redirect("webform1.aspx");
loginerr.Visible = false;
}
else
{
loginerr.Text = "Incorrect Login Information";
loginerr.Visible = true;
}
con.Close();
}
catch (Exception ex)
{
loginerr.Text = ex.Message;
loginerr.Visible = true;
}
}

the logic is simple, u have to select the user's username and password from the database by asking the program to select d username and password where the username and password equals the username n password d user typed, pass it into label and compare, if its same thing, should pass the person's name into a session variable and display the session variable in the next page. else, some form of coordinated error message should appear.
Lemme know if u get what i'm saying
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900