Checkboxes in FormView bound to ObjectDataSource

P

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:
Code:
<asp:FormView ID="fwAgente" runat="server"
AllowPaging="True"
DataKeyNames="ID"
DataSourceID="AgenteDataSource">
<ItemTemplate>
<table>
<tr><td align="right"><b>ID:</b></td>
<td><%# Eval("ID") %></td></tr>

<tr><td align="left" style="height:
21px"><b>Nome:</b></td><td style="width: 5px; height: 21px"><%#
Eval("Nome") %></td></tr>
<tr><td align="left"><b>Cognome:</b></td>
<td style="width: 5px"><%# Eval("Cognome") %></td></tr>
<tr><td align="left"><b>Telefono:</b></td>
<td style="width: 5px"><%# Eval("Telefono") %></td></tr>
<tr>
<td style="width:7%">Annullato: </td>
<td style="width:6%">
<asp:CheckBox ID="chkAnnAg"
runat="server" Enabled="false"
Checked ='<%#
Convert.ToBoolean(Eval("annullato")) %>'  />
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="EditButton"
Text="Edit"
CommandName="Edit"
RunAt="server"/>
&nbsp;
<asp:LinkButton ID="NewButton"
Text="New"
CommandName="New"
RunAt="server"/>
&nbsp;
<asp:LinkButton ID="DeleteButton"
Text="Delete"
CommandName="Delete"
RunAt="server"/>
</td>
</tr>
</table>
</ItemTemplate>

<EditItemTemplate>
<table>
<tr><td align="right"><b>ID:</b></td>
<td><asp:TextBox ID="EditIDTextBox"
Enabled="false"
Text='<%# Bind("ID") %>'

RunAt="Server"
/></td></tr>

<tr><td align="right"><b>Nome:</b></td>
<td><asp:TextBox ID="EditNomeTextBox"
Text='<%# Bind("Nome")
%>'
RunAt="Server"
/></td></tr>
<tr><td align="right"><b>Cognome:</b></td>
<td><asp:TextBox ID="EditCognomeTextBox"
Text='<%#
Bind("Cognome") %>'
RunAt="Server"
/></td></tr>
<tr><td align="right"><b>Telefono:</b></td>
<td><asp:TextBox ID="EditTelefonoTextBox"

Text='<%#
Bind("Telefono") %>'
RunAt="Server"
/></td></tr>

<tr><td align="right"><b>Annullato:</b></td>
<td style="width:6%">
<asp:CheckBox ID="chkAnnAg"
runat="server" Enabled="true"
Checked ='<%#
Convert.ToBoolean(Eval("annullato")) %>'  />
</td>
</tr>

<tr>
<td colspan="2">
<asp:LinkButton ID="UpdateButton"
Text="Update"
CommandName="Update"
RunAt="server"/>
&nbsp;
<asp:LinkButton ID="CancelUpdateButton"
Text="Cancel"
CommandName="Cancel"
RunAt="server"/>
</td>
</tr>
</table>
</EditItemTemplate>
<InsertItemTemplate>
<table>
<tr><td align="right"><b>Login:</b></td>
<td><asp:TextBox ID="InsertLoginTextBox"
Text='<%# Bind("Login")
%>'
RunAt="Server"
/></td></tr>

<tr><td align="right"><b>Nome:</b></td>
<td><asp:TextBox ID="InsertNomeTextBox"
Text='<%# Bind("Nome")
%>'
RunAt="Server"
/></td></tr>

<tr><td align="right"><b>Cognome:</b></td>
<td><asp:TextBox
ID="InsertCognomeTextBox"
Text='<%#
Bind("Cognome") %>'
RunAt="Server"
/></td></tr>

<tr><td align="right"><b>Telefono:</b></td>
<td><asp:TextBox
ID="InsertTelefonoTextBox"
Text='<%#
Bind("Telefono") %>'
RunAt="Server"
/></td></tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="InsertButton"
Text="Insert"
CommandName="Insert"
RunAt="server"/>
&nbsp;
<asp:LinkButton ID="CancelInsertButton"
Text="Cancel"
CommandName="Cancel"
RunAt="server"/>
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FormView>

and here is the objectdatasource:

Code:
<asp:ObjectDataSource ID="AgenteDataSource"
runat="server"
DataObjectTypeName="Reminder.Agente"
TypeName="Reminder.Agente"
DeleteMethod="Delete"
InsertMethod="Insert"
SelectMethod="GetDataTable"
UpdateMethod="Update"
OldValuesParameterFormatString="original_{0}"
OnUpdated="AgenteDataSource_Updated"
OnObjectCreated = "genteDataSource_ObjectCreated"

OnInserted="AgenteDataSource_Updated"
OnDeleted="AgenteDataSource_Updated"<SelectParameters>
<asp:Parameter Name="vId" Type="String" />
<asp:Parameter Name="vAnn" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>

and here is the declaration of the object

Code:
public class Agente
{
private MySqlConnection cnn = new MySqlConnection();
protected int _id;
protected string _login;
protected string _nome;
protected string _cognome;
protected string _telefono;
protected string _annullato;
protected string _loginmod;

//public Agente(string login, string nome, string cognome, string
telefono)
public Agente()
{}

public int ID
{
get { return _id; }
set { _id = value; }
}
public string Login
{
get { return _login; }
set { _login = value; }
}
public string Nome
{
get {return _nome;}
set { _nome = value; }
}
public string Cognome
{
get { return _cognome; }
set { _cognome = value; }
}
public string Telefono
{
get { return _telefono; }
set { _telefono = value; }
}
public string Annullato
{
get { return _annullato; }
set { _annullato = value.ToString(); }
}

here is the update method of Agente Object:

Code:
public void Update(Agente _A)
{
Agente AgNow = new Agente(); //AgNow=dati presenti adesso da
aggiornare
AgNow.AutoLoadByID(_A.ID);

//aggiorno i dati di AgNow con quelli dell'update se presenti
//if (_A.ID != null) AgNow.ID = _A.ID;
if (_A.Login != null) AgNow.Login = _A.Login;
if (_A.Nome != null) AgNow.Nome = _A.Nome;
if (_A.Cognome != null) AgNow.Cognome = _A.Cognome;
if (_A.Telefono != null) AgNow.Telefono = _A.Telefono;
if (_A._annullato != null) AgNow.Annullato = _A.Annullato;

MySqlCommand cmd = new MySqlCommand();
if (cnn.ConnectionString == string.Empty)
cnn.ConnectionString =
ConfigurationManager.ConnectionStrings["reminder_local"].ToString();
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new MySqlParameter("P_ID", AgNow.ID));
cmd.Parameters.Add(new MySqlParameter("P_login", AgNow.Login));
cmd.Parameters.Add(new MySqlParameter("P_nome", AgNow.Nome));
cmd.Parameters.Add(new MySqlParameter("P_cognome",
AgNow.Cognome));
cmd.Parameters.Add(new MySqlParameter("P_telefono",
AgNow.Telefono));
cmd.Parameters.Add(new MySqlParameter("P_annullato",
AgNow.Annullato));
cmd.Parameters.Add(new MySqlParameter("P_loginmod",
Glob.vLogin));
cmd.CommandText = "spUpdateAgente";

try
{
if (cnn.State == ConnectionState.Closed)
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
}
catch (MySqlException Exception)
{ throw (Exception); }
}
 
P

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top