C#失败获取用户输入。 [英] C# fail get user input.

查看:83
本文介绍了C#失败获取用户输入。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的aspx

my aspx

<tr>
            <td>input</td>
            <td>
                <input type="text" runat="server" id="input" name="input" value="" style="width: 250px" />
            </td>
            <td>
                <asp:Button ID="Button1"  style="text-align: right;" align="right"  runat="server" Text="Submit" BorderStyle="Solid" OnClick="btnSubmit_Click" />
           </td>
</tr>





我的c#



my c#

protected string r_input = string.Empty;



我的页面加载


my page load

protected void Page_Load(object sender, EventArgs e)
  {
     if (Request.Params["input"] != null) r_input = Request.Params["input"];
  }



我的提交按钮


my submit button

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string connectionStringDB = "server=PC00093-LimKY-;database=master;uid=sa;password=abc135;";
    using (SqlConnection connection = new SqlConnection(connectionStringDB))
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO countrydetail (ID,Name) VALUES (@id,@Name)");
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;
        cmd.Parameters.AddWithValue("@ID", 1);
        cmd.Parameters.AddWithValue("@Name", r_input);
        connection.Open();
        cmd.ExecuteNonQuery();
    }





我在文本框中输入内容后点击提交按钮。我查看我的数据库,只有ID成功插入数据库,名称列为空



我尝试过:



i多次尝试,仍然无法工作....需要帮助。



After i input something into the textbox i click the submit button.I check my db,only ID successfully insert into database,Name Column is empty

What I have tried:

i tried many times,still cannot work....need help.

推荐答案

那是因为你的受保护字符串r_input没有设置。 />
首先需要了解的是页面生命周期 - 请参阅: MSDN - ASP.Net页面生命周期概述 [ ^ ]



使用调试器&在btnSubmit_Click事件中设置一个断点。

检查r_input的值是什么(空字符串)&然后查看输入的值,有点令人困惑地命名为输入 - 它将具有用户输入的任何值。



亲切的问候
That would be because your protected string r_input is not set.
First thing you need to understand is the Page Lifecycle - refer: MSDN - ASP.Net Page Life Cycle Overview[^]

Use your debugger & put a break point in btnSubmit_Click event.
Check what the value of r_input is (an empty string) & then look at the value of your input, somewhat confusingly named "input" - it will have whatever value the user has entered.

Kind Regards


这篇关于C#失败获取用户输入。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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