By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,543 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,543 IT Pros & Developers. It's quick & easy.

Checkboxes in FormView bound to ObjectDataSource

P: n/a
Pao
I have a formview bounded to an objectdatasource.
All ok but I have some trouble with a checkbox: when I am in update
mode and I update the record, this field is never updated (no errors
thrown).
The field bounded in the database (MySql) is a string field
(ENUM('True','False') default 'False'.

The checkbox is correctly bounded in the itemtemplate and in the
edititemtemplate.

Here is the formview:
Expand|Select|Wrap|Line Numbers
  1. <asp:FormView ID="fwAgente" runat="server"
  2. AllowPaging="True"
  3. DataKeyNames="ID"
  4. DataSourceID="AgenteDataSource">
  5. <ItemTemplate>
  6. <table>
  7. <tr><td align="right"><b>ID:</b></td>
  8. <td><%# Eval("ID") %></td></tr>
  9.  
  10. <tr><td align="left" style="height:
  11. 21px"><b>Nome:</b></td><td style="width: 5px; height: 21px"><%#
  12. Eval("Nome") %></td></tr>
  13. <tr><td align="left"><b>Cognome:</b></td>
  14. <td style="width: 5px"><%# Eval("Cognome") %></td></tr>
  15. <tr><td align="left"><b>Telefono:</b></td>
  16. <td style="width: 5px"><%# Eval("Telefono") %></td></tr>
  17. <tr>
  18. <td style="width:7%">Annullato: </td>
  19. <td style="width:6%">
  20. <asp:CheckBox ID="chkAnnAg"
  21. runat="server" Enabled="false"
  22. Checked ='<%#
  23. Convert.ToBoolean(Eval("annullato")) %>'  />
  24. </td>
  25. </tr>
  26. <tr>
  27. <td colspan="2">
  28. <asp:LinkButton ID="EditButton"
  29. Text="Edit"
  30. CommandName="Edit"
  31. RunAt="server"/>
  32. &nbsp;
  33. <asp:LinkButton ID="NewButton"
  34. Text="New"
  35. CommandName="New"
  36. RunAt="server"/>
  37. &nbsp;
  38. <asp:LinkButton ID="DeleteButton"
  39. Text="Delete"
  40. CommandName="Delete"
  41. RunAt="server"/>
  42. </td>
  43. </tr>
  44. </table>
  45. </ItemTemplate>
  46.  
  47. <EditItemTemplate>
  48. <table>
  49. <tr><td align="right"><b>ID:</b></td>
  50. <td><asp:TextBox ID="EditIDTextBox"
  51. Enabled="false"
  52. Text='<%# Bind("ID") %>'
  53.  
  54. RunAt="Server"
  55. /></td></tr>
  56.  
  57. <tr><td align="right"><b>Nome:</b></td>
  58. <td><asp:TextBox ID="EditNomeTextBox"
  59. Text='<%# Bind("Nome")
  60. %>'
  61. RunAt="Server"
  62. /></td></tr>
  63. <tr><td align="right"><b>Cognome:</b></td>
  64. <td><asp:TextBox ID="EditCognomeTextBox"
  65. Text='<%#
  66. Bind("Cognome") %>'
  67. RunAt="Server"
  68. /></td></tr>
  69. <tr><td align="right"><b>Telefono:</b></td>
  70. <td><asp:TextBox ID="EditTelefonoTextBox"
  71.  
  72. Text='<%#
  73. Bind("Telefono") %>'
  74. RunAt="Server"
  75. /></td></tr>
  76.  
  77. <tr><td align="right"><b>Annullato:</b></td>
  78. <td style="width:6%">
  79. <asp:CheckBox ID="chkAnnAg"
  80. runat="server" Enabled="true"
  81. Checked ='<%#
  82. Convert.ToBoolean(Eval("annullato")) %>'  />
  83. </td>
  84. </tr>
  85.  
  86. <tr>
  87. <td colspan="2">
  88. <asp:LinkButton ID="UpdateButton"
  89. Text="Update"
  90. CommandName="Update"
  91. RunAt="server"/>
  92. &nbsp;
  93. <asp:LinkButton ID="CancelUpdateButton"
  94. Text="Cancel"
  95. CommandName="Cancel"
  96. RunAt="server"/>
  97. </td>
  98. </tr>
  99. </table>
  100. </EditItemTemplate>
  101. <InsertItemTemplate>
  102. <table>
  103. <tr><td align="right"><b>Login:</b></td>
  104. <td><asp:TextBox ID="InsertLoginTextBox"
  105. Text='<%# Bind("Login")
  106. %>'
  107. RunAt="Server"
  108. /></td></tr>
  109.  
  110. <tr><td align="right"><b>Nome:</b></td>
  111. <td><asp:TextBox ID="InsertNomeTextBox"
  112. Text='<%# Bind("Nome")
  113. %>'
  114. RunAt="Server"
  115. /></td></tr>
  116.  
  117. <tr><td align="right"><b>Cognome:</b></td>
  118. <td><asp:TextBox
  119. ID="InsertCognomeTextBox"
  120. Text='<%#
  121. Bind("Cognome") %>'
  122. RunAt="Server"
  123. /></td></tr>
  124.  
  125. <tr><td align="right"><b>Telefono:</b></td>
  126. <td><asp:TextBox
  127. ID="InsertTelefonoTextBox"
  128. Text='<%#
  129. Bind("Telefono") %>'
  130. RunAt="Server"
  131. /></td></tr>
  132. <tr>
  133. <td colspan="2">
  134. <asp:LinkButton ID="InsertButton"
  135. Text="Insert"
  136. CommandName="Insert"
  137. RunAt="server"/>
  138. &nbsp;
  139. <asp:LinkButton ID="CancelInsertButton"
  140. Text="Cancel"
  141. CommandName="Cancel"
  142. RunAt="server"/>
  143. </td>
  144. </tr>
  145. </table>
  146. </InsertItemTemplate>
  147. </asp:FormView>
  148.  
and here is the objectdatasource:

Expand|Select|Wrap|Line Numbers
  1. <asp:ObjectDataSource ID="AgenteDataSource"
  2. runat="server"
  3. DataObjectTypeName="Reminder.Agente"
  4. TypeName="Reminder.Agente"
  5. DeleteMethod="Delete"
  6. InsertMethod="Insert"
  7. SelectMethod="GetDataTable"
  8. UpdateMethod="Update"
  9. OldValuesParameterFormatString="original_{0}"
  10. OnUpdated="AgenteDataSource_Updated"
  11. OnObjectCreated = "genteDataSource_ObjectCreated"
  12.  
  13. OnInserted="AgenteDataSource_Updated"
  14. OnDeleted="AgenteDataSource_Updated"
  15.         
  16.                 >
  17.  
  18. <SelectParameters>
  19. <asp:Parameter Name="vId" Type="String" />
  20. <asp:Parameter Name="vAnn" Type="String" />
  21. </SelectParameters>
  22. </asp:ObjectDataSource>
  23.  
and here is the declaration of the object

Expand|Select|Wrap|Line Numbers
  1. public class Agente
  2. {
  3. private MySqlConnection cnn = new MySqlConnection();
  4. protected int _id;
  5. protected string _login;
  6. protected string _nome;
  7. protected string _cognome;
  8. protected string _telefono;
  9. protected string _annullato;
  10. protected string _loginmod;
  11.  
  12. //public Agente(string login, string nome, string cognome, string
  13. telefono)
  14. public Agente()
  15. {}
  16.  
  17. public int ID
  18. {
  19. get { return _id; }
  20. set { _id = value; }
  21. }
  22. public string Login
  23. {
  24. get { return _login; }
  25. set { _login = value; }
  26. }
  27. public string Nome
  28. {
  29. get {return _nome;}
  30. set { _nome = value; }
  31. }
  32. public string Cognome
  33. {
  34. get { return _cognome; }
  35. set { _cognome = value; }
  36. }
  37. public string Telefono
  38. {
  39. get { return _telefono; }
  40. set { _telefono = value; }
  41. }
  42. public string Annullato
  43. {
  44. get { return _annullato; }
  45. set { _annullato = value.ToString(); }
  46. }
  47.  
here is the update method of Agente Object:

Expand|Select|Wrap|Line Numbers
  1. public void Update(Agente _A)
  2. {
  3. Agente AgNow = new Agente(); //AgNow=dati presenti adesso da
  4. aggiornare
  5. AgNow.AutoLoadByID(_A.ID);
  6.  
  7. //aggiorno i dati di AgNow con quelli dell'update se presenti
  8. //if (_A.ID != null) AgNow.ID = _A.ID;
  9. if (_A.Login != null) AgNow.Login = _A.Login;
  10. if (_A.Nome != null) AgNow.Nome = _A.Nome;
  11. if (_A.Cognome != null) AgNow.Cognome = _A.Cognome;
  12. if (_A.Telefono != null) AgNow.Telefono = _A.Telefono;
  13. if (_A._annullato != null) AgNow.Annullato = _A.Annullato;
  14.  
  15. MySqlCommand cmd = new MySqlCommand();
  16. if (cnn.ConnectionString == string.Empty)
  17. cnn.ConnectionString =
  18. ConfigurationManager.ConnectionStrings["reminder_local"].ToString();
  19. cmd.Connection = cnn;
  20. cmd.CommandType = CommandType.StoredProcedure;
  21. cmd.Parameters.Add(new MySqlParameter("P_ID", AgNow.ID));
  22. cmd.Parameters.Add(new MySqlParameter("P_login", AgNow.Login));
  23. cmd.Parameters.Add(new MySqlParameter("P_nome", AgNow.Nome));
  24. cmd.Parameters.Add(new MySqlParameter("P_cognome",
  25. AgNow.Cognome));
  26. cmd.Parameters.Add(new MySqlParameter("P_telefono",
  27. AgNow.Telefono));
  28. cmd.Parameters.Add(new MySqlParameter("P_annullato",
  29. AgNow.Annullato));
  30. cmd.Parameters.Add(new MySqlParameter("P_loginmod",
  31. Glob.vLogin));
  32. cmd.CommandText = "spUpdateAgente";
  33.  
  34. try
  35. {
  36. if (cnn.State == ConnectionState.Closed)
  37. cnn.Open();
  38. cmd.ExecuteNonQuery();
  39. cnn.Close();
  40. }
  41. catch (MySqlException Exception)
  42. { throw (Exception); }
  43. }
  44.  
Jan 4 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Pao
I fiund the solution
it was a problem of conversion from bool (from checkbox.checked) to
string (my object property)
protected void AgenteDataSource_OnUpdating(object sender,
ObjectDataSourceMethodEventArgs e)
{
Agente _a = (Agente)e.InputParameters[0];
CheckBox _c = (CheckBox)fwAgente.FindControl("chkAnnAg");

if (_c.Checked)
_a.Annullato = "True";
else
_a.Annullato = "False";
}
so I change the property before the raise of the update method of my
business object.

Hope this helps

Jan 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.