如果存在记录,则启用和禁用提交。 [英] Enable and Disable Submit if record exist.

查看:68
本文介绍了如果存在记录,则启用和禁用提交。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我有一个Web表单,用户可以根据该表单提交信息。当用户单击该提交按钮时,数据将被提交到数据库中,并且用户将被重定向回其个人资料的主页。然后,用户可以返回他们刚刚离开的表单页面,并查看他们刚刚提交的数据。我想要做的是如果记录存在则禁用提交按钮。现在我将我的代码设置为文本框。文本框默认为零。提交按钮已启用。单击提交按钮并将数据保存到数据库并且用户返回到该表单时,应禁用提交按钮。它由于某种原因不起作用。我怎样才能让它工作?



这是启用和禁用该文本框的提交按钮的代码:



Hello to all. I have a web form that a user can submit information based on that form. When the user clicks that submit button the data is submitted into the database and the user is redirected back to their home page of their profile. The user can then go back into the form page they just left and see the data that they just submitted. What I am trying to do is to disable the submit button if the record exist. Right now I am setting my code to a textbox. The textbox has a zero in it as a default. The submit button is enable. When the submit button is clicked and the data is saved to the database and the user comes back to that form the submit button should be disabled. It is not working for some reason. How can I get this to work?

Here is the code to enable and disable the submit button for that textbox:

if (TextBoxFTUG.Text.Trim().Length > 0)
        {
            ButtonSubmit.Enabled = true;
        }

        else if (TextBoxFTUG.Text.Trim().Length > 1)
        {
            ButtonSubmit.Enabled = false;
        }

推荐答案

对于一个好的结构,你应该总是在最后添加一个else语句,至少有一个调试通知,否则执行可能会在没有你注意的情况下失败。



你也改变了逻辑,你应该改变代码如下所示,否则你永远不会进入第二if语句。

For a good structure you should always add an else statement at the end with at least a debug notification, otherwise the execution can fall through without you noticing.

You also have reversed the logic, you should change the code as shown below, otherwise you will never go into the second if statement.
if (TextBoxFTUG.Text.Trim().Length > 1)
{
    ButtonSubmit.Enabled = true;
}
else if (TextBoxFTUG.Text.Trim().Length > 0)
{
    ButtonSubmit.Enabled = false;
}
else
{
   throw new Exception("Some error");
}





但你也说你在文本框中默认为0并将其更改为1 。

这意味着 TextBoxFTUG.Text.Trim()的长度将始终为1.

试试这个代替。





But you also say that you have a 0 as default in the text box and the change it to a 1.
That means that the length of TextBoxFTUG.Text.Trim() will always be 1.
Try this code instead.

ButtonSubmit.Enabled = (int.Parse(TextBoxFTUG.Text.Trim()) == 0);


我已解决了我的问题!!!!!这就是我做的。当用户输入他们的数据并单击提交按钮时,代码会将用户重定向回他们的主页。用户可以单击该表单的链接,查看刚刚提交的数据。我有一个代码,用这些数据填充文本框。她是代码:



I have solved my issue!!!!! Here is what I did. When the user enters their data and click the submit button the code redirects the user back their home page. There the user can click the link of that same form and see their data that was just submitted. I have a code in place that populates the textboxes with that data. Hers is that code:

if (TextBoxuser_ID.Text.Trim().Length > 0)
        {
            SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con3.Open();

            SqlCommand scmd3 = new SqlCommand("Select FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from Table55 where user_ID = '" + TextBoxuser_ID.Text + "'", con3);
            SqlDataReader dr3 = scmd3.ExecuteReader();
            if (dr3.Read())
            {
                TextBoxFTUG.Text = dr3["FT_UNDERGR"].ToString();
                TextBoxFTG.Text = dr3["FT_GRAD"].ToString();
                TextBoxTHUGDR.Text = dr3["FTE_UNDERG"].ToString();
                TextBoxTHGDR.Text = dr3["FTE_GRAD"].ToString();
                TextBoxNCCDR.Text = dr3["NON_CREDIT"].ToString();
                TextBoxTCNC.Text = dr3["TOTAL_FTE"].ToString();
                TextBoxTNFUG.Text = dr3["FCFTUHC"].ToString();
                TextBoxTNFG.Text = dr3["FCFTPBHC"].ToString();
                TextBoxTNCPUG.Text = dr3["FCPTUHC"].ToString();
                TextBoxTNCPG.Text = dr3["FCPTPBHC"].ToString();
                TextBoxTNNCC.Text = dr3["NCHC"].ToString();
                
            }
            con3.Close();
            dr3.Close();
        }





我使用了禁用提交按钮的代码,只是将其添加到填充的代码中。以下是禁用代码的代码:





I took my code that disables the submit button and just added it to the populated code. Here is the code with the disable code:

if (TextBoxINST_ID.Text.Trim().Length > 0)
        {
            SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con3.Open();

            SqlCommand scmd3 = new SqlCommand("Select FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from TableFTE2014 where INST_ID = '" + TextBoxINST_ID.Text + "'", con3);
            SqlDataReader dr3 = scmd3.ExecuteReader();
            if (dr3.Read())
            {
                TextBoxFTUG.Text = dr3["FT_UNDERGR"].ToString();
                TextBoxFTG.Text = dr3["FT_GRAD"].ToString();
                TextBoxTHUGDR.Text = dr3["FTE_UNDERG"].ToString();
                TextBoxTHGDR.Text = dr3["FTE_GRAD"].ToString();
                TextBoxNCCDR.Text = dr3["NON_CREDIT"].ToString();
                TextBoxTCNC.Text = dr3["TOTAL_FTE"].ToString();
                TextBoxTNFUG.Text = dr3["FCFTUHC"].ToString();
                TextBoxTNFG.Text = dr3["FCFTPBHC"].ToString();
                TextBoxTNCPUG.Text = dr3["FCPTUHC"].ToString();
                TextBoxTNCPG.Text = dr3["FCPTPBHC"].ToString();
                TextBoxTNNCC.Text = dr3["NCHC"].ToString();
                ButtonSubmit.Enabled = false;
            }
            con3.Close();
            dr3.Close();
        }





现在禁用代码可以使用填充的代码。因此,当用户首次登录并转到表单时,将启用提交按钮。当用户提交数据并重定向回其主屏幕并再次单击表单的链接时,由于提交的填充数据,提交按钮被禁用。



Now the disable code works with the populated code. So when a user log in and goes to the form for the first time the submit button will be enabled. When the user submits the data and is redirected back to their home screen and clicks on the link for the form again the submit button is disabled because of the populated data that was submitted.


这篇关于如果存在记录,则启用和禁用提交。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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