c# - 组合框数据绑定(bind)()检查

标签 c# javascript asp.net sql

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

我已将 DataBind() 放入

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

但以上并没有帮助。 如何检查一切是否正确绑定(bind)。

请帮忙。 谢谢

 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">

最佳答案

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();
    }
    }

关于c# - 组合框数据绑定(bind)()检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8580323/

相关文章:

c# - Select 查询中的 if 条件还是后面代码中的 If 条件?什么是性能明智的好?

c# - 升级到 MVC3。找不到 ashx 资源。路由问题?

c# - 在 C# 中手动信任 SSL 证书

c# - 将 [][] 形式的数组转换为 [,] 的最聪明方法?

Javascript - 循环嵌套循环

javascript - 从 ajax jQuery 中的数据变量获取属性

c# - Mvc 5 属性路由

c# - 命名空间 system.windows 中不存在“Forms”

javascript - 按钮在 Firefox 中保持在顶部,但在 Chrome 中不是

c# - 从 XmlDataSource 填充 DropDownList