【问题标题】:select tag send null value when submit using asp.net使用asp.net提交时选择标签发送空值
【发布时间】:2018-04-27 22:08:05
【问题描述】:

我正在使用以下代码使用 asp.net 和 c# 从数据库中填充选择标记。 它工作正常并正确显示数据,但是当我将表单提交到另一个页面时,我得到所选数据的空值 这是我在 Collages.aspx 页面中的代码

<select class="form-control" id="uni_name" name="uni_name" runat="server">

</select>

这是填充选择标签的 Collages.aspx.cs 代码

 protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection conn = new DB_Connection().getConnection();
    SqlDataAdapter uni_adapter = new SqlDataAdapter("select uni_id,uni_name from universities", conn);
    DataSet ds = new DataSet();
    uni_adapter.Fill(ds);

    uni_name.DataSource = ds;
    uni_name.DataTextField = "uni_name";
    uni_name.DataValueField = "uni_id";
    uni_name.DataBind();
}

这是我用来获取选定值的代码

 Response.Write(Request.Form["uni_name"]);

你能帮忙解决这个问题吗?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    请在 isPostback 中写下你的代码

     protected void Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
    {
        SqlConnection conn = new DB_Connection().getConnection();
        SqlDataAdapter uni_adapter = new SqlDataAdapter("select uni_id,uni_name from universities", conn);
        DataSet ds = new DataSet();
        uni_adapter.Fill(ds);
    
        uni_name.DataSource = ds;
        uni_name.DataTextField = "uni_name";
        uni_name.DataValueField = "uni_id";
        uni_name.DataBind();
    }
    }
    

    【讨论】:

    • 没有工作。问题不在于显示数据问题当我提交表单时它为此选择标签发送空值
    【解决方案2】:

    问题在于 ASP.NET 页面创建过程中的事件顺序。回发数据在OnInitOnLoad 事件之间进行处理。由于您在OnLoad 事件之前不填写您的选择,因此在处理回发中的视图数据时它确实存在。

    将您的代码从Page_Load 移动到Page_Intit,并且在处理视图数据时将存在带有其数据的选择。

    【讨论】:

    • 不起作用,我将代码放在 onInit 中,但相同。我的问题不在于在选择标签内显示数据问题是在单击提交按钮后它发送空值
    猜你喜欢
    • 2012-12-02
    • 2021-12-30
    • 1970-01-01
    • 2021-02-23
    • 2019-05-22
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 2011-04-14
    相关资源
    最近更新 更多