如何根据数据库显示和隐藏提交按钮? [英] How to show and Hide Submit button depending on database?

查看:80
本文介绍了如何根据数据库显示和隐藏提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#在Asp.net中有一个Web表单。我有一个代码,用于检查表中是否存在记录,具体取决于年份。如果该记录存​​在隐藏的提交按钮,如果它不存在,则显示提交按钮。该代码效果很好。现在我使用名为Submit的列更新了该表。在该列中,它将是True或False。如何根据年份检查记录是否存在以及提交以显示或隐藏提交按钮?



示例:



1. James Hammer 2015 False

2. James Black 2015 True



如果提交=假,显示提交按钮

如果提交=真,则隐藏提交按钮



这是我的代码:



I have a web form in Asp.net using C#. I have a code that checks to see if a record exist in a table depending on the year. If that record exist the Submit button hidden and if it does not exist the Submit button is shown. That code worked great. Now I have updated that table with a column named Submit. In that column it will be True or False. How can I get my code to check to see if the record exist depending on the Year and the Submit to show or hide the Submit button?

Example:

1. James Hammer 2015 False
2. James Black 2015 True

If Submit = False, Show Submit Button
If Submit = True, Hide Submit Button

Here is my code:

 SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                    con.Open();

const string cmdStr = "Select count(*) from Table44 where User_ID= @User_ID AND YEAR = 2015 AND SUBMIT = False";
                    SqlCommand userExist = new SqlCommand(cmdStr, con7);
                    userExist.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);
                    userExist.Parameters.AddWithValue("@YEAR", TextBoxDATE2.Text);
                    userExist.Parameters.AddWithValue("@SUBMIT", TextBoxSUB.Text);
                    int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
                    if (temp == 0)
                    {
                        ButtonSubmit.Visible = true;
                        
                    }
                    else if (temp == 1)
                    {
                        ButtonSubmit.Visible = false;
                        
                    }

推荐答案

请输入您的连接字符串: -

Please put your connection string :-
SqlConnection con7 = new SqlConnection();
con7.ConnectionString = "Please put your ConnectionString here";
const string cmdStr = "Select count(*) from Table44 where User_ID= @User_ID AND YEAR = 2015 AND SUBMIT = @SUBMIT";
SqlCommand userExist = new SqlCommand(cmdStr, con7);
userExist.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);
userExist.Parameters.AddWithValue("@YEAR", TextBoxDATE2.Text);
userExist.Parameters.AddWithValue("@SUBMIT", TextBoxSUB.Text);
if (con7.State == ConnectionState.Open)	{ con7.Close(); }

con7.Open();
object ans = userExist.ExecuteScalar();
con7.Close();
int temp = Convert.ToInt32(ans.ToString());
if (temp == 0)
{
	ButtonSubmit.Visible = true;
}
else if (temp == 1)
{
	ButtonSubmit.Visible = false;
}






在我这边,代码工作正常。



Hi,

At my side below code is working fine.

string sql = "select query";
SqlConnection con = new SqlConnection("data connection");
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();

if(count != 0)
{
//do this string
}
else
{

//do this one
}





请尝试让我知道。



提前谢谢。



Please try and let me know.

Thanks in advance.


我是这样做的。感谢你的帮助。



I did it this way. Thanks for all your help.

SqlConnection con7 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                    con7.Open();

                    SqlCommand level = new SqlCommand("Select User_ID, SUBMITTED, FINYR from Table99 where User_ID = @User_ID AND FINYEAR = 2015", con7);
                    level.Parameters.Add(new SqlParameter("User_ID", TextBoxUser_ID.Text));
                    level.Parameters.Add(new SqlParameter("SUBMITTED", TextBoxSUB.Text));

                    SqlDataReader reader3 = level.ExecuteReader();
                    DataTable dt1 = new DataTable();
                    dt1.Load(reader3);

                    foreach (DataRow dr1 in dt1.Rows)
                    {
                        string returnedLevel = (dr1[1].ToString());
                        int inst_id = Convert.ToInt32(dr1[0].ToString());
                        Session["inst_id"] = inst_id;


                        if (returnedLevel == "False")
                        {
                            ButtonSubmit.Visible = true;
                            //ButtonPrint.Visible = false;
                        }
                        else if (returnedLevel == "True")
                        {
                            ButtonSubmit.Visible = false;
                            //ButtonPrint.Visible = true;
                        }
                        con7.Close();
                        reader3.Close();


这篇关于如何根据数据库显示和隐藏提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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