2011-12-20 6 views
0

私はコンボボックスとその中の値のリストを持っています。 値を追加して保存すると、コンボボックスに表示されます。しかし、私はページを更新した後にのみ表示されます。データを正しくバインドしません。Combobox Databind()check

私は

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

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

あなたはコンボボックスに要素を追加するWebメソッドを使用することができ、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"> 

答えて

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

それは動作しません:( – Ish

+0

は、C#のコードをアップロードしてください –

+0

を私の編集を参照してください@Ish –

1

であるとしたときにどのアイテム使用のjqueryの、あるいはJavaScriptを追加し、uは再バインドこのwebmthodを呼んでいただき、ありがとうございますこれは通常、0123の後に呼び出されるいくつかのコントロールイベントハンドラで行われます。データは

1

あなたは、あなたのデータソースを更新した後DataBind()を呼び出す必要がありますイベントであるため、この呼び出しは更新後にのみ表示されます(更新後の最初の2回目に呼び出されます)。アップデートを実行あなたの方法に

ので、ちょうど追加DataBind()、のようなものは:

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

私は既にそれを持っています。 – Ish

+0

私はこの背後にある理由を理解していません – Ish

+0

あなたのコードに 'DataContext'の割り当てはありませんか?どのようにデータバインディングを実行するのですか? –