【问题标题】:Combobox Databind() check组合框 Databind() 检查
【发布时间】:2011-12-20 18:42:06
【问题描述】:

我有一个组合框和一个值列表。 如果我添加一个值并保存它,它应该出现在组合框中。但它只在我刷新页面后出现。它没有正确绑定数据。

我已将 DataBind() 放入

 if (!Page.IsPostBack)
            {
DataBind() ;
}

但以上没有帮助。 如何检查所有内容是否正确绑定。

请帮忙。 谢谢

 protected void Page_Load(object sender, EventArgs e)
        {

 DataBind();
            if (!Page.IsPostBack)
            {
}
}



protected void btn_save_click(object sender, EventArgs e)

            {
    SqlCommand command_update = new SqlCommand("Update", connection_save1); 
                        command_update.CommandType = System.Data.CommandType.StoredProcedure;


                        command_update.Parameters.Add(new SqlParameter("@ViewId", Int32.Parse(Id.Value)));
    SqlParameter Returns = new SqlParameter("@ReturnCode", SqlDbType.Char);
                        Returns.Size = 1;
                        Returns.Direction = ParameterDirection.Output;
                        command_insert.Parameters.Add(Returns);
     bSuccess = command_insert.Parameters["@ReturnCode"].Value.ToString();
    if (bSuccess == "1")
                        {
                            //Response.Write("Insert successful");
                            dd_group.DataBind();
                            dd_group.SelectedValue = command_insert.Parameters["@ReturnCode"].Value.ToString().Trim();
    }
    }

这里是html

<asp:DropDownList ID="dd_group" DataSourceID="sp" DataTextField="maintitle"
                        DataValueField="Id" runat="server" AutoPostBack="True" 
                        OnSelectedIndexChanged="group_SelectedIndexChanged1" Height="24px" 
                        Width="50%">
                    </asp:DropDownList>
 <asp:SqlDataSource ID="sp" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="GetIds" runat="server" SelectCommandType="StoredProcedure">

【问题讨论】:

    标签: c# javascript asp.net sql


    【解决方案1】:
    protected void Pre_Render(object sender, EventArgs e)
            {
    
     DataBind();
    
    }
    
    
    
    protected void btn_save_click(object sender, EventArgs e)
    
                {
        SqlCommand command_update = new SqlCommand("Update", connection_save1); 
                            command_update.CommandType = System.Data.CommandType.StoredProcedure;
    
    
                            command_update.Parameters.Add(new SqlParameter("@ViewId", Int32.Parse(Id.Value)));
        SqlParameter Returns = new SqlParameter("@ReturnCode", SqlDbType.Char);
                            Returns.Size = 1;
                            Returns.Direction = ParameterDirection.Output;
                            command_insert.Parameters.Add(Returns);
         bSuccess = command_insert.Parameters["@ReturnCode"].Value.ToString();
        if (bSuccess == "1")
                            {
                                //Response.Write("Insert successful");
                                dd_group.DataBind();
                                dd_group.SelectedValue = command_insert.Parameters["@ReturnCode"].Value.ToString().Trim();
        }
        }
    

    【讨论】:

    • 在该特定控件上是否有偶然需要或期望数据源属性..?如果是这样,请在此处添加您的 DataBind() 或 BindData()
    • 我尝试将 Databind() 放在您描述的确切位置。但没有运气:(
    • 我有一个更好的解决方案:BindGroupId.DataSource = 不管; BindGroupId.DataTextField = "列名"; BindGroupId.DataValueField = "值"; BindGroupId.DataBind();
    • @Ish 我有更好的解决方案:DataValueField 和 DataTextField 应该用 aspx 代码而不是 CS 代码编写......
    【解决方案2】:

    您可以使用 webmethod 将元素添加到组合框,当您添加任何项目时使用 jquery 甚至 javascript 并调用此 webmthod 来重新绑定数据

    【讨论】:

      【解决方案3】:

      您必须在更新数据源后调用DataBind():这通常在某些控制事件处理程序中完成,该处理程序在Page_Load() 事件之后调用,因此此调用仅在您刷新后可见(然后调用它第二次,更新后的第一次)。

      因此,只需将 DataBind() 添加到执行更新的方法中,例如:

      mycontrol.DataSource = newvariable;
      mycontrol.DataBind();
      

      【讨论】:

      • 我没有理解这背后的原因
      • 我在您的代码中没有看到任何DataContext 分配?如何执行数据绑定?
      猜你喜欢
      • 2014-07-27
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 2021-11-19
      相关资源
      最近更新 更多