我有一个组合框和其中的值列表。 如果我添加一个值并保存它,它应该出现在组合框中。但只有刷新页面后才会出现。它没有正确绑定(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/