ASP.Net代码优化 [英] ASP.Net code optimization

查看:91
本文介绍了ASP.Net代码优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎每一段代码,我只需要使用正确的IF_ELSE条件进行优化并安排正确的流程。



我有一个webform(UserProfile) .aspx),其中包含FormView(用于显示用户配置文件)。



在FormView ItemTemplate中,我使用简单的HTML和ASP.Net标签使用Label的文本显示Info property:

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 :

Eval("col_name");



在EditItemTemplate中,我有相同的表,但有文本框而不是标签。



我有一个按钮编辑,在其Click事件上,我正在更改FormViewMode


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

FormView1.changeMode(FormViewMode.Edit); //WORKING





我有另一个按钮取消,在其Click事件上,我正在更改FormViewMode



I have another button "Cancel", on its Click event, I'm changing FormViewMode

FormView1.changeMode(FormViewMode.ReadOnly); //NOT WORKING



显示用户配置文件,我从查询字符串传递用户ID

因此有效的URL如下所示:


To display user profile, I'm passing user-id from querystring
So the valid URL looks like :

/UserProfile.aspx?user=121



我想要达到的目的是:


What I want to achieve is :

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我想在FormView周围添加一个UpdatePanel一旦我完成上述任务,就可以保持一致的外观。



我知道这个问题很难理解所以请随意问一下细节。我将根据需要提供代码。



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.

推荐答案

要验证登录用户,请使用会话并检查存储会话变量的标签是否为空。 />
没有真正得到你的问题,

如果用户登录你试图打开一些控件吗?如果是,为什么不将控件设置为打开,然后确保用户在被查看之前登录。

这就是我所说的

如果我有一个页面说login.aspx,我在webform1.aspx中有一些控件,我只需要显示重新登录的用户,我会在登录按钮的login_Click事件中使用这些代码行,假设用户的登录信息存储在mysql数据库;

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读取;

con 。打开();

read = lol.ExecuteReader();

while(read.Read())

{

unch.Text = read.GetString(名称);

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 =登录信息不正确;

loginerr.Visible = true;

}

con.Close();

}

catch(例外情况)

{

loginerr.Text = ex.Message;

loginerr.Visible = true;

}

}



逻辑很简单,你必须从中选择用户的用户名和密码数据库通过要求程序选择用户名和密码,其中用户名和密码等于用户输入的用户名和密码d,将其传递给标签并进行比较,如果同样的话,应该将人的姓名传递给会话变量并显示会话变量在下一页。否则,应出现某种形式的协调错误信息。

Lemme知道你是否得到我所说的
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


这篇关于ASP.Net代码优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆